public function AddTopic(ModuleTopic_EntityTopic $oTopic) { $sql = "INSERT INTO ".Config::Get('db.table.topic')." (blog_id, user_id, topic_type, topic_title, topic_tags, topic_date_add, topic_user_ip, topic_publish, topic_publish_draft, topic_publish_index, topic_cut_text, topic_forbid_comment, topic_text_hash ) VALUES(?d, ?d, ?, ?, ?, ?, ?, ?d, ?d, ?d, ?, ?, ?) "; if ($iId=$this->oDb->query($sql,$oTopic->getBlogId(),$oTopic->getUserId(),$oTopic->getType(),$oTopic->getTitle(), $oTopic->getTags(),$oTopic->getDateAdd(),$oTopic->getUserIp(),$oTopic->getPublish(),$oTopic->getPublishDraft(),$oTopic->getPublishIndex(),$oTopic->getCutText(),$oTopic->getForbidComment(),$oTopic->getTextHash())) { $oTopic->setId($iId); $this->AddTopicContent($oTopic); return $iId; } return false; }
/** * Добавляет топик * * @param ModuleTopic_EntityTopic $oTopic Объект топика * @return int|bool */ public function AddTopic(ModuleTopic_EntityTopic $oTopic) { $sql = "INSERT INTO " . Config::Get('db.table.topic') . "\n\t\t\t(blog_id,\n\t\t\tblog_id2,\n\t\t\tblog_id3,\n\t\t\tblog_id4,\n\t\t\tblog_id5,\n\t\t\tuser_id,\n\t\t\ttopic_type,\n\t\t\ttopic_title,\t\t\t\n\t\t\ttopic_slug,\n\t\t\ttopic_tags,\n\t\t\ttopic_date_add,\n\t\t\ttopic_date_publish,\n\t\t\ttopic_user_ip,\n\t\t\ttopic_publish,\n\t\t\ttopic_publish_draft,\n\t\t\ttopic_publish_index,\n\t\t\ttopic_skip_index,\n\t\t\ttopic_cut_text,\n\t\t\ttopic_forbid_comment,\t\t\t\n\t\t\ttopic_text_hash\t\t\t\n\t\t\t)\n\t\t\tVALUES(?d, ?d, ?d, ?d, ?d, ?d,\t?,\t?,\t?,\t?, ?, ?, ?, ?d, ?d, ?d, ?d, ?, ?, ?)\n\t\t"; if ($iId = $this->oDb->query($sql, $oTopic->getBlogId(), $oTopic->getBlogId2(), $oTopic->getBlogId3(), $oTopic->getBlogId4(), $oTopic->getBlogId5(), $oTopic->getUserId(), $oTopic->getType(), $oTopic->getTitle(), $oTopic->getSlug(), $oTopic->getTags(), $oTopic->getDateAdd(), $oTopic->getDatePublish(), $oTopic->getUserIp(), $oTopic->getPublish(), $oTopic->getPublishDraft(), $oTopic->getPublishIndex(), $oTopic->getSkipIndex(), $oTopic->getCutText(), $oTopic->getForbidComment(), $oTopic->getTextHash())) { $oTopic->setId($iId); $this->AddTopicContent($oTopic); return $iId; } return false; }
/** * Добавляет топик * * @param ModuleTopic_EntityTopic $oTopic Объект топика * * @return int|bool */ public function AddTopic(ModuleTopic_EntityTopic $oTopic) { $sql = "INSERT INTO ?_topic (\n blog_id,\n user_id,\n topic_type,\n topic_title,\n topic_tags,\n topic_date_add,\n topic_date_show,\n topic_user_ip,\n topic_publish,\n topic_publish_draft,\n topic_publish_index,\n topic_cut_text,\n topic_forbid_comment,\n topic_text_hash,\n topic_url,\n topic_index_ignore\n\t\t\t)\n\t\t\tVALUES (\n ?d:blog_id,\n ?d:user_id,\n ?:topic_type,\n ?:topic_title,\n ?:topic_tags,\n ?:topic_date_add,\n ?:topic_date_show,\n ?:topic_user_ip,\n ?d:topic_publish,\n ?d:topic_publish_draft,\n ?d:topic_publish_index,\n ?:topic_cut_text,\n ?d:topic_forbid_comment,\n ?:topic_text_hash,\n ?:topic_url,\n ?d:topic_index_ignore\n\t\t\t)\n\t\t"; $nId = $this->oDb->sqlQuery($sql, array(':blog_id' => $oTopic->getBlogId(), ':user_id' => $oTopic->getUserId(), ':topic_type' => $oTopic->getType(), ':topic_title' => $oTopic->getTitle(), ':topic_tags' => $oTopic->getTags(), ':topic_date_add' => $oTopic->getDateAdd(), ':topic_date_show' => $oTopic->getDateShow(), ':topic_user_ip' => $oTopic->getUserIp(), ':topic_publish' => $oTopic->getPublish() ? 1 : 0, ':topic_publish_draft' => $oTopic->getPublishDraft() ? 1 : 0, ':topic_publish_index' => $oTopic->getPublishIndex() ? 1 : 0, ':topic_cut_text' => $oTopic->getCutText(), ':topic_forbid_comment' => $oTopic->getForbidComment(), ':topic_text_hash' => $oTopic->getTextHash(), ':topic_url' => $oTopic->getTopicUrl(), ':topic_index_ignore' => $oTopic->getTopicIndexIgnore())); if ($nId) { $oTopic->setId($nId); $this->AddTopicContent($oTopic); return $nId; } return false; }
/** * Обработка дополнительных полей топика * * @param ModuleTopic_EntityTopic $oTopic * @param string $sType * * @return bool */ public function processTopicFields($oTopic, $sType = 'add') { /** @var ModuleTopic_EntityContentValues $aValues */ $aValues = array(); if ($sType == 'update') { // * Получаем существующие значения if ($aData = $this->GetTopicValuesByArrayId(array($oTopic->getId()))) { $aValues = $aData[$oTopic->getId()]; } // * Чистим существующие значения E::ModuleTopic()->DeleteTopicValuesByTopicId($oTopic->getId()); } if ($oType = E::ModuleTopic()->GetContentTypeByUrl($oTopic->getType())) { //получаем поля для данного типа if ($aFields = $oType->getFields()) { foreach ($aFields as $oField) { $sData = null; if (isset($_REQUEST['fields'][$oField->getFieldId()]) || isset($_FILES['fields_' . $oField->getFieldId()]) || $oField->getFieldType() == 'single-image-uploader') { //текстовые поля if (in_array($oField->getFieldType(), array('input', 'textarea', 'select'))) { $sData = E::ModuleText()->Parser($_REQUEST['fields'][$oField->getFieldId()]); } //поле ссылки if ($oField->getFieldType() == 'link') { $sData = $_REQUEST['fields'][$oField->getFieldId()]; } //поле даты if ($oField->getFieldType() == 'date') { if (isset($_REQUEST['fields'][$oField->getFieldId()])) { if (F::CheckVal($_REQUEST['fields'][$oField->getFieldId()], 'text', 6, 10) && substr_count($_REQUEST['fields'][$oField->getFieldId()], '.') == 2) { list($d, $m, $y) = explode('.', $_REQUEST['fields'][$oField->getFieldId()]); if (@checkdate($m, $d, $y)) { $sData = $_REQUEST['fields'][$oField->getFieldId()]; } } } } //поле с файлом if ($oField->getFieldType() == 'file') { //если указано удаление файла if (F::GetRequest('topic_delete_file_' . $oField->getFieldId())) { if ($oTopic->getFieldFile($oField->getFieldId())) { @unlink(Config::Get('path.root.dir') . $oTopic->getFieldFile($oField->getFieldId())->getFileUrl()); //$oTopic->setValueField($oField->getFieldId(),''); $sData = null; } } else { //если удаление файла не указано, уже ранее залит файл^ и нового файла не загружалось if ($sType == 'update' && isset($aValues[$oField->getFieldId()])) { $sData = $aValues[$oField->getFieldId()]->getValueSource(); } } if (isset($_FILES['fields_' . $oField->getFieldId()]) && is_uploaded_file($_FILES['fields_' . $oField->getFieldId()]['tmp_name'])) { $iMaxFileSize = F::MemSize2Int(Config::Get('module.uploader.files.default.file_maxsize')); $aFileExtensions = Config::Get('module.uploader.files.default.file_extensions'); if (!$iMaxFileSize || filesize($_FILES['fields_' . $oField->getFieldId()]['tmp_name']) <= $iMaxFileSize) { $aPathInfo = pathinfo($_FILES['fields_' . $oField->getFieldId()]['name']); if (!$aFileExtensions || in_array(strtolower($aPathInfo['extension']), $aFileExtensions)) { $sFileTmp = $_FILES['fields_' . $oField->getFieldId()]['tmp_name']; $sDirSave = Config::Get('path.uploads.root') . '/files/' . E::ModuleUser()->GetUserCurrent()->getId() . '/' . F::RandomStr(16); mkdir(Config::Get('path.root.dir') . $sDirSave, 0777, true); if (is_dir(Config::Get('path.root.dir') . $sDirSave)) { $sFile = $sDirSave . '/' . F::RandomStr(10) . '.' . strtolower($aPathInfo['extension']); $sFileFullPath = Config::Get('path.root.dir') . $sFile; if (copy($sFileTmp, $sFileFullPath)) { //удаляем старый файл if ($oTopic->getFieldFile($oField->getFieldId())) { $sOldFile = Config::Get('path.root.dir') . $oTopic->getFieldFile($oField->getFieldId())->getFileUrl(); F::File_Delete($sOldFile); } $aFileObj = array(); $aFileObj['file_hash'] = F::RandomStr(32); $aFileObj['file_name'] = E::ModuleText()->Parser($_FILES['fields_' . $oField->getFieldId()]['name']); $aFileObj['file_url'] = $sFile; $aFileObj['file_size'] = $_FILES['fields_' . $oField->getFieldId()]['size']; $aFileObj['file_extension'] = $aPathInfo['extension']; $aFileObj['file_downloads'] = 0; $sData = serialize($aFileObj); F::File_Delete($sFileTmp); } } } else { $sTypes = implode(', ', $aFileExtensions); E::ModuleMessage()->AddError(E::ModuleLang()->Get('topic_field_file_upload_err_type', array('types' => $sTypes)), null, true); } } else { E::ModuleMessage()->AddError(E::ModuleLang()->Get('topic_field_file_upload_err_size', array('size' => $iMaxFileSize)), null, true); } F::File_Delete($_FILES['fields_' . $oField->getFieldId()]['tmp_name']); } } // Поле с изображением if ($oField->getFieldType() == 'single-image-uploader') { $sTargetType = $oField->getFieldType() . '-' . $oField->getFieldId(); $iTargetId = $oTopic->getId(); // 1. Удалить значение target_tmp // Нужно затереть временный ключ в ресурсах, что бы в дальнейшем картнка не // воспринималась как временная. if ($sTargetTmp = E::ModuleSession()->GetCookie(ModuleUploader::COOKIE_TARGET_TMP)) { // 2. Удалить куку. // Если прозошло сохранение вновь созданного топика, то нужно // удалить куку временной картинки. Если же сохранялся уже существующий топик, // то удаление куки ни на что влиять не будет. E::ModuleSession()->DelCookie(ModuleUploader::COOKIE_TARGET_TMP); // 3. Переместить фото $sNewPath = E::ModuleUploader()->GetUserImageDir(E::UserId(), true, false); $aMresourceRel = E::ModuleMresource()->GetMresourcesRelByTargetAndUser($sTargetType, 0, E::UserId()); if ($aMresourceRel) { $oResource = array_shift($aMresourceRel); $sOldPath = $oResource->GetFile(); $oStoredFile = E::ModuleUploader()->Store($sOldPath, $sNewPath); /** @var ModuleMresource_EntityMresource $oResource */ $oResource = E::ModuleMresource()->GetMresourcesByUuid($oStoredFile->getUuid()); if ($oResource) { $oResource->setUrl(E::ModuleMresource()->NormalizeUrl(E::ModuleUploader()->GetTargetUrl($sTargetType, $iTargetId))); $oResource->setType($sTargetType); $oResource->setUserId(E::UserId()); // 4. В свойство поля записать адрес картинки $sData = $oResource->getMresourceId(); $oResource = array($oResource); E::ModuleMresource()->UnlinkFile($sTargetType, 0, $oTopic->getUserId()); E::ModuleMresource()->AddTargetRel($oResource, $sTargetType, $iTargetId); } } } else { // Топик редактируется, просто обновим поле $aMresourceRel = E::ModuleMresource()->GetMresourcesRelByTargetAndUser($sTargetType, $iTargetId, E::UserId()); if ($aMresourceRel) { $oResource = array_shift($aMresourceRel); $sData = $oResource->getMresourceId(); } else { $sData = false; // $this->DeleteField($oField); } } } E::ModuleHook()->Run('content_field_proccess', array('sData' => &$sData, 'oField' => $oField, 'oTopic' => $oTopic, 'aValues' => $aValues, 'sType' => &$sType)); //Добавляем поле к топику. if ($sData) { /** @var ModuleTopic_EntityContentValues $oValue */ $oValue = E::GetEntity('Topic_ContentValues'); $oValue->setTargetId($oTopic->getId()); $oValue->setTargetType('topic'); $oValue->setFieldId($oField->getFieldId()); $oValue->setFieldType($oField->getFieldType()); $oValue->setValue($sData); $oValue->setValueSource(in_array($oField->getFieldType(), array('file', 'single-image-uploader')) ? $sData : $_REQUEST['fields'][$oField->getFieldId()]); $this->AddTopicValue($oValue); } } } } } return true; }
/** * Формирует и возвращает полный ЧПУ URL для топика * * @param ModuleTopic_EntityTopic $oTopic * @param bool $bAbsolute При false вернет относительный УРЛ * @return string */ public function BuildUrlForTopic($oTopic, $bAbsolute = true) { $sUrlMask = Config::Get('module.topic.url'); $iDateCreate = strtotime($oTopic->getDatePublish()); $aReplace = array('%year%' => date("Y", $iDateCreate), '%month%' => date("m", $iDateCreate), '%day%' => date("d", $iDateCreate), '%hour%' => date("H", $iDateCreate), '%minute%' => date("i", $iDateCreate), '%second%' => date("s", $iDateCreate), '%login%' => '', '%blog%' => '', '%id%' => $oTopic->getId(), '%title%' => $oTopic->getSlug(), '%type%' => $oTopic->getType()); /** * Получаем связанные данные только если в этом есть необходимость */ if (strpos($sUrlMask, '%blog%') !== false) { if (!($oBlog = $oTopic->GetBlog())) { $oBlog = $this->Blog_GetBlogById($oTopic->getBlogId()); } if ($oBlog) { if ($oBlog->getType() == 'personal') { $sUrlMask = str_replace('%blog%', '%login%', $sUrlMask); } else { $aReplace['%blog%'] = $oBlog->getUrl(); } } } if (strpos($sUrlMask, '%login%') !== false) { if (!($oUser = $oTopic->GetUser())) { $oUser = $this->User_GetUserById($oTopic->getUserId()); } if ($oUser) { $aReplace['%login%'] = $oUser->getLogin(); } } $sUrl = strtr($sUrlMask, $aReplace); return $bAbsolute ? Router::GetPathRootWeb() . '/' . $sUrl : $sUrl; }