/system/app/Store/Mapper/PickupLocationMapper.php

https://github.com/seosamba/ecommerce · PHP · 149 lines · 99 code · 16 blank · 34 comment · 8 complexity · 193221933210de14f5e97c6748e97dda MD5 · raw file

  1. <?php
  2. /**
  3. * PickupLocation.php
  4. *
  5. * @method Store_Mapper_PickupLocationMapper getInstance() getInstance() Returns an instance of itself
  6. * @method Zend_Db_Table getDbTable() getDbTable() Returns an instance of DbTable
  7. */
  8. class Store_Mapper_PickupLocationMapper extends Application_Model_Mappers_Abstract
  9. {
  10. protected $_model = 'Store_Model_PickupLocation';
  11. protected $_dbTable = 'Store_DbTable_PickupLocation';
  12. protected static $_lastQueryResultCount = false;
  13. public function lastQueryResultCount($flag)
  14. {
  15. self::$_lastQueryResultCount = (bool)$flag;
  16. return $this;
  17. }
  18. /**
  19. * Save pickup locations data
  20. *
  21. * @param Store_Model_PickupLocation $model
  22. * @return bool|mixed
  23. */
  24. public function save($model)
  25. {
  26. if (!$model instanceof $this->_model) {
  27. $model = new $this->_model($model);
  28. }
  29. $data = array(
  30. 'address1' => $model->getAddress1(),
  31. 'address2' => $model->getAddress2(),
  32. 'zip' => $model->getZip(),
  33. 'country' => $model->getCountry(),
  34. 'city' => $model->getCity(),
  35. 'working_hours' => $model->getWorkingHours(),
  36. 'phone' => $model->getPhone(),
  37. 'location_category_id' => $model->getLocationCategoryId(),
  38. 'name' => $model->getName(),
  39. 'lat' => $model->getLat(),
  40. 'lng' => $model->getLng(),
  41. 'notes' => $model->getNotes(),
  42. 'weight' => $model->getWeight(),
  43. 'external_id' => $model->getExternalId(),
  44. 'allowed_to_delete' => $model->getAllowedToDelete()
  45. );
  46. if ($model->getId() === null) {
  47. $result = $this->getDbTable()->insert($data);
  48. $model->setId($result);
  49. } else {
  50. $where = $this->getDbTable()->getAdapter()->quoteInto('id = ?', $model->getId());
  51. $result = (bool)$this->getDbTable()->update($data, $where);
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Get pickup locations data
  57. *
  58. * @param int $categoryId (category(zone id) of the pickup locations)
  59. * @param string $order OPTIONAL An SQL ORDER clause.
  60. * @param int $limit OPTIONAL An SQL LIMIT count.
  61. * @param int $offset OPTIONAL An SQL LIMIT offset.
  62. * @return array
  63. */
  64. public function fetchAll($categoryId = null, $order = null, $limit = null, $offset = null)
  65. {
  66. $select = $this->getDbTable()->select(Zend_Db_Table::SELECT_WITHOUT_FROM_PART)
  67. ->setIntegrityCheck(false)
  68. ->from(array('shopping_pickup_location'));
  69. if (!empty($order)) {
  70. $select->order($order);
  71. }
  72. if ($categoryId) {
  73. $where = $this->getDbTable()->getAdapter()->quoteInto('location_category_id = ?', $categoryId);
  74. $select->where($where);
  75. }
  76. if (self::$_lastQueryResultCount) {
  77. $data = $this->getDbTable()->fetchAll($select)->toArray();
  78. return array(
  79. 'totalRecords' => sizeof($data),
  80. 'data' => array_slice($data, $offset, $limit),
  81. 'offset' => $offset,
  82. 'limit' => $limit
  83. );
  84. }
  85. $select->limit($limit, $offset);
  86. return $this->getDbTable()->fetchAll($select)->toArray();
  87. }
  88. /**
  89. * Delete single pickup location
  90. *
  91. * @param int $id
  92. * @return array
  93. */
  94. public function delete($id)
  95. {
  96. $result = array();
  97. $rowset = $this->getDbTable()->find($id);
  98. foreach ($rowset as $row) {
  99. $result[$row->id] = $row->delete();
  100. }
  101. return $result;
  102. }
  103. /**
  104. * @param $countries
  105. * @return int
  106. */
  107. public function deleteLocationsBeforeProcess($countries) {
  108. $where = $this->getDbTable()->getAdapter()->quoteInto('country IN (?)', $countries);
  109. $where .= ' AND ' . $this->getDbTable()->getAdapter()->quoteInto('allowed_to_delete = ?', '1');
  110. return $this->getDbTable()->delete($where);
  111. }
  112. public function findLocationByExternalId($id, $withAlloowedToDelete = false) {
  113. if(!empty($id)) {
  114. $where = $this->getDbTable()->getAdapter()->quoteInto('external_id = ?', $id);
  115. if($withAlloowedToDelete) {
  116. $where .= ' AND ' . $this->getDbTable()->getAdapter()->quoteInto('allowed_to_delete = ?', '1');
  117. }
  118. return $this->_findWhere($where);
  119. }
  120. return null;
  121. }
  122. /**
  123. * @return array
  124. */
  125. public function getUniqueCountries()
  126. {
  127. $select = $this->getDbTable()->select(Zend_Db_Table::SELECT_WITHOUT_FROM_PART)
  128. ->setIntegrityCheck(false)
  129. ->from(array('shopping_pickup_location'), array('country'));
  130. $select->group('country');
  131. return $this->getDbTable()->getAdapter()->fetchCol($select);
  132. }
  133. }