/** * Create new version * @property Db_Object $object * @return boolean */ public function newVersion(Db_Object $object) { $object->commitChanges(); $newVersion = $this->getLastVersion($object->getName(), $object->getId()) + 1; $newData = $object->getData(); if ($object->getConfig()->hasEncrypted()) { $ivField = $object->getConfig()->getIvField(); $ivKey = $object->get($ivField); if (empty($ivKey)) { $ivKey = Utils_String::createEncryptIv(); $newData[$ivField] = $ivKey; } $newData = $this->getStore()->encryptData($object, $newData); } $newData['id'] = $object->getId(); try { $vObject = new Db_Object('vc'); $vObject->set('date', date('Y-m-d')); $vObject->set('data', base64_encode(serialize($newData))); $vObject->set('user_id', User::getInstance()->id); $vObject->set('version', $newVersion); $vObject->set('record_id', $object->getId()); $vObject->set('object_name', $object->getName()); $vObject->set('date', date('Y-m-d H:i:s')); if ($vObject->save()) { return $newVersion; } return false; } catch (Exception $e) { $this->logError('Cannot create new version for ' . $object->getName() . '::' . $object->getId() . ' ' . $e->getMessage()); return false; } }
/** * Prepear data for linked field component * @param array $data * @return array */ protected function _collectBlockLinksData(array $data) { $ids = Utils::fetchCol('id', $data); $data = Utils::rekey('id', $data); $obj = new Db_Object('Blocks'); $model = Model::factory('Blocks'); $fields = array('id', 'title' => $obj->getConfig()->getLinkTitle(), 'is_system'); $usedRC = $obj->getConfig()->isRevControl(); if ($usedRC) { $fields[] = 'published'; } $odata = $model->getItems($ids, $fields); if (!empty($data)) { $odata = Utils::rekey('id', $odata); } /* * Find out deleted records */ $deleted = array_diff($ids, array_keys($odata)); $result = array(); foreach ($ids as $id) { if (in_array($id, $deleted)) { $title = ''; if (isset($data[$id]['title'])) { $title = $data[$id]['title']; } $item = array('id' => $id, 'deleted' => 1, 'title' => $title, 'published' => 1, 'is_system' => 0); if ($usedRC) { $item['published'] = 0; } } else { $item = array('id' => $id, 'deleted' => 0, 'title' => $odata[$id]['title'], 'published' => 1, 'is_system' => $odata[$id]['is_system']); if ($usedRC) { $item['published'] = $odata[$id]['published']; } } $result[] = $item; } return $result; }
/** * Get ORM object data * Sends a JSON reply in the result and * closes the application */ public function loaddataAction() { $id = Request::post('id', 'integer', false); if (!$id) { Response::jsonSuccess(array()); } try { $obj = new Db_Object($this->_objectName, $id); } catch (Exception $e) { Response::jsonError($this->_lang->CANT_EXEC); } $data = $obj->getData(); /* * Prepare mltilink properties */ $linkedObjects = $obj->getConfig()->getLinks(array('multy')); foreach ($linkedObjects as $linkObject => $fieldCfg) { foreach ($fieldCfg as $field => $linkCfg) { if (empty($data[$field])) { continue; } $data[$field] = array_values($this->_collectLinksData($data[$field], $linkObject)); } } $data['id'] = $obj->getId(); /* * Send response */ Response::jsonSuccess($data); }
/** * Delete Db object * @param Db_Object $object * @param boolean $log - optional, log changes * @param boolean $transaction - optional , use transaction if available * @return boolean */ public function delete(Db_Object $object, $log = true, $transaction = true) { if ($object->getConfig()->isReadOnly()) { if ($this->_log) { $this->_log->log('ORM :: cannot delete readonly object ' . $object->getName()); } return false; } if (!$object->getId()) { return false; } if ($this->_eventManager) { $this->_eventManager->fireEvent(Db_Object_Event_Manager::BEFORE_DELETE, $object); } $transact = $object->getConfig()->isTransact(); $db = $this->_getDbConnection($object); if ($transact && $transaction) { $db->beginTransaction(); } Model::factory($this->_linksObject)->clearObjectLinks($object); if ($db->delete($object->getTable(), $db->quoteIdentifier($object->getConfig()->getPrimaryKey()) . ' =' . $object->getId())) { /** * @todo убрать жесткую связанность */ if ($log && $object->getConfig()->hasHistory()) { Model::factory($this->_historyObject)->log(User::getInstance()->id, $object->getId(), Model_Historylog::Delete, $object->getTable()); } $success = true; } else { $success = false; } if ($transact && $transaction) { if ($success) { $db->commit(); } else { $db->rollBack(); } } if ($this->_eventManager) { $this->_eventManager->fireEvent(Db_Object_Event_Manager::AFTER_DELETE, $object); } return $success; }