Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Preview a draft article.
  * 
  * @return ViewModel 
  */
 public function previewAction()
 {
     $id = $this->params('id');
     $slug = $this->params('slug', '');
     $page = $this->params('p', 1);
     $remain = $this->params('r', '');
     if ('' !== $remain) {
         $this->view()->assign('remain', $remain);
     }
     $time = time();
     $details = Draft::getDraft($id);
     $details['time_publish'] = $time;
     $params = array('preview' => 1);
     if (!$id) {
         return $this->jumpTo404(__('Page not found'));
     }
     if (strval($slug) != $details['slug']) {
         $routeParams = array('time' => $time, 'id' => $id, 'slug' => $details['slug'], 'p' => $page);
         if ($remain) {
             $params['r'] = $remain;
         }
         return $this->redirect()->setStatusCode(301)->toRoute('', array_merge($routeParams, $params));
     }
     $module = $this->getModule();
     $route = Pi::api('api', $module)->getRouteName();
     foreach ($details['content'] as &$value) {
         $value['url'] = $this->url($route, array_merge(array('time' => date('Ymd', $time), 'id' => $id, 'slug' => $slug, 'p' => $value['page']), $params));
         if (isset($value['title']) and preg_replace('/ /', '', trim($value['title'])) !== '') {
             $showTitle = true;
         } else {
             $value['title'] = '';
         }
     }
     $details['view'] = $this->url($route, array_merge(array('time' => date('Ymd', $time), 'id' => $id, 'slug' => $slug, 'r' => 0), $params));
     $details['remain'] = $this->url($route, array_merge(array('time' => date('Ymd', $time), 'id' => $id, 'slug' => $slug, 'r' => $page), $params));
     $module = $this->getModule();
     $this->view()->assign(array('details' => $details, 'page' => $page, 'showTitle' => isset($showTitle) ? $showTitle : null, 'config' => Pi::config('', $module)));
     $this->view()->setTemplate('article-detail');
 }
Example #3
0
 /**
  * Delete published articles
  * 
  * @return ViewModel 
  */
 public function deleteAction()
 {
     // 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', '');
     $ids = array_filter(explode(',', $id));
     $from = $this->params('from', '');
     if (empty($ids)) {
         return $this->jumpTo404(__('Invalid article ID'));
     }
     $module = $this->getModule();
     $modelArticle = $this->getModel('article');
     $modelAsset = $this->getModel('asset');
     // Delete articles that user has permission to do
     $rules = Rule::getPermission();
     if (1 == count($ids)) {
         $row = $modelArticle->find($ids[0]);
         $slug = Draft::getStatusSlug($row->status);
         $resource = $slug . '-delete';
         if (!(isset($rules[$row->category][$resource]) and $rules[$row->category][$resource])) {
             return $this->jumpToDenied();
         }
     } else {
         $rows = $modelArticle->select(array('id' => $ids));
         $ids = array();
         foreach ($rows as $row) {
             $slug = Draft::getStatusSlug($row->status);
             $resource = $slug . '-delete';
             if (isset($rules[$row->category][$resource]) and $rules[$row->category][$resource]) {
                 $ids[] = $row->id;
             }
         }
     }
     $resultsetArticle = $modelArticle->select(array('id' => $ids));
     // Step operation
     foreach ($resultsetArticle as $article) {
         // Delete feature image
         if ($article->image) {
             @unlink(Pi::path($article->image));
             @unlink(Pi::path(Media::getThumbFromOriginal($article->image)));
         }
     }
     // Batch operation
     // Deleting extended fields
     $this->getModel('extended')->delete(array('article' => $ids));
     // Deleting statistics
     $this->getModel('stats')->delete(array('article' => $ids));
     // Deleting compiled article
     $this->getModel('compiled')->delete(array('article' => $ids));
     // Delete tag
     if ($this->config('enable_tag')) {
         Pi::service('tag')->delete($module, $ids);
     }
     // Delete related articles
     $this->getModel('related')->delete(array('article' => $ids));
     // Delete visits
     $this->getModel('visit')->delete(array('article' => $ids));
     // Delete assets
     $modelAsset->delete(array('article' => $ids));
     // Delete article directly
     $modelArticle->delete(array('id' => $ids));
     // Clear cache
     Pi::service('render')->flushCache($module);
     if ($from) {
         $from = urldecode($from);
         return $this->redirect()->toUrl($from);
     } else {
         // Go to list page
         return $this->redirect()->toRoute('', array('controller' => 'article', 'action' => 'published', 'from' => 'all'));
     }
 }