CustomFields.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ R2tB54vMcPc078VSLaJmKSpbruf7KR9VezjJSRGM8BY=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.7
  11. */
  12. /**
  13. * custom fields table service class
  14. *
  15. * IMPORTANT:
  16. * search custom fields by multiple categories:
  17. * select * from custom_fields where category_ids REGEXP '"x"|"y"|"z"';
  18. */
  19. namespace Ppb\Service;
  20. use Ppb\Db\Table;
  21. class CustomFields extends AbstractService
  22. {
  23. /**
  24. *
  25. * allowed custom field types
  26. *
  27. * @var array
  28. */
  29. protected $_customFieldTypes = array(
  30. 'user', 'item');
  31. /**
  32. *
  33. * class constructor
  34. */
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. $this->setTable(
  39. new Table\CustomFields());
  40. }
  41. /**
  42. *
  43. * get allowed custom field types
  44. *
  45. * @return array
  46. */
  47. public function getCustomFieldTypes()
  48. {
  49. return $this->_customFieldTypes;
  50. }
  51. /**
  52. *
  53. * save custom field object in the custom fields table
  54. *
  55. * @param array $data
  56. * @return \Ppb\Service\CustomFields
  57. */
  58. public function save($data)
  59. {
  60. $row = null;
  61. $data = $this->_prepareSaveData($data);
  62. if (array_key_exists('id', $data)) {
  63. $select = $this->_table->select()
  64. ->where("id = ?", $data['id']);
  65. unset($data['id']);
  66. $row = $this->_table->fetchRow($select);
  67. }
  68. if (count($row) > 0) {
  69. $this->_table->update($data, "id='{$row['id']}'");
  70. }
  71. else {
  72. $this->_table->insert($data);
  73. }
  74. return $this;
  75. }
  76. /**
  77. *
  78. * delete a custom field from the table
  79. *
  80. * @param integer $id the id of the custom field
  81. * @return integer returns the number of affected rows
  82. */
  83. public function delete($id)
  84. {
  85. $where = $this->_table->getAdapter()->quoteInto('id = ?', $id);
  86. return $this->_table->delete($where);
  87. }
  88. /**
  89. *
  90. * get certain custom fields based on a set of queries
  91. *
  92. * @param array $data the search data used to return the requested fields
  93. * @param mixed $order order by field(s)
  94. * @return \Cube\Db\Table\Rowset\AbstractRowset
  95. */
  96. public function getFields(array $data = null, $order = null)
  97. {
  98. $select = $this->_table->select();
  99. foreach ((array)$data as $key => $value) {
  100. if ($key === 'category_ids') {
  101. $select->where("category_ids REGEXP '\"" . implode('"|"',
  102. array_unique($value)) . "\"' OR category_ids = ''");
  103. }
  104. else {
  105. $select->where("{$key} = ?", $value);
  106. }
  107. }
  108. if ($order === null) {
  109. $order = array('active DESC', 'order_id ASC');
  110. }
  111. $select->order($order);
  112. return $this->fetchAll($select);
  113. }
  114. /**
  115. *
  116. * save custom fields settings (order etc)
  117. *
  118. * @param array $data
  119. * @return $this
  120. * @throws \InvalidArgumentException
  121. */
  122. public function saveBrowseSettings(array $data)
  123. {
  124. if (!isset($data['id'])) {
  125. throw new \InvalidArgumentException("The form must use an element with the name 'id'.");
  126. }
  127. $columns = array_keys($data);
  128. foreach ((array)$data['id'] as $key => $value) {
  129. $row = $this->_table->fetchRow("id='{$value}'");
  130. $input = array();
  131. foreach ($columns as $column) {
  132. if (is_array($data[$column]) && isset($data[$column][$key])) {
  133. $input[$column] = $data[$column][$key];
  134. }
  135. }
  136. $input = parent::_prepareSaveData($input);
  137. if (count($row) > 0) {
  138. $this->_table->update($input, "id='{$value}'");
  139. }
  140. }
  141. return $this;
  142. }
  143. }