123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- /**
- *
- * PHP Pro Bid $Id$ R2tB54vMcPc078VSLaJmKSpbruf7KR9VezjJSRGM8BY=
- *
- * @link http://www.phpprobid.com
- * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
- * @license http://www.phpprobid.com/license Commercial License
- *
- * @version 7.7
- */
- /**
- * custom fields table service class
- *
- * IMPORTANT:
- * search custom fields by multiple categories:
- * select * from custom_fields where category_ids REGEXP '"x"|"y"|"z"';
- */
- namespace Ppb\Service;
- use Ppb\Db\Table;
- class CustomFields extends AbstractService
- {
- /**
- *
- * allowed custom field types
- *
- * @var array
- */
- protected $_customFieldTypes = array(
- 'user', 'item');
- /**
- *
- * class constructor
- */
- public function __construct()
- {
- parent::__construct();
- $this->setTable(
- new Table\CustomFields());
- }
- /**
- *
- * get allowed custom field types
- *
- * @return array
- */
- public function getCustomFieldTypes()
- {
- return $this->_customFieldTypes;
- }
- /**
- *
- * save custom field object in the custom fields table
- *
- * @param array $data
- * @return \Ppb\Service\CustomFields
- */
- public function save($data)
- {
- $row = null;
- $data = $this->_prepareSaveData($data);
- if (array_key_exists('id', $data)) {
- $select = $this->_table->select()
- ->where("id = ?", $data['id']);
- unset($data['id']);
- $row = $this->_table->fetchRow($select);
- }
- if (count($row) > 0) {
- $this->_table->update($data, "id='{$row['id']}'");
- }
- else {
- $this->_table->insert($data);
- }
- return $this;
- }
- /**
- *
- * delete a custom field from the table
- *
- * @param integer $id the id of the custom field
- * @return integer returns the number of affected rows
- */
- public function delete($id)
- {
- $where = $this->_table->getAdapter()->quoteInto('id = ?', $id);
- return $this->_table->delete($where);
- }
- /**
- *
- * get certain custom fields based on a set of queries
- *
- * @param array $data the search data used to return the requested fields
- * @param mixed $order order by field(s)
- * @return \Cube\Db\Table\Rowset\AbstractRowset
- */
- public function getFields(array $data = null, $order = null)
- {
- $select = $this->_table->select();
- foreach ((array)$data as $key => $value) {
- if ($key === 'category_ids') {
- $select->where("category_ids REGEXP '\"" . implode('"|"',
- array_unique($value)) . "\"' OR category_ids = ''");
- }
- else {
- $select->where("{$key} = ?", $value);
- }
- }
- if ($order === null) {
- $order = array('active DESC', 'order_id ASC');
- }
- $select->order($order);
- return $this->fetchAll($select);
- }
- /**
- *
- * save custom fields settings (order etc)
- *
- * @param array $data
- * @return $this
- * @throws \InvalidArgumentException
- */
- public function saveBrowseSettings(array $data)
- {
- if (!isset($data['id'])) {
- throw new \InvalidArgumentException("The form must use an element with the name 'id'.");
- }
- $columns = array_keys($data);
- foreach ((array)$data['id'] as $key => $value) {
- $row = $this->_table->fetchRow("id='{$value}'");
- $input = array();
- foreach ($columns as $column) {
- if (is_array($data[$column]) && isset($data[$column][$key])) {
- $input[$column] = $data[$column][$key];
- }
- }
- $input = parent::_prepareSaveData($input);
- if (count($row) > 0) {
- $this->_table->update($input, "id='{$value}'");
- }
- }
- return $this;
- }
- }
|