/** * Update published article data * * @param int $id Article ID * @return array */ protected function update($id) { $result = array('status' => self::RESULT_FALSE, 'message' => array(), 'data' => array()); if (!$id) { $result['message'] = __('Not enough parameter.'); } $modelDraft = $this->getModel('draft'); $rowDraft = $modelDraft->findRow($id, 'id', false); if (!$rowDraft->id or !$rowDraft->article) { $result['message'] = __('Invalid draft.'); } $module = $this->getModule(); $modelArticle = $this->getModel('article'); // Update draft to article $articleId = $rowDraft->article; $timestamp = time(); $article = array('subject' => $rowDraft->subject, 'subtitle' => $rowDraft->subtitle, 'summary' => $rowDraft->summary, 'content' => $rowDraft->content, 'uid' => $rowDraft->uid, 'author' => $rowDraft->author, 'source' => $rowDraft->source, 'pages' => $rowDraft->pages, 'time_submit' => $rowDraft->time_submit, 'time_publish' => $rowDraft->time_publish, 'time_update' => $rowDraft->time_update > $timestamp ? $rowDraft->time_update : $timestamp, 'user_update' => Pi::user()->getId()); $rowArticle = $modelArticle->find($articleId); $rowArticle->assign($article); $rowArticle->save(); // Compiled article content $modelCompiled = $this->getModel('compiled'); $compiledType = $this->config('compiled_type') ?: 'html'; $compiledContent = Compiled::compiled($rowArticle->markup, $rowArticle->content, $compiledType); $name = $articleId . '-' . $compiledType; $compiled = array('name' => $name, 'article' => $articleId, 'type' => $compiledType, 'content' => $compiledContent); $rowCompiled = $modelCompiled->find($name, 'name'); if ($rowCompiled->id) { $rowCompiled->assign($compiled); $rowCompiled->save(); } else { $rowCompiled = $modelCompiled->createRow($compiled); $rowCompiled->save(); } // Update value of extended fields $extended = array(); $modelExtended = $this->getModel('extended'); $columns = $modelExtended->getValidColumns(); foreach ($columns as $column) { $extended[$column] = $rowDraft->{$column}; } $extended['article'] = $articleId; $rowExtended = $modelExtended->find($articleId, 'article'); if (!$rowExtended) { $rowExtended = $modelExtended->createRow($extended); } else { $rowExtended->assign($extended); } $rowExtended->save(); // Move feature image if (strcmp($rowDraft->image, $rowArticle->image) != 0) { if ($rowArticle->image) { $image = Pi::path($rowArticle->image); unlink($image); unlink(Media::getThumbFromOriginal($image)); } $rowArticle->image = $rowDraft->image; $rowArticle->save(); } // Update assets $modelAsset = $this->getModel('asset'); $modelAsset->delete(array('article' => $articleId)); $modelDraftAsset = $this->getModel('asset_draft'); $resultsetDraftAsset = $modelDraftAsset->select(array('draft' => $id)); foreach ($resultsetDraftAsset as $asset) { $data = array('media' => $asset->media, 'type' => $asset->type, 'article' => $articleId); $rowAsset = $modelAsset->createRow($data); $rowAsset->save(); } // Clear draft assets $modelDraftAsset->delete(array('draft' => $id)); // Save tag if ($this->config('enable_tag')) { Pi::service('tag')->update($module, $articleId, '', $rowDraft->tag); } // Save related articles $relatedArticles = $rowDraft->related; if ($relatedArticles) { $modelRelated = $this->getModel('related'); $modelRelated->saveRelated($articleId, $relatedArticles); } // Delete draft $modelDraft->delete(array('id' => $rowDraft->id)); $result['status'] = self::RESULT_TRUE; $result['data']['redirect'] = $this->url('', array('action' => 'published', 'controller' => 'article')); $result['message'] = __('Article update successfully.'); return $result; }
/** * Get draft article details. * * @param int $id Draft article ID * @return array */ public static function getDraft($id) { $result = array(); $module = Pi::service('module')->current(); $config = Pi::config('', $module); $row = Pi::model('draft', $module)->findRow($id, 'id', false); if (empty($row->id)) { return array(); } $subject = $subtitle = $content = ''; if ($row->markup) { $subject = Pi::service('markup')->render($row->subject, 'html', $row->markup); $subtitle = Pi::service('markup')->render($row->subtitle, 'html', $row->markup); } else { $subject = Pi::service('markup')->render($row->subject, 'html'); $subtitle = Pi::service('markup')->render($row->subtitle, 'html'); } $content = Compiled::compiled($row->markup, $row->content, 'html'); $result = array('title' => $subject, 'content' => self::breakPage($content), 'slug' => $row->slug, 'seo' => array('title' => $row->seo_title, 'keywords' => $row->seo_keywords, 'description' => $row->seo_description), 'subtitle' => $subtitle, 'source' => $row->source, 'pages' => $row->pages, 'time_publish' => $row->time_publish, 'author' => array(), 'attachment' => array(), 'tag' => $row->tag, 'related' => array(), 'category' => $row->category); // Get author if ($row->author) { $author = Pi::model('author', $module)->find($row->author); if ($author) { $result['author'] = $author->toArray(); if (empty($result['author']['photo'])) { $result['author']['photo'] = Pi::service('asset')->getModuleAsset($config['default_author_photo'], $module); } } } // Get attachments $resultsetDraftAsset = Pi::model('asset_draft', $module)->select(array('draft' => $id, 'type' => 'attachment')); $mediaIds = array(0); foreach ($resultsetDraftAsset as $asset) { $mediaIds[$asset->media] = $asset->media; } $modelMedia = Pi::model('media', $module); $rowMedia = $modelMedia->select(array('id' => $mediaIds)); foreach ($rowMedia as $media) { $result['attachment'][] = array('original_name' => $media->title, 'extension' => $media->type, 'size' => $media->size, 'url' => Pi::service('url')->assemble('admin', array('module' => $module, 'controller' => 'media', 'action' => 'download', 'name' => $media->id))); } // Get related articles $relatedIds = $related = array(); $relatedIds = $row->related; if ($relatedIds) { $related = array_flip($relatedIds); $where = array('id' => $relatedIds); $columns = array('id', 'subject'); $resultsetRelated = Entity::getArticlePage($where, 1, null, $columns, null, $module); foreach ($resultsetRelated as $key => $val) { if (array_key_exists($key, $related)) { $related[$key] = $val; } } $result['related'] = array_filter($related, function ($var) { return is_array($var); }); } if (empty($row->seo_keywords) && $config['seo_keywords']) { if ($config['seo_keywords'] == Article::FIELD_SEO_KEYWORDS_TAG) { $result['seo']['keywords'] = implode(' ', $result['tag']); } else { if ($config['seo_keywords'] == Article::FIELD_SEO_KEYWORDS_CATEGORY) { $rowCategory = Pi::model('category', $module)->find($row->category); $result['seo']['keywords'] = $rowCategory->title; } } } if (empty($row->seo_description) && $config['seo_description']) { if ($config['seo_description'] == Article::FIELD_SEO_DESCRIPTION_SUMMARY) { $result['seo']['description'] = $row->summary; } } return $result; }
/** * Get published article details * * @param int $id Article ID * @return array */ public static function getEntity($id) { $module = Pi::service('module')->current(); $config = Pi::config('', $module); $row = Pi::model('article', $module)->find($id); if (empty($row->id)) { return array(); } if ($row->markup) { $subject = Pi::service('markup')->render($row->subject, 'html', $row->markup); $subtitle = Pi::service('markup')->render($row->subtitle, 'html', $row->markup); } else { $subject = Pi::service('markup')->render($row->subject, 'html'); $subtitle = Pi::service('markup')->render($row->subtitle, 'html'); } $content = Compiled::getContent($row->id, 'html'); $result = array('title' => $subject, 'content' => Draft::breakPage($content), 'subtitle' => $subtitle, 'source' => $row->source, 'pages' => $row->pages, 'time_publish' => $row->time_publish, 'active' => $row->active, 'visits' => '', 'slug' => '', 'seo' => array(), 'author' => array(), 'attachment' => array(), 'tag' => '', 'related' => array()); // Get author if ($row->author) { $author = Pi::api('api', $module)->getAuthorList((array) $row->author); if ($author) { $result['author'] = array_shift($author); if (empty($result['author']['photo'])) { $result['author']['photo'] = Pi::service('asset')->getModuleAsset($config['default_author_photo'], $module); } } } // Get attachments $resultsetAsset = Pi::model('asset', $module)->select(array('article' => $id, 'type' => 'attachment')); $mediaIds = array(); foreach ($resultsetAsset as $asset) { $mediaIds[$asset->media] = $asset->media; } if ($mediaIds) { $resultsetMedia = Pi::model('media', $module)->select(array('id' => $mediaIds)); foreach ($resultsetMedia as $media) { $result['attachment'][] = array('original_name' => $media->title, 'extension' => $media->type, 'size' => $media->size, 'url' => Pi::service('url')->assemble('default', array('module' => $module, 'controller' => 'media', 'action' => 'download', 'id' => $media->id))); } } // Get tag /* if ($config['enable_tag']) { $result['tag'] = Pi::service('tag')->get($module, $id); } */ // Get related articles $relatedIds = Pi::model('related', $module)->getRelated($id); if ($relatedIds) { $related = array_flip($relatedIds); $where = array('id' => $relatedIds); $columns = array('id', 'subject', 'time_publish'); $resultsetRelated = self::getArticlePage($where, 1, null, $columns, null, $module); foreach ($resultsetRelated as $key => $val) { if (array_key_exists($key, $related)) { $related[$key] = $val; } } $result['related'] = array_filter($related, function ($var) { return is_array($var); }); } // Getting seo $modelExtended = Pi::model('extended', $module); $rowExtended = $modelExtended->find($row->id, $module); if ($rowExtended) { $result['slug'] = $rowExtended->slug; $result['seo'] = array('title' => $rowExtended->seo_title, 'keywords' => $rowExtended->seo_keywords, 'description' => $rowExtended->seo_description); } // Getting stats data $modelStatis = Pi::model('stats', $module); $rowStatis = $modelStatis->find($row->id, $module); if ($rowStatis) { $result['visits'] = $rowStatis->visits; } return $result; }