UsersAddressBook.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ XubaWWqi8sJjRoa6EkQ1vqsujJyOFyvZAZsIfKglcVA=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.0
  11. */
  12. /**
  13. * users address book table service class
  14. */
  15. namespace Ppb\Service;
  16. use Ppb\Db\Table\UsersAddressBook as UsersAddressBookTable,
  17. Cube\Controller\Front,
  18. Ppb\Model\Elements,
  19. Ppb\Db\Table\Row\User as UserModel;
  20. class UsersAddressBook extends AbstractService
  21. {
  22. /**
  23. *
  24. * columns to be saved in the serializable address field
  25. * TODO: get from user model
  26. *
  27. * @var array
  28. */
  29. protected $_addressFields = array();
  30. /**
  31. *
  32. * class constructor
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. $this->setTable(
  38. new UsersAddressBookTable());
  39. }
  40. /**
  41. *
  42. * set address fields (by default from user elements model)
  43. *
  44. * @param array $addressFields
  45. * @return array
  46. */
  47. public function setAddressFields($addressFields = null)
  48. {
  49. if ($addressFields === null) {
  50. $userModel = new Elements\User();
  51. $elements = $userModel->getElements();
  52. foreach ($elements as $element) {
  53. if (array_intersect((array)$element['form_id'], array('address')) && $element['element'] != 'hidden') {
  54. $addressFields[] = $element['id'];
  55. }
  56. }
  57. }
  58. $this->_addressFields = $addressFields;
  59. return $this;
  60. }
  61. /**
  62. *
  63. * get address fields
  64. *
  65. * @return array
  66. */
  67. public function getAddressFields()
  68. {
  69. if (!$this->_addressFields) {
  70. $this->setAddressFields();
  71. }
  72. return $this->_addressFields;
  73. }
  74. /**
  75. *
  76. * save a user's address in the address book table
  77. *
  78. * @param array $data address data (serializable)
  79. * @param int $userId user id
  80. * @param string $prefix the prefix of the input fields (optional)
  81. * @return int the id of the saved column
  82. * @throws \InvalidArgumentException
  83. */
  84. public function save($data, $userId, $prefix = null)
  85. {
  86. $row = array();
  87. if ($prefix !== null) {
  88. foreach ($data as $key => $value) {
  89. if (strstr($key, $prefix) !== false) {
  90. unset($data[$key]);
  91. $key = str_replace($prefix, '', $key);
  92. $data[$key] = $value;
  93. }
  94. }
  95. }
  96. if (isset($data['address_id'])) {
  97. $select = $this->_table->select()
  98. ->where('user_id = ?', $userId)
  99. ->where("id = ?", $data['address_id']);
  100. $row = $this->_table->fetchRow($select);
  101. }
  102. $address = array();
  103. $addressFields = $this->getAddressFields();
  104. foreach ($addressFields as $field) {
  105. $address[$field] = (isset($data[$field])) ? $data[$field] : null;
  106. }
  107. if (count($row) > 0) {
  108. $this->_table->update(array(
  109. 'address' => serialize($address),
  110. ), "id='{$row['id']}'");
  111. $id = $row['id'];
  112. }
  113. else {
  114. $this->_table->insert(array(
  115. 'user_id' => $userId,
  116. 'address' => serialize($address),
  117. ));
  118. $id = $this->_table->lastInsertId();
  119. }
  120. // one address needs to be set as primary
  121. $select = $this->_table->select()
  122. ->where("user_id = ?", $userId)
  123. ->where('is_primary = ?', 1);
  124. $rowset = $this->_table->fetchAll($select);
  125. if (!count($rowset)) {
  126. $this->_table->update(array(
  127. 'is_primary' => 1
  128. ), "id='{$id}'");
  129. }
  130. return $id;
  131. }
  132. /**
  133. *
  134. * get the addresses of a user in a key => value format
  135. * to be used for the shipping address selector
  136. *
  137. * @param UserModel $user
  138. * @param string $separator
  139. * @param bool $enhanced if true, it will return the data as an array, usable by the SelectAddress form element
  140. * @return array
  141. */
  142. public function getMultiOptions(UserModel $user, $separator = ', ', $enhanced = false)
  143. {
  144. $data = array();
  145. $view = Front::getInstance()->getBootstrap()->getResource('view');
  146. /** @var \Members\View\Helper\UserDetails $userDetails */
  147. $userDetails = $view->getHelper('userDetails');
  148. $object = clone $user;
  149. $select = $this->getTable()->select()
  150. ->order('is_primary DESC, id DESC');
  151. $addresses = $user->findDependentRowset('\Ppb\Db\Table\UsersAddressBook', null, $select);
  152. foreach ($addresses as $address) {
  153. $title = $userDetails->userDetails($object)->setAddress($address)->displayAddress($separator);
  154. if ($enhanced) {
  155. $data[(string)$address['id']] = array(
  156. 'title' => $title,
  157. 'locationId' => $address['country'],
  158. 'postCode' => $address['zip_code'],
  159. );
  160. }
  161. else {
  162. $data[(string)$address['id']] = $title;
  163. }
  164. }
  165. return $data;
  166. }
  167. }