CustomFieldsData.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ ZRV4ap0dm8YTIJ3igakzQCA/TZT5c1ueZVq7mFgDF/8=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.9 [rev.7.9.01]
  11. */
  12. /**
  13. * custom fields data table service class
  14. *
  15. * IMPORTANT:
  16. * search serialized custom fields:
  17. * select * from probid_custom_fields_data where value REGEXP '"x"|"y"|"z"';
  18. * (maybe we will serialize all saved data)
  19. *
  20. * @7.9: empty multi choice values are saved as an empty string rather than as an empty serialized array
  21. */
  22. namespace Ppb\Service;
  23. use Ppb\Db\Table;
  24. class CustomFieldsData extends AbstractService
  25. {
  26. /**
  27. *
  28. * custom fields and custom fields data tables service
  29. *
  30. * @var \Ppb\Service\CustomFields
  31. */
  32. protected $_customFields;
  33. /**
  34. *
  35. * class constructor
  36. */
  37. public function __construct()
  38. {
  39. parent::__construct();
  40. $this->setTable(
  41. new Table\CustomFieldsData());
  42. }
  43. /**
  44. *
  45. * get custom fields table service
  46. *
  47. * @return \Ppb\Service\CustomFields
  48. */
  49. public function getCustomFieldsService()
  50. {
  51. if (!$this->_customFields instanceof CustomFields) {
  52. $this->setCustomFieldsService(
  53. new CustomFields());
  54. }
  55. return $this->_customFields;
  56. }
  57. /**
  58. *
  59. * set custom fields table service
  60. *
  61. * @param \Ppb\Service\CustomFields $customFields
  62. *
  63. * @return $this
  64. */
  65. public function setCustomFieldsService(CustomFields $customFields)
  66. {
  67. $this->_customFields = $customFields;
  68. return $this;
  69. }
  70. /**
  71. *
  72. * save data in the table
  73. *
  74. * 7.7: data is only saved if the custom field exists - workaround for when using the bulk lister and having
  75. * custom fields columns that do not exist
  76. *
  77. * @param string|array $value custom field value (if array it will be serialized before saving)
  78. * @param string $type custom field data type (item, user etc)
  79. * @param integer $fieldId custom_fields table id
  80. * @param integer $ownerId id of the column for which this data belongs to
  81. *
  82. * @return $this
  83. */
  84. public function save($value, $type, $fieldId, $ownerId)
  85. {
  86. $customField = $this->getCustomFieldsService()->findBy('id', $fieldId);
  87. if (count($customField) > 0) {
  88. if (is_array($value)) {
  89. $value = array_filter($value);
  90. $value = (!empty($value)) ? serialize($value) : '';
  91. }
  92. $data = array(
  93. 'value' => strval($value),
  94. 'field_id' => intval($fieldId),
  95. 'owner_id' => intval($ownerId),
  96. 'type' => strval($type),
  97. );
  98. $select = $this->_table->select()
  99. ->where("field_id = ?", $fieldId)
  100. ->where("owner_id = ?", $ownerId)
  101. ->where("type = ?", $type);
  102. $row = $this->_table->fetchRow($select);
  103. if (count($row) > 0) {
  104. $this->_table->update($data, "id='{$row['id']}'");
  105. }
  106. else {
  107. $this->_table->insert($data);
  108. }
  109. }
  110. return $this;
  111. }
  112. /**
  113. *
  114. * delete data from the table
  115. *
  116. * @param string $type custom field type
  117. * @param integer $ownerId the id of record that the custom field data belongs to
  118. *
  119. * @return integer returns the number of affected rows
  120. */
  121. public function delete($type, $ownerId)
  122. {
  123. $adapter = $this->_table->getAdapter();
  124. $where = $adapter->quoteInto('type = ? AND ', $type) .
  125. $adapter->quoteInto('owner_id = ?', $ownerId);
  126. return $this->_table->delete($where);
  127. }
  128. }