/system/app/Models/Mapper/Zone.php

https://github.com/seosamba/ecommerce · PHP · 223 lines · 155 code · 35 blank · 33 comment · 12 complexity · e21f4382129cdb944849ce4adf357bba MD5 · raw file

  1. <?php
  2. /**
  3. * Zone
  4. *
  5. * @author Pavel Kovalyov <pavlo.kovalyov@gmail.com>
  6. */
  7. class Models_Mapper_Zone extends Application_Model_Mappers_Abstract {
  8. protected $_dbTable = 'Models_DbTable_Zone';
  9. protected $_model = 'Models_Model_Zone';
  10. public function save($model) {
  11. if (!$model instanceof $this->_model){
  12. $model = new $this->_model($model);
  13. }
  14. $data = array(
  15. 'name' => $model->getName()
  16. );
  17. if ($model->getId() === null){
  18. $result = $this->getDbTable()->insert($data);
  19. $model->setId($result);
  20. } else {
  21. $where = $this->getDbTable()->getAdapter()->quoteInto('id = ?', $model->getId());
  22. $result = (bool) $this->getDbTable()->update($data, $where);
  23. }
  24. $this->_updateCountries($model);
  25. $this->_updateStates($model);
  26. $this->_updateZip($model);
  27. return $result;
  28. }
  29. public function find($id) {
  30. $result = $this->getDbTable()->find($id);
  31. if ($result->count() == 0){
  32. return null;
  33. }
  34. $data = $result->current();
  35. $zone = new $this->_model($data->toArray());
  36. $zoneCountries = $data->findManyToManyRowset('Models_DbTable_Country','Models_DbTable_ZoneCountry');
  37. $zone->setCountries($zoneCountries->toArray());
  38. $zoneStates = $data->findManyToManyRowset('Models_DbTable_State','Models_DbTable_ZoneState');
  39. $zone->setStates($zoneStates->toArray());
  40. $zoneZip = $data->findDependentRowset('Models_DbTable_Zip');
  41. $zone->setZip($zoneZip->toArray());
  42. return $zone->toArray();
  43. }
  44. public function fetchAll($where = null, $order = array()) {
  45. $entries = array();
  46. $resultSet = $this->getDbTable()->fetchAll($where, $order);
  47. if(null !== $resultSet) {
  48. foreach ($resultSet as $row) {
  49. $zone = new $this->_model($row->toArray());
  50. $zone->setCountries($row->findManyToManyRowset('Models_DbTable_Country','Models_DbTable_ZoneCountry')->toArray());
  51. $zone->setStates($row->findManyToManyRowset('Models_DbTable_State','Models_DbTable_ZoneState')->toArray());
  52. $zone->setZip($row->findDependentRowset('Models_DbTable_Zip')->toArray());
  53. $entries[] = $zone;
  54. unset ($zone);
  55. }
  56. }
  57. return $entries;
  58. }
  59. public function createModel($data = null){
  60. return new $this->_model($data);
  61. }
  62. private function _updateCountries($zone){
  63. $countryTable = new Models_DbTable_Country();
  64. $newCountries = $zone->getCountries();
  65. $newCodesList = array();
  66. foreach ($newCountries as $country) {
  67. array_push($newCodesList, $country['country']);
  68. }
  69. if (!empty($newCodesList)){
  70. $sql = $countryTable->getAdapter()->select()->from($countryTable->info('name'))
  71. ->where('country in (?)', $newCodesList);
  72. $list = $countryTable->getAdapter()->fetchAll($sql);
  73. } else {
  74. $list = array();
  75. }
  76. $zoneCountryTable = new Models_DbTable_ZoneCountry();
  77. $zoneCountryTable->delete($zoneCountryTable->getAdapter()->quoteInto('zone_id = ?', $zone->getId()));
  78. if (!empty ($list)) {
  79. $zoneCountryTable->getAdapter()->beginTransaction();
  80. foreach ($list as $country) {
  81. $zoneCountryTable->insert(array(
  82. 'zone_id' => $zone->getId(),
  83. 'country_id' => $country['id']
  84. ));
  85. }
  86. $zoneCountryTable->getAdapter()->commit();
  87. }
  88. return $this;
  89. }
  90. private function _updateStates($zone){
  91. $states = $zone->getStates();
  92. $zoneStateTable = new Models_DbTable_ZoneState();
  93. $zoneStateTable->delete($zoneStateTable->getAdapter()->quoteInto('zone_id = ?', $zone->getId()));
  94. if (!empty($states)){
  95. $zoneStateTable->getAdapter()->beginTransaction();
  96. foreach ($states as $state) {
  97. $zoneStateTable->insert(array(
  98. 'zone_id' => $zone->getId(),
  99. 'state_id' => $state['id']
  100. ));
  101. }
  102. $zoneStateTable->getAdapter()->commit();
  103. }
  104. return $this;
  105. }
  106. private function _updateZip($zone) {
  107. $zip = $zone->getZip();
  108. $zoneZipTable = new Models_DbTable_Zip();
  109. $zoneZipTable->delete($zoneZipTable->getAdapter()->quoteInto('zone_id = ?', $zone->getId()));
  110. if (!empty ($zip)){
  111. $zoneZipTable->getAdapter()->beginTransaction();
  112. foreach ($zip as $code){
  113. if (!empty($code)) {
  114. $zoneZipTable->insert(array(
  115. 'zone_id' => $zone->getId(),
  116. 'zip' => $code
  117. ));
  118. }
  119. }
  120. $zoneZipTable->getAdapter()->commit();
  121. }
  122. return $this;
  123. }
  124. public function delete($id) {
  125. $result = array();
  126. $rowset = $this->getDbTable()->find($id);
  127. foreach ($rowset as $row) {
  128. $result[$row->id] = $row->delete();
  129. }
  130. return $result;
  131. }
  132. /**
  133. * Delete countries by zone id
  134. *
  135. * @param int $zoneId zone id
  136. * @throws Exception
  137. */
  138. public function deleteCountriesByZoneId($zoneId)
  139. {
  140. $where = $this->getDbTable()->getAdapter()->quoteInto('zone_id = ?', $zoneId);
  141. $this->getDbTable()->getAdapter()->delete('shopping_zone_country', $where);
  142. }
  143. /**
  144. * Delete states by zone id
  145. *
  146. * @param int $zoneId zone id
  147. * @throws Exception
  148. */
  149. public function deleteStatesByZoneId($zoneId)
  150. {
  151. $where = $this->getDbTable()->getAdapter()->quoteInto('zone_id = ?', $zoneId);
  152. $this->getDbTable()->getAdapter()->delete('shopping_zone_state', $where);
  153. }
  154. /**
  155. * Delete zips by zone id
  156. *
  157. * @param int $zoneId zone id
  158. * @throws Exception
  159. */
  160. public function deleteZipsByZoneId($zoneId)
  161. {
  162. $where = $this->getDbTable()->getAdapter()->quoteInto('zone_id = ?', $zoneId);
  163. $this->getDbTable()->getAdapter()->delete('shopping_zone_zip', $where);
  164. }
  165. /**
  166. * Get zone names
  167. *
  168. * @return mixed
  169. * @throws Exception
  170. */
  171. public function getZoneNames()
  172. {
  173. $select = $this->getDbTable()->getAdapter()->select()->from('shopping_zone');
  174. return $this->getDbTable()->getAdapter()->fetchPairs($select);
  175. }
  176. /**
  177. * @return array
  178. * @throws Zend_Db_Table_Exception
  179. */
  180. public function getSavedZoneCountries() {
  181. $zoneCountryTable = new Models_DbTable_ZoneCountry();
  182. $sql = $zoneCountryTable->getAdapter()->select()->distinct()->from($zoneCountryTable->info('name'), array(
  183. 'country_id',
  184. ));
  185. $countriesList = $zoneCountryTable->getAdapter()->fetchCol($sql);
  186. return $countriesList;
  187. }
  188. }