setTable( new Table\CustomFieldsData()); } /** * * get custom fields table service * * @return \Ppb\Service\CustomFields */ public function getCustomFieldsService() { if (!$this->_customFields instanceof CustomFields) { $this->setCustomFieldsService( new CustomFields()); } return $this->_customFields; } /** * * set custom fields table service * * @param \Ppb\Service\CustomFields $customFields * * @return $this */ public function setCustomFieldsService(CustomFields $customFields) { $this->_customFields = $customFields; return $this; } /** * * save data in the table * * 7.7: data is only saved if the custom field exists - workaround for when using the bulk lister and having * custom fields columns that do not exist * * @param string|array $value custom field value (if array it will be serialized before saving) * @param string $type custom field data type (item, user etc) * @param integer $fieldId custom_fields table id * @param integer $ownerId id of the column for which this data belongs to * * @return $this */ public function save($value, $type, $fieldId, $ownerId) { $customField = $this->getCustomFieldsService()->findBy('id', $fieldId); if (count($customField) > 0) { if (is_array($value)) { $value = array_filter($value); $value = (!empty($value)) ? serialize($value) : ''; } $data = array( 'value' => strval($value), 'field_id' => intval($fieldId), 'owner_id' => intval($ownerId), 'type' => strval($type), ); $select = $this->_table->select() ->where("field_id = ?", $fieldId) ->where("owner_id = ?", $ownerId) ->where("type = ?", $type); $row = $this->_table->fetchRow($select); if (count($row) > 0) { $this->_table->update($data, "id='{$row['id']}'"); } else { $this->_table->insert($data); } } return $this; } /** * * delete data from the table * * @param string $type custom field type * @param integer $ownerId the id of record that the custom field data belongs to * * @return integer returns the number of affected rows */ public function delete($type, $ownerId) { $adapter = $this->_table->getAdapter(); $where = $adapter->quoteInto('type = ? AND ', $type) . $adapter->quoteInto('owner_id = ?', $ownerId); return $this->_table->delete($where); } }