/** * Save the record in the database * If the record is missing, it will be added * If the record is present, it will be changed * * @param bool $useTransactions * @return bool */ public function save($useTransactions = true) { $update = $this->isSaved(); try { if ($useTransactions) { $this->_db->beginTransaction(); } if ($update) { $commit = $this->preUpdate(); } else { $commit = $this->preInsert(); } if (!$commit) { if ($useTransactions) { $this->_db->rollback(); } return false; } $row = array(); foreach ($this->_properties as $k => $v) { if ($update && !$v['updated']) { continue; } switch ($v['type']) { case self::TYPE_TIMESTAMP: if (!is_null($v['value'])) { if ($this->_db instanceof Zend_Db_Adapter_Pdo_Pgsql) { $v['value'] = date('Y-m-d H:i:sO', $v['value']); } else { $v['value'] = date('Y-m-d H:i:s', $v['value']); } } break; case self::TYPE_BOOLEAN: $v['value'] = (int) (bool) $v['value']; break; case self::TYPE_HTML: $filterContent = new Default_Form_Filter_DeleteBasePathUrl(); $content = $filterContent->filter($v['value']); if ($content === FALSE) { return FALSE; } else { $v['value'] = $content; } break; } $row[$k] = $v['value']; } if (count($row) > 0) { // perform insert/update if ($update) { $commit = (bool) $this->_db->update($this->_table, $row, sprintf('%s = %d', $this->_idField, $this->getId())); } else { $this->_db->insert($this->_table, $row); $lastInsertId = $this->_db->lastInsertId($this->_table, $this->_idField); $this->_id = $lastInsertId; $commit = (bool) $lastInsertId; } } // update internal id if ($commit) { if ($update) { $commit = $this->postUpdate(); } else { $commit = $this->postInsert(); } } if ($useTransactions) { if ($commit) { $this->_db->commit(); } else { $this->_db->rollback(); } } return $commit; } catch (Exception $exc) { if ($useTransactions) { $this->_db->rollback(); } // Запомним в логе сообщений if ($update) { $message = "Update row from the table - \"{$this->_table}\" with a key field - \"{$this->_idField}\"=" . $this->getId(); } else { $message = "Insert a row to table - \"{$this->_table}\""; } $message .= "\n\n" . $exc->getMessage(); $this->_logMsg->db_save_err($message); // Запомним в логе ошибок $message .= "\n\n" . $exc->getTraceAsString(); $this->_logEx->err($message); return FALSE; } }
/** * Content filtering information for blog post: * - when saving removes the base path in the URL resources * - when reading add the base path to the URL resources * * @param string $action Maybe two values: 'save' and 'load' * @return bool */ protected function filterContent($action) { if (!$this->profile->content) { return TRUE; } //Сохранение записи if ($action == 'save') { // Определим действие над полем $action = $this->profile->getAction('content'); // Удалим базовый путь из URL ресурсов $filterContent = new Default_Form_Filter_DeleteBasePathUrl(); $content = $filterContent->filter($this->profile->content); if (!$content === FALSE) { $this->profile->content = $content; // Установим старое действие над полем $this->profile->setAction('content', $action); return TRUE; } else { return FALSE; } } //Чтение записи if ($action == 'load') { // Добавим базовый путь к URL ресурсам на странице HTML $filterContent = new Default_Form_Filter_AddBasePathUrl(); $content = $filterContent->filter($this->profile->content); if (!$content === FALSE) { $this->profile->content = $content; // Установим действие по умолчанию для свойства 'content' $this->profile->setAction('content', Default_Model_Profile::ACTION_NONE); return TRUE; } else { return FALSE; } } }
/** * Content filtering information for different languages: * - when saving removes the base path in the URL resources * - when reading add the base path to the URL resources * * @param string $type Возможно 2 значения: 'save' и 'load' * @return bool */ protected function filterContent($type) { $is_content = false; //-------------------------- // Проверим наличие информации $translate = Zend_Registry::get('Zend_Translate'); $list_languages = $translate->getAdapter()->getList(); foreach ($list_languages as $language) { $key_content = 'content_' . $language; if ($this->profile->{$key_content}) { $is_content = TRUE; } } if (!$is_content) { return TRUE; } //Сохранение записи if ($type === 'save') { foreach ($list_languages as $language) { $key_content = 'content_' . $language; if (!$this->profile->{$key_content}) { continue; } // Определим действие над полем $action = $this->profile->getAction($key_content); // Удалим базовый путь из URL ресурсов $filterContent = new Default_Form_Filter_DeleteBasePathUrl(); $content = $filterContent->filter($this->profile->{$key_content}); if (!$content === FALSE) { $this->profile->{$key_content} = $content; // Установим старое действие над полем $this->profile->setAction($key_content, $action); } else { return FALSE; } } } //Чтение записи if ($type === 'load') { foreach ($list_languages as $language) { $key_content = 'content_' . $language; if (!$this->profile->{$key_content}) { continue; } // Добавим базовый путь к URL ресурсам на странице HTML $filterContent = new Default_Form_Filter_AddBasePathUrl(); $content = $filterContent->filter($this->profile->{$key_content}); if (!$content === FALSE) { $this->profile->{$key_content} = $content; // Установим действие по умолчанию для свойства 'content' $this->profile->setAction($key_content, Default_Model_Profile::ACTION_NONE); } else { return FALSE; } } } return TRUE; }