/** * Processing media list */ public function listAction() { $params = $where = array(); $type = $this->params('type', 'image'); $keyword = $this->params('keyword', ''); $style = 'default'; if ('image' == $type) { $style = $this->params('style', 'normal'); $params['style'] = $style; } $where['type'] = $this->getExtension($type); $types = array(); foreach ($where['type'] as $item) { $types[$item] = $item; } $miniType = $this->params('mini_type', ''); if (!empty($miniType)) { $where['type'] = $miniType; $params['mini_type'] = $miniType; } if (!empty($keyword)) { $where['title like ?'] = '%' . $keyword . '%'; $params['keyword'] = $keyword; } $model = $this->getModel('media'); $page = $this->params('p', 1); $page = $page > 0 ? $page : 1; $module = $this->getModule(); $config = Pi::config('', $module); $limit = (int) $config['page_limit_all'] ?: 40; $resultSet = Media::getList($where, $page, $limit, null, null, $module); // Total count $count = $model->count($where); // Pagination $paginator = Paginator::factory($count, array('limit' => $limit, 'page' => $page, 'url_options' => array('page_param' => 'p', 'params' => array_merge(array('module' => $this->getModule(), 'controller' => 'media', 'action' => 'list', 'type' => $type, 'style' => $style), $params)))); // Getting search form $form = new SimpleSearchForm(); $this->view()->assign(array('title' => _a('All Media'), 'medias' => $resultSet, 'paginator' => $paginator, 'type' => $type, 'keyword' => $keyword, 'types' => $types, 'form' => $form, 'count' => $count, 'miniType' => $miniType, 'style' => $style)); }
/** * Delete draft, along with featured image and attachment. * * @param array $ids Draft ID * @param string $module Current module name * @return int Affected rows */ public static function deleteDraft($ids, $module = null) { $module = $module ?: Pi::service('module')->current(); $modelDraft = Pi::model('draft', $module); $modelArticle = Pi::model('article', $module); // Delete feature image $resultsetFeatureImage = $modelDraft->select(array('id' => $ids)); foreach ($resultsetFeatureImage as $featureImage) { if ($featureImage->article) { $rowArticle = $modelArticle->find($featureImage->article); if ($featureImage->image && strcmp($featureImage->image, $rowArticle->image) != 0) { @unlink(Pi::path($featureImage->image)); @unlink(Pi::path(Media::getThumbFromOriginal($featureImage->image))); } } else { if ($featureImage->image) { @unlink(Pi::path($featureImage->image)); @unlink(Pi::path(Media::getThumbFromOriginal($featureImage->image))); } } } // Delete assets $modelDraftAsset = Pi::model('asset_draft', $module); $modelDraftAsset->delete(array('draft' => $ids)); // Delete draft $affectedRows = $modelDraft->delete(array('id' => $ids)); return $affectedRows; }
/** * Remove image * * @return ViewModel */ public function removeImageAction() { Pi::service('log')->mute(); $id = $this->params('id', 0); $fakeId = $this->params('fake_id', 0); $affectedRows = 0; $module = $this->getModule(); if ($id) { $rowDraft = $this->getModel('draft')->find($id); if ($rowDraft && $rowDraft->image) { $thumbUrl = Media::getThumbFromOriginal($rowDraft->image); if ($rowDraft->article) { $modelArticle = $this->getModel('article'); $rowArticle = $modelArticle->find($rowDraft->article); if ($rowArticle && $rowArticle->image != $rowDraft->image) { // Delete file @unlink(Pi::path($rowDraft->image)); @unlink(Pi::path($thumbUrl)); } } else { @unlink(Pi::path($rowDraft->image)); @unlink(Pi::path($thumbUrl)); } // Update db $rowDraft->image = ''; $affectedRows = $rowDraft->save(); } } else { if ($fakeId) { $session = Media::getUploadSession($module, 'feature'); if (isset($session->{$fakeId})) { $uploadInfo = $session->{$fakeId}; $url = Media::getThumbFromOriginal($uploadInfo['tmp_name']); $affectedRows = unlink(Pi::path($uploadInfo['tmp_name'])); @unlink(Pi::path($url)); unset($session->{$id}); unset($session->{$fakeId}); } } } echo json_encode(array('status' => $affectedRows ? true : false, 'message' => 'ok')); exit; }
/** * Save author information * * @param array $data Author information * @return boolean */ protected function saveAuthor($data) { $module = $this->getModule(); $modelAuthor = $this->getModel('author'); $fakeId = $photo = null; if (isset($data['id'])) { $id = $data['id']; unset($data['id']); } $fakeId = $this->params('fake_id', 0); unset($data['photo']); if (empty($id)) { $rowAuthor = $modelAuthor->createRow($data); $rowAuthor->save(); if (empty($rowAuthor->id)) { return false; } $id = $rowAuthor->id; } else { $rowAuthor = $modelAuthor->find($id); if (empty($rowAuthor)) { return false; } $rowAuthor->assign($data); $rowAuthor->save(); } // Save photo $session = Media::getUploadSession($module, 'author'); if (isset($session->{$id}) || $fakeId && isset($session->{$fakeId})) { $uploadInfo = isset($session->{$id}) ? $session->{$id} : $session->{$fakeId}; if ($uploadInfo) { $fileName = $rowAuthor->id; $pathInfo = pathinfo($uploadInfo['tmp_name']); if ($pathInfo['extension']) { $fileName .= '.' . $pathInfo['extension']; } $fileName = $pathInfo['dirname'] . '/' . $fileName; $rowAuthor->photo = rename(Pi::path($uploadInfo['tmp_name']), Pi::path($fileName)) ? $fileName : $uploadInfo['tmp_name']; $rowAuthor->save(); } unset($session->{$id}); unset($session->{$fakeId}); } return $id; }
/** * List custom articles and with a slideshow besides article list * * @param array $options * @param string $module * @return boolean */ public static function recommendedSlideshow($options = array(), $module = null) { if (!$module) { return false; } // Getting custom article list $columns = array('subject', 'summary', 'time_publish', 'image'); $ids = explode(',', $options['articles']); foreach ($ids as &$id) { $id = trim($id); } $where = array('id' => $ids); $articles = Entity::getAvailableArticlePage($where, 1, 10, $columns, null, $module); $config = Pi::config('', $module); $image = $config['default_feature_thumb']; $image = Pi::service('asset')->getModuleAsset($image, $module); foreach ($articles as &$article) { $article['subject'] = mb_substr($article['subject'], 0, $options['max_subject_length'], 'UTF-8'); $article['summary'] = mb_substr($article['summary'], 0, $options['max_summary_length'], 'UTF-8'); $article['image'] = $article['image'] ? Media::getThumbFromOriginal(Pi::url($article['image'])) : $image; } // Getting image link url $urlRows = explode('\\n', $options['image-link']); $imageLinks = array(); foreach ($urlRows as $row) { list($id, $url) = explode(':', trim($row), 2); $imageLinks[trim($id)] = trim($url); } // Fetching image ID $images = explode(',', $options['images']); $imageIds = array(); foreach ($images as $key => &$image) { $image = trim($image); if (is_numeric($image)) { $imageIds[] = $image; } else { $url = $image ?: 'image/default-recommended.png'; $image = array('url' => Pi::service('asset')->getModuleAsset($url, $module), 'link' => $imageLinks[$key + 1], 'title' => _b('This is default recommended image'), 'description' => _b('You should to add your own images and its title and description!')); } } if (!empty($imageIds)) { $images = array(); $rowset = Pi::model('media', $module)->select(array('id' => $imageIds)); foreach ($rowset as $row) { $id = $row['id']; $link = isset($imageLinks[$id]) ? $imageLinks[$id] : ''; $images[] = array('url' => Pi::url($row['url']), 'link' => $link, 'title' => $row['title'], 'description' => $row['description']); } } return array('articles' => $articles, 'target' => $options['target'], 'style' => $options['block-style'], 'elements' => (array) $options['element'], 'height' => $options['image-height'], 'images' => $images, 'config' => Pi::config('', $module), 'rows' => $options['description_rows']); }
/** * Topic list page for viewing */ public function allTopicAction() { $page = $this->params('p', 1); $page = $page > 0 ? $page : 1; $module = $this->getModule(); $config = Pi::config('', $module); $limit = (int) $config['page_limit_all']; $where = array('active' => 1); // Get topics $resultsetTopic = TopicService::getTopics($where, $page, $limit); foreach ($resultsetTopic as &$topic) { $topic['image'] = $topic['image'] ? Media::getThumbFromOriginal($topic['image']) : Pi::service('asset')->getModuleAsset($config['default_topic_image']); } $topicIds = array_keys($resultsetTopic) ?: array(0); // Get topic article counts $model = $this->getModel('article_topic'); $select = $model->select()->where(array('topic' => $topicIds))->columns(array('count' => new Expression('count(id)'), 'topic'))->group(array('topic')); $rowRelation = $model->selectWith($select); $articleCount = array(); foreach ($rowRelation as $row) { $articleCount[$row->topic] = $row->count; } // Get last added article $lastAdded = array(); $select = $model->select()->where(array('topic' => $topicIds))->columns(array('id' => new Expression('max(id)')))->group(array('topic')); $rowset = $model->selectWith($select); $ids = array(0); foreach ($rowset as $row) { $ids[] = $row['id']; } $rowAdded = $model->select(array('id' => $ids)); foreach ($rowAdded as $row) { $lastAdded[$row['topic']] = $row['time']; } // Total count $modelTopic = $this->getModel('topic'); $totalCount = $modelTopic->getSearchRowsCount($where); // Pagination $route = Pi::api('api', $module)->getRouteName(); $paginator = Paginator::factory($totalCount, array('limit' => $limit, 'page' => $page, 'url_options' => array('page_param' => 'p', 'params' => array('topic' => 'all')))); $this->view()->assign(array('title' => __('All Topics'), 'topics' => $resultsetTopic, 'paginator' => $paginator, 'count' => $articleCount, 'config' => $config, 'route' => $route, 'lastAdded' => $lastAdded)); }
/** * Save category information * * @param array $data Category information * @return boolean * @throws \Exception */ protected function saveCategory($data) { $module = $this->getModule(); $model = $this->getModel('category'); $fakeId = $image = null; if (isset($data['id'])) { $id = $data['id']; unset($data['id']); } $fakeId = $this->params('fake_id', 0); $parent = $data['parent']; unset($data['parent']); unset($data['image']); if (isset($data['slug']) && empty($data['slug'])) { unset($data['slug']); } if (empty($id)) { $id = $model->add($data, $parent); $row = $model->find($id); } else { $row = $model->find($id); if (empty($row)) { return $this->jumpTo404(_a('Category is not exists.')); } $row->assign($data); $row->save(); // Move node position $parentNode = $model->getParentNode($id); $currentParent = $parentNode['id']; if ($currentParent != $parent) { $children = $model->getDescendantIds($id); if (array_search($parent, $children) !== false) { return $this->jumpTo404(_a('Category cannot be moved to self or a child.')); } else { $model->move($id, $parent); } } } // Save image $session = Media::getUploadSession($module, 'category'); if (isset($session->{$id}) || $fakeId && isset($session->{$fakeId})) { $uploadInfo = isset($session->{$id}) ? $session->{$id} : $session->{$fakeId}; if ($uploadInfo) { $fileName = $row->id; $pathInfo = pathinfo($uploadInfo['tmp_name']); if ($pathInfo['extension']) { $fileName .= '.' . $pathInfo['extension']; } $fileName = $pathInfo['dirname'] . '/' . $fileName; $row->image = rename(Pi::path($uploadInfo['tmp_name']), Pi::path($fileName)) ? $fileName : $uploadInfo['tmp_name']; $row->save(); } unset($session->{$id}); unset($session->{$fakeId}); } return $id; }
/** * Save topic information * * @param array $data Topic information * @return boolean * @throws \Exception */ protected function saveTopic($data) { $module = $this->getModule(); $model = $this->getModel('topic'); $fakeId = $image = null; if (isset($data['id'])) { $id = $data['id']; unset($data['id']); } //$data['active'] = 1; $fakeId = $this->params('fake_id', 0); unset($data['image']); if (isset($data['slug']) && empty($data['slug'])) { unset($data['slug']); } if (empty($id)) { $row = $model->createRow($data); $row->save(); $id = $row->id; } else { $row = $model->find($id); if (empty($row->id)) { return false; } $row->assign($data); $row->save(); } // Save image $session = Media::getUploadSession($module, 'topic'); if (isset($session->{$id}) || $fakeId && isset($session->{$fakeId})) { $uploadInfo = isset($session->{$id}) ? $session->{$id} : $session->{$fakeId}; if ($uploadInfo) { $fileName = $row->id; $pathInfo = pathinfo($uploadInfo['tmp_name']); if ($pathInfo['extension']) { $fileName .= '.' . $pathInfo['extension']; } $fileName = $pathInfo['dirname'] . '/' . $fileName; $row->image = rename(Pi::path($uploadInfo['tmp_name']), Pi::path($fileName)) ? $fileName : $uploadInfo['tmp_name']; $row->save(); } unset($session->{$id}); unset($session->{$fakeId}); } return $id; }
/** * Get published article details * * @param array $where * @param int $page * @param int $limit * @param array $columns * @param string $order * @param string $module * @return array */ public static function getArticlePage($where, $page, $limit, $columns = null, $order = null, $module = null) { $offset = $limit && $page ? $limit * ($page - 1) : null; $module = $module ?: Pi::service('module')->current(); $config = Pi::config('', $module); $articleIds = $userIds = $authorIds = $categoryIds = array(); $categories = $authors = $users = $tags = $urls = array(); $modelArticle = Pi::model('article', $module); // Generate columns of extended table and statistics table $extendedColumns = Pi::service('registry')->handler('extended', $module)->read($module); $statisColumns = ModelStats::getAvailableColumns(); if (!empty($columns)) { // Get needed columns of extended table foreach ($extendedColumns as $key => $col) { if (!in_array($col, $columns)) { unset($extendedColumns[$key]); } } // Get needed columns of statistics table foreach ($statisColumns as $key => $col) { if (!in_array($col, $columns)) { unset($statisColumns[$key]); } } // Remove fields not belong to article table $columns = array_diff($columns, $extendedColumns); $columns = array_diff($columns, $statisColumns); } $resultset = $modelArticle->getSearchRows($where, $limit, $offset, $columns, $order); if ($resultset) { foreach ($resultset as $row) { $articleIds[] = $row['id']; if (!empty($row['author'])) { $authorIds[] = $row['author']; } if (!empty($row['uid'])) { $userIds[] = $row['uid']; } } $authorIds = array_unique($authorIds); $userIds = array_unique($userIds); // Getting statistics data $templateStatis = array(); if (!empty($statisColumns)) { $statisColumns[] = 'id'; $statisColumns[] = 'article'; $modelStatis = Pi::model('stats', $module); $select = $modelStatis->select()->where(array('article' => $articleIds))->columns($statisColumns); $rowStatis = $modelStatis->selectWith($select); $statis = array(); foreach ($rowStatis as $item) { $temp = $item->toArray(); unset($temp['id']); unset($temp['article']); $statis[$item->article] = $temp; } foreach ($statisColumns as $col) { if (in_array($col, array('id', 'article'))) { continue; } $templateStatis[$col] = null; } } // Default extended columns $extendedColumns = array_merge($extendedColumns, array('slug', 'seo_title', 'seo_keywords', 'seo_description')); $extendedColumns = array_unique($extendedColumns); // Getting extended data $templateExtended = array(); if (!empty($extendedColumns)) { $extendedColumns[] = 'id'; $extendedColumns[] = 'article'; $modelExtended = Pi::model('extended', $module); $select = $modelExtended->select()->where(array('article' => $articleIds))->columns($extendedColumns); $rowExtended = $modelExtended->selectWith($select); $extended = array(); foreach ($rowExtended as $item) { $temp = $item->toArray(); unset($temp['id']); unset($temp['article']); $extended[$item->article] = $temp; } foreach ($extendedColumns as $col) { if (in_array($col, array('id', 'article'))) { continue; } $templateExtended[$col] = null; } } $categories = Pi::api('api', $module)->getCategoryList(); if (!empty($authorIds) && (empty($columns) || in_array('author', $columns))) { $resultsetAuthor = Pi::api('api', $module)->getAuthorList($authorIds); foreach ($resultsetAuthor as $row) { $authors[$row['id']] = array('name' => $row['name']); } unset($resultsetAuthor); } if (!empty($userIds) && (empty($columns) || in_array('uid', $columns))) { $resultsetUser = Pi::user()->get($userIds, array('id', 'name')); foreach ($resultsetUser as $row) { $users[$row['id']] = array('name' => $row['name']); } unset($resultsetUser); } /* if (!empty($articleIds)) { if ((empty($columns) || in_array('tag', $columns)) && $config['enable_tag'] ) { $tags = Pi::service('tag')->get($module, $articleIds); } } */ foreach ($resultset as &$row) { if (empty($columns) || in_array('category', $columns)) { if (!empty($categories[$row['category']])) { $row['category_title'] = $categories[$row['category']]['title']; $row['category_slug'] = $categories[$row['category']]['slug']; } } if (empty($columns) || in_array('uid', $columns)) { if (!empty($users[$row['uid']])) { $row['user_name'] = $users[$row['uid']]['name']; } } if (empty($columns) || in_array('author', $columns)) { if (!empty($authors[$row['author']])) { $row['author_name'] = $authors[$row['author']]['name']; } } if (empty($columns) || in_array('image', $columns)) { if ($row['image']) { $row['thumb'] = Media::getThumbFromOriginal($row['image']); } } /* if ((empty($columns) || in_array('tag', $columns)) && $config['enable_tag']) { if (!empty($tags[$row['id']])) { $row['tag'] = $tags[$row['id']]; } } */ if (empty($columns) || in_array('subject', $columns)) { $route = Pi::api('api', $module)->getRouteName($module); $row['url'] = Pi::service('url')->assemble($route, array('time' => date('Ymd', $row['time_publish']), 'id' => $row['id'], 'slug' => $extended[$row['id']]['slug'])); } if (!isset($statis[$row['id']])) { $statis[$row['id']] = $templateStatis; } $row = array_merge($row, $statis[$row['id']]); if (!isset($extended[$row['id']])) { $extended[$row['id']] = $templateExtended; } $row = array_merge($row, $extended[$row['id']]); } } return $resultset; }
/** * Saving media information * * @param array $data Media information * @return boolean * @throws \Exception */ protected function saveMedia($data) { $module = $this->getModule(); $model = $this->getModel('media'); $image = null; if (isset($data['id'])) { $id = $data['id']; unset($data['id']); } $fakeId = $this->params('fake_id', 0); unset($data['media']); // Getting media info $session = Media::getUploadSession($module, 'media'); if (isset($session->{$id}) || $fakeId && isset($session->{$fakeId})) { $uploadInfo = isset($session->{$id}) ? $session->{$id} : $session->{$fakeId}; if ($uploadInfo) { $pathInfo = pathinfo($uploadInfo['tmp_name']); if ($pathInfo['extension']) { $data['type'] = strtolower($pathInfo['extension']); } $data['size'] = filesize(Pi::path($uploadInfo['tmp_name'])); // Meta $metaColumns = array('w', 'h'); $meta = array(); foreach ($uploadInfo as $key => $info) { if (in_array($key, $metaColumns)) { $meta[$key] = $info; } } $data['meta'] = json_encode($meta); } unset($session->{$id}); unset($session->{$fakeId}); } // Getting user ID if (!isset($data['uid']) || !$data['uid']) { $data['uid'] = Pi::user()->getId(); } if (empty($id)) { $data['time_upload'] = time(); $row = $model->createRow($data); $row->save(); } else { $data['time_update'] = time(); $row = $model->find($id); if (empty($row)) { return false; } $row->assign($data); $row->save(); } // Save image if (!empty($uploadInfo)) { $fileName = $row->id; if ($pathInfo['extension']) { $fileName .= '.' . $pathInfo['extension']; } $fileName = $pathInfo['dirname'] . '/' . $fileName; $row->url = rename(Pi::path($uploadInfo['tmp_name']), Pi::path($fileName)) ? $fileName : $uploadInfo['tmp_name']; $row->save(); } return $row->id; }
/** * 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')); } }