LinkRedirects.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ TstYLto9/R/fBHbctEnS2Cm1Xc5ijlB/HsHBV/A0xcw=
  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. * link redirects table service class
  14. */
  15. namespace Ppb\Service\Table;
  16. use Ppb\Db\Table\LinkRedirects as LinkRedirectsTable,
  17. Cube\Db\Table\AbstractTable;
  18. class LinkRedirects extends AbstractServiceTable
  19. {
  20. /**
  21. *
  22. * number of insert rows that appear at the bottom of a table form
  23. *
  24. * @var integer
  25. */
  26. protected $_insertRows = 3;
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. $this->setTable(
  31. new LinkRedirectsTable());
  32. }
  33. /**
  34. *
  35. * get all table columns needed to generate the
  36. * link redirects management table in the admin area
  37. *
  38. * @return array
  39. */
  40. public function getColumns()
  41. {
  42. return array(
  43. array(
  44. 'label' => $this->_('Original Link'),
  45. 'class' => 'size-large',
  46. 'element_id' => 'old_link',
  47. ),
  48. array(
  49. 'label' => $this->_('New Link'),
  50. 'element_id' => 'new_link',
  51. ),
  52. array(
  53. 'label' => $this->_('Redirect Code'),
  54. 'class' => 'size-mini',
  55. 'element_id' => 'redirect_code',
  56. ),
  57. array(
  58. 'label' => $this->_('Order ID'),
  59. 'class' => 'size-mini',
  60. 'element_id' => 'order_id',
  61. ),
  62. array(
  63. 'label' => $this->_('Delete'),
  64. 'class' => 'size-mini',
  65. 'element_id' => array(
  66. 'id', 'delete'
  67. ),
  68. ),
  69. );
  70. }
  71. /**
  72. *
  73. * get all form elements that are needed to generate the
  74. * link redirects management table in the admin area
  75. *
  76. * @return array
  77. */
  78. public function getElements()
  79. {
  80. return array(
  81. array(
  82. 'id' => 'id',
  83. 'element' => 'hidden',
  84. ),
  85. array(
  86. 'id' => 'old_link',
  87. 'element' => 'text',
  88. 'attributes' => array(
  89. 'class' => 'form-control input-block-level',
  90. 'placeholder' => $this->_('String / regex format'),
  91. ),
  92. ),
  93. array(
  94. 'id' => 'new_link',
  95. 'element' => 'text',
  96. 'attributes' => array(
  97. 'class' => 'form-control input-medium',
  98. 'placeholder' => $this->_('String / sprintf format'),
  99. ),
  100. ),
  101. array(
  102. 'id' => 'redirect_code',
  103. 'element' => 'text',
  104. 'attributes' => array(
  105. 'class' => 'form-control input-mini',
  106. ),
  107. ),
  108. array(
  109. 'id' => 'order_id',
  110. 'element' => 'text',
  111. 'attributes' => array(
  112. 'class' => 'form-control input-mini',
  113. ),
  114. ),
  115. array(
  116. 'id' => 'delete',
  117. 'element' => 'checkbox',
  118. ),
  119. );
  120. }
  121. /**
  122. *
  123. * save data in the table (update if an id exists or insert otherwise)
  124. *
  125. * TODO: PROBLEM WITH "max_input_vars" php setting for big arrays of data
  126. *
  127. * @param array $data
  128. *
  129. * @return \Ppb\Service\Table\AbstractServiceTable
  130. * @throws \InvalidArgumentException
  131. */
  132. public function save(array $data)
  133. {
  134. if (!isset($data['id'])) {
  135. throw new \InvalidArgumentException("The form must use an element with the name 'id'.");
  136. }
  137. $columns = array_keys($data);
  138. $tableColumns = array_flip(array_values($this->getTable()->info(AbstractTable::COLS)));
  139. foreach ($data['id'] as $key => $value) {
  140. $row = $this->_table->fetchRow("id='{$value}'");
  141. $input = array();
  142. foreach ($columns as $column) {
  143. if (isset($data[$column][$key]) && array_key_exists($column, $tableColumns)) {
  144. $input[$column] = $data[$column][$key];
  145. }
  146. }
  147. $input = $this->_prepareSaveData($input);
  148. if (count($row) > 0) {
  149. $this->_table->update($input, "id='{$value}'");
  150. }
  151. else if (count(array_filter($input)) > 0 && !empty($input[$this->getMainColumn($columns)])) {
  152. $this->_table->insert($input);
  153. }
  154. }
  155. return $this;
  156. }
  157. /**
  158. *
  159. * fetches all matched rows
  160. *
  161. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  162. * @param string|array $order
  163. * @param int $count
  164. * @param int $offset
  165. *
  166. * @return array
  167. */
  168. public function fetchAll($where = null, $order = null, $count = null, $offset = null)
  169. {
  170. if ($order === null) {
  171. $order = 'order_id ASC, id ASC';
  172. }
  173. return parent::fetchAll($where, $order, $count, $offset);
  174. }
  175. }