UserAddressBook.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ QjNDoWzRmcsxshHaeMx+iPN51BkRa7RwH3IFv6Y9maw=
  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 row object model
  14. */
  15. namespace Ppb\Db\Table\Row;
  16. class UserAddressBook extends AbstractRow
  17. {
  18. /**
  19. *
  20. * serializable fields
  21. *
  22. * @var array
  23. */
  24. protected $_serializable = array('address');
  25. /**
  26. *
  27. * check if the address is the user's primary address
  28. *
  29. * @return bool
  30. */
  31. public function isPrimary()
  32. {
  33. return (bool)$this->getData('is_primary');
  34. }
  35. /**
  36. *
  37. * set the address as primary
  38. *
  39. * @return bool
  40. */
  41. public function setPrimary()
  42. {
  43. if (!$this->isPrimary()) {
  44. $addresses = $this->findParentRow('\Ppb\Db\Table\Users')
  45. ->findDependentRowset('\Ppb\Db\Table\UsersAddressBook');
  46. /** @var \Ppb\Db\Table\Row\UserAddressBook $address */
  47. foreach ($addresses as $address) {
  48. $address->save(array(
  49. 'is_primary' => ($this->getData('id') == $address['id']) ? 1 : 0,
  50. ));
  51. }
  52. return true;
  53. }
  54. return false;
  55. }
  56. /**
  57. *
  58. * check if an address can be edited (if not part of an invoice)
  59. * or return an error message string otherwise
  60. *
  61. * @return bool|string
  62. */
  63. public function canEdit()
  64. {
  65. $translate = $this->getTranslate();
  66. if ($this->_usedInSales() && !$this->isPrimary()) {
  67. return $translate->_('Cannot edit an address that is used in an invoice.');
  68. }
  69. return true;
  70. }
  71. /**
  72. *
  73. * check if an address can be deleted (if not part of an invoice or the primary address)
  74. * or return an error message string otherwise
  75. *
  76. * @return bool|string
  77. */
  78. public function canDelete()
  79. {
  80. $translate = $this->getTranslate();
  81. if ($this->isPrimary()) {
  82. return $translate->_('The primary address cannot be deleted.');
  83. }
  84. else if ($this->_usedInSales()) {
  85. return $translate->_('This address cannot be removed because it is used in an invoice.');
  86. }
  87. return true;
  88. }
  89. /**
  90. *
  91. * check if the address has been used in a purchase as billing and/or shipping address
  92. *
  93. * @return bool
  94. */
  95. protected function _usedInSales()
  96. {
  97. $shippingAddress = $this->findDependentRowset('\Ppb\Db\Table\Sales', 'BillingAddress');
  98. if (count($shippingAddress) > 0) {
  99. return true;
  100. }
  101. else {
  102. $billingAddress = $this->findDependentRowset('\Ppb\Db\Table\Sales', 'ShippingAddress');
  103. if (count($billingAddress) > 0) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. }