/** * 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; }
/** * List all articles for pulling */ public function pullAction() { // Fetch topic details $topic = $this->params('topic', ''); if (empty($topic)) { return $this->jumpTo404(_a('Invalid topic ID!')); } if (is_numeric($topic)) { $rowTopic = $this->getModel('topic')->find($topic); } else { $rowTopic = $this->getModel('topic')->find($topic, 'slug'); } $where = array(); $page = $this->params('p', 1); $limit = $this->params('limit', 20); $data = $ids = array(); $module = $this->getModule(); $modelArticle = $this->getModel('article'); $categoryModel = $this->getModel('category'); $modelRelation = $this->getModel('article_topic'); // Get topic articles $rowRelation = $modelRelation->select(array('topic' => $rowTopic->id)); $topicArticles = array(); foreach ($rowRelation as $row) { $topicArticles[] = $row['article']; } // Get category $category = $this->params('category', 0); if ($category > 1) { $categoryIds = $categoryModel->getDescendantIds($category); if ($categoryIds) { $where['category'] = $categoryIds; } } // Get topic $model = $this->getModel('topic'); $topics = $model->getList(); // Build where $where['status'] = Article::FIELD_STATUS_PUBLISHED; $where['active'] = 1; $keyword = $this->params('keyword', ''); if (!empty($keyword)) { $where['subject like ?'] = sprintf('%%%s%%', $keyword); } // Retrieve data $data = Entity::getArticlePage($where, $page, $limit); // Getting article topic $articleIds = array_keys($data); if (empty($articleIds)) { $articleIds = array(0); } $rowRelation = $this->getModel('article_topic')->select(array('article' => $articleIds)); $relation = array(); foreach ($rowRelation as $row) { if (isset($relation[$row['article']])) { $relation[$row['article']] .= ',' . $topics[$row['topic']]; } else { $relation[$row['article']] = $topics[$row['topic']]; } } // Total count $select = $modelArticle->select()->columns(array('total' => new Expression('count(id)')))->where($where); $resulsetCount = $modelArticle->selectWith($select); $totalCount = (int) $resulsetCount->current()->total; // Paginator $paginator = Paginator::factory($totalCount, array('limit' => $limit, 'page' => $page, 'url_options' => array('page_param' => 'p', 'params' => array_filter(array('module' => $module, 'controller' => 'topic', 'action' => 'pull', 'topic' => $topic, 'category' => $category, 'keyword' => $keyword))))); // Prepare search form $form = new SimpleSearchForm(); $form->setData($this->params()->fromQuery()); $count = $this->getModel('article_topic')->count(array('topic' => $rowTopic->id)); $this->view()->assign(array('title' => _a('All Articles'), 'data' => $data, 'form' => $form, 'paginator' => $paginator, 'category' => $category, 'categories' => Pi::api('api', $module)->getCategoryList(), 'action' => 'pull', 'topics' => $topics, 'relation' => $relation, 'topic' => $rowTopic->toArray(), 'pulled' => $topicArticles, 'count' => $count)); }
/** * Edit a draft * * @return ViewModel */ public function editAction() { // Denied user viewing if no front-end management permission assigned if (!$this->config('enable_front_edit') && 'front' == $this->section) { return $this->jumpTo404(); } $id = $this->params('id', 0); $module = $this->getModule(); $options = Setup::getFormConfig(); $elements = $options['elements']; if (!$id) { return; } $draftModel = $this->getModel('draft'); $row = $draftModel->findRow($id, 'id', false); // Generate user permissions $status = ''; switch ((int) $row->status) { case DraftModel::FIELD_STATUS_DRAFT: $status = 'draft'; break; case DraftModel::FIELD_STATUS_PENDING: $status = 'pending'; break; case DraftModel::FIELD_STATUS_REJECTED: $status = 'rejected'; break; } if ($row->article) { $status = 'publish'; } $isMine = $row->uid == Pi::user()->getId(); $rules = Rule::getPermission($isMine); if (!(isset($rules[$row->category][$status . '-edit']) and $rules[$row->category][$status . '-edit'])) { return $this->jumpToDenied(); } $categories = array(); $approve = array(); $delete = array(); foreach ($rules as $key => $rule) { if (isset($rule[$status . '-edit']) and $rule[$status . '-edit']) { $categories[$key] = true; } // Getting approving and deleting permission for draft article if (isset($rule['approve']) and $rule['approve']) { $approve[] = $key; } if (isset($rule['approve-delete']) and $rule['approve-delete']) { $delete[] = $key; } } $currentDelete = (isset($rules[$row->category][$status . '-delete']) and $rules[$row->category][$status . '-delete']) ? true : false; $currentApprove = (isset($rules[$row->category]['approve']) and $rules[$row->category]['approve']) ? true : false; if (empty($row)) { return; } // prepare data $data = (array) $row; $data['category'] = $data['category'] ?: $this->config('default_category'); $data['related'] = $data['related'] ? implode(self::TAG_DELIMITER, $data['related']) : ''; $data['time_publish'] = $data['time_publish'] ? _date($data['time_publish']) : ''; $data['time_update'] = $data['time_update'] ? _date($data['time_update']) : ''; $featureImage = $data['image'] ? Pi::url($data['image']) : ''; $featureThumb = $data['image'] ? Pi::url(Media::getThumbFromOriginal($data['image'])) : ''; $form = $this->getDraftForm('edit', $options); $allCategory = $form->get('category')->getValueOptions(); $form->get('category')->setValueOptions(array_intersect_key($allCategory, $categories)); $form->setData($data); // Get author info if (in_array('author', $elements) and $data['author']) { $author = $this->getModel('author')->find($data['author']); if ($author) { $this->view()->assign('author', array('id' => $author->id, 'name' => $author->name)); } } // Get submitter info $columns = array('id', 'name'); if ($data['uid']) { $user = Pi::user()->get($data['uid'], $columns); if ($user) { $this->view()->assign('user', array('id' => $user['id'], 'name' => $user['name'])); } } // Get update user info if ($data['user_update']) { $userUpdate = Pi::user()->get($data['user_update'], $columns); if ($userUpdate) { $this->view()->assign('userUpdate', array('id' => $userUpdate['id'], 'name' => $userUpdate['name'])); } } // Get related articles if (in_array('related', $elements)) { $related = $relatedIds = array(); if (!empty($row->related)) { $relatedIds = array_flip($row->related); $related = Entity::getArticlePage(array('id' => $row->related), 1); foreach ($related as $item) { if (array_key_exists($item['id'], $relatedIds)) { $relatedIds[$item['id']] = $item; } } $related = array_filter($relatedIds, function ($var) { return is_array($var); }); } } // Get assets $attachments = $images = array(); $resultsetDraftAsset = $this->getModel('asset_draft')->select(array('draft' => $id))->toArray(); // Getting media ID $mediaIds = array(0); foreach ($resultsetDraftAsset as $asset) { $mediaIds[] = $asset['media']; } // Getting media details $resultsetMedia = $this->getModel('media')->select(array('id' => $mediaIds)); $medias = array(); foreach ($resultsetMedia as $media) { $medias[$media->id] = $media->toArray(); } // Getting assets foreach ($resultsetDraftAsset as $asset) { $media = $medias[$asset['media']]; if ('attachment' == $asset['type']) { $attachments[] = array('id' => $asset['id'], 'title' => $media['title'], 'size' => $media['size'], 'deleteUrl' => $this->url('', array('controller' => 'draft', 'action' => 'remove-asset', 'id' => $asset['id'])), 'downloadUrl' => $this->url('admin', array('controller' => 'media', 'action' => 'download', 'id' => $media['id']))); } else { $imageSize = getimagesize(Pi::path($media['url'])); $images[] = array('id' => $asset['id'], 'title' => $media['title'], 'size' => $media['size'], 'w' => $imageSize['0'], 'h' => $imageSize['1'], 'downloadUrl' => $this->url('', array('controller' => 'media', 'action' => 'download', 'id' => $media['id'])), 'preview_url' => Pi::url($media['url']), 'thumb_url' => Pi::url(Media::getThumbFromOriginal($media['url']))); } } $this->setModuleConfig(); $this->view()->assign(array('title' => __('Edit Article'), 'form' => $form, 'draft' => (array) $row, 'related' => $related, 'attachments' => $attachments, 'images' => $images, 'featureImage' => $featureImage, 'featureThumb' => $featureThumb, 'config' => Pi::config('', $module), 'from' => $this->params('from', ''), 'elements' => $elements, 'status' => $row->article ? Article::FIELD_STATUS_PUBLISHED : $row->status, 'rules' => $rules, 'approve' => $approve, 'delete' => $delete, 'currentDelete' => $currentDelete, 'currentApprove' => $currentApprove)); $this->view()->setTemplate('draft-edit', $module, 'front'); }
/** * Get article by title via AJAX. * * @return ViewModel */ public function getFuzzyArticleAction() { Pi::service('log')->mute(); $articles = array(); $pageCount = $total = 0; $module = $this->getModule(); $where = array('status' => Article::FIELD_STATUS_PUBLISHED); $keyword = $this->params('keyword', ''); $type = $this->params('type', 'title'); $limit = $this->params('limit', 10); $limit = $limit > 100 ? 100 : $limit; $page = $this->params('page', 1); $exclude = $this->params('exclude', 0); $offset = $limit * ($page - 1); $articleModel = $this->getModel('article'); if (strcasecmp('tag', $type) == 0) { if ($keyword) { $total = Pi::service('tag')->getCount($keyword, $module); $pageCount = ceil($total / $limit); // Get article ids $articleIds = Pi::service('tag')->getList($keyword, $module, '', $limit, $offset); if ($articleIds) { $where['id'] = $articleIds; $articles = array_flip($articleIds); // Get articles $resultsetArticle = Entity::getArticlePage($where, 1, $limit, null, null, $module); foreach ($resultsetArticle as $key => $val) { $articles[$key] = $val; } $articles = array_filter($articles, function ($var) { return is_array($var); }); } } } else { // Get resultset if ($keyword) { $where['subject like ?'] = sprintf('%%%s%%', $keyword); } $articles = Entity::getArticlePage($where, $page, $limit); // Get total $total = $articleModel->getSearchRowsCount($where); $pageCount = ceil($total / $limit); } foreach ($articles as $key => &$article) { if ($exclude && $exclude == $key) { unset($articles[$key]); } $article['time_publish_text'] = date('Y-m-d', $article['time_publish']); } echo json_encode(array('status' => true, 'message' => __('OK'), 'data' => array_values($articles), 'paginator' => array('currentPage' => $page, 'pageCount' => $pageCount, 'keyword' => $keyword, 'type' => $type, 'limit' => $limit, 'totalCount' => $total))); exit; }