ShippingCarriers.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ lJDOdMT1an/qHaOrKol6U2qTA6skzLsfxcaYnijNI5s=
  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. * shipping carriers and carrier settings tables service class
  14. */
  15. namespace Ppb\Service\Table;
  16. use Ppb\Db\Table,
  17. Cube\Db\Expr;
  18. class ShippingCarriers extends AbstractServiceTable
  19. {
  20. /**
  21. *
  22. * gateways settings table
  23. *
  24. * @var \Ppb\Db\Table\ShippingCarriersSettings
  25. */
  26. protected $_shippingCarriersSettings;
  27. /**
  28. *
  29. * class constructor
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. $this->setTable(
  35. new Table\ShippingCarriers());
  36. $this->setShippingCarriersSettings();
  37. }
  38. /**
  39. *
  40. * set the table that will be used by the service
  41. *
  42. * @param \Ppb\Db\Table\ShippingCarriersSettings $shippingCarriersSettings
  43. * @return \Ppb\Service\Table\ShippingCarriers
  44. */
  45. public function setShippingCarriersSettings(Table\ShippingCarriersSettings $shippingCarriersSettings = null)
  46. {
  47. if (!$shippingCarriersSettings instanceof Table\ShippingCarriersSettings) {
  48. $shippingCarriersSettings = new Table\ShippingCarriersSettings();
  49. }
  50. $this->_shippingCarriersSettings = $shippingCarriersSettings;
  51. return $this;
  52. }
  53. /**
  54. *
  55. * get gateways data from the 'shipping_carriers' and 'shipping_carriers_settings' tables
  56. *
  57. * @param int $carrierIds carrier ids (to fetch data for specific carriers)
  58. * @param bool $activeOnly show only active carriers
  59. * @return array
  60. */
  61. public function getData($carrierIds = null, $activeOnly = false)
  62. {
  63. $select = $this->_table->select();
  64. if ($carrierIds !== null) {
  65. $select->where('id IN (?)', new Expr(implode(', ', (array)$carrierIds)));
  66. }
  67. if ($activeOnly) {
  68. $select->where('enabled = ?', 1);
  69. }
  70. $carriers = $this->fetchAll($select)->toArray();
  71. foreach ($carriers as $key => $carrier) {
  72. $select = $this->_shippingCarriersSettings->select()
  73. ->where('carrier_id = ?', $carrier['id']);
  74. $carrierParams = $this->fetchAll($select);
  75. foreach ($carrierParams as $param) {
  76. $carriers[$key][$param['name']] = $param['value'];
  77. }
  78. }
  79. return (count($carriers) == 1) ? $carriers[0] : $carriers;
  80. }
  81. /**
  82. *
  83. * get all active carriers
  84. *
  85. * @return array
  86. */
  87. public function getMultiOptions()
  88. {
  89. $data = array();
  90. $translate = $this->getTranslate();
  91. $select = $this->_table->select()
  92. ->where('enabled = ?', 1);
  93. $rows = $this->_table->fetchAll($select);
  94. foreach ($rows as $row) {
  95. $data[(string)$row['id']] = $translate->_($row['name']);
  96. }
  97. return $data;
  98. }
  99. /**
  100. *
  101. * save data in the tables (for the 'shipping_carriers_settings' table, insert if key doesnt exist or update if it does)
  102. *
  103. * @param array $data
  104. * @return \Ppb\Service\Table\ShippingCarriers
  105. * @throws \InvalidArgumentException
  106. */
  107. public function save(array $data)
  108. {
  109. if (!isset($data['id'])) {
  110. throw new \InvalidArgumentException("The form must use an element with the name 'id'.");
  111. }
  112. $columns = array('id', 'enabled');
  113. $params = $data;
  114. foreach ($data as $key => $value) {
  115. if (!in_array($key, $columns)) {
  116. unset($data[$key]);
  117. }
  118. else {
  119. unset($params[$key]);
  120. }
  121. }
  122. foreach ($params as $key => $param) {
  123. $carrierId = key($param);
  124. $value = $param[$carrierId];
  125. $select = $this->_shippingCarriersSettings->select()
  126. ->where('name = ?', $key)
  127. ->where('carrier_id = ?', $carrierId);
  128. $row = $this->_shippingCarriersSettings->fetchRow($select);
  129. if (count($row) > 0) {
  130. $this->_shippingCarriersSettings->update(
  131. array('value' => $value), "id = '{$row['id']}'");
  132. }
  133. else {
  134. $input = array(
  135. 'name' => $key,
  136. 'value' => $value,
  137. 'carrier_id' => $carrierId,
  138. );
  139. $this->_shippingCarriersSettings->insert($input);
  140. }
  141. }
  142. parent::save($data);
  143. return $this;
  144. }
  145. public function getColumns()
  146. {
  147. }
  148. public function getElements()
  149. {
  150. }
  151. }