Exemple #1
0
 /**
  * Finally delete content item
  */
 public function make()
 {
     // remove gallery files if exists
     foreach ($this->_records->get() as $record) {
         $galleryPath = '/upload/gallery/' . (int) $record->id;
         if (Directory::exist($galleryPath)) {
             Directory::remove($galleryPath);
         }
     }
     // finally remove from db
     $this->_records->forceDelete();
 }
Exemple #2
0
 /**
  * Build search results
  * @return array[AbstractSearchResult]
  */
 public function getResult()
 {
     // relevant search by string query
     $records = Content::where('display', '=', 1)->search($this->query)->take($this->limit)->get();
     // check if result is not empty
     if ($records->count() < 1) {
         return [];
     }
     // build result items
     $result = [];
     foreach ($records as $item) {
         /** @var \Apps\ActiveRecord\Content $item */
         $title = $item->getLocaled('title');
         $text = App::$Security->strip_tags($item->getLocaled('text'));
         $snippet = Text::snippet($text);
         // prevent empty items
         if (Str::likeEmpty($title)) {
             continue;
         }
         // initialize abstract response pattern
         $res = new AbstractSearchResult();
         $res->setTitle($title);
         $res->setSnippet($snippet);
         $res->setDate($item->created_at);
         $res->setRelevance((int) $item->relevance);
         $res->setUri('/content/read/' . $item->getPath());
         // accumulate response var
         $result[] = $res;
     }
     return $result;
 }
Exemple #3
0
 /**
  * Change content item rating action
  * @param string $type
  * @param int $id
  * @throws NativeException
  * @throws ForbiddenException
  * @throws NotFoundException
  * @return string
  */
 public function actionChangerate($type, $id)
 {
     // check input params
     if (!Arr::in($type, ['plus', 'minus']) || !Obj::isLikeInt($id)) {
         throw new NativeException('Bad conditions');
     }
     // get current user and check is authed
     $user = App::$User->identity();
     if ($user === null || !App::$User->isAuth()) {
         throw new ForbiddenException(__('Authorization is required!'));
     }
     // set ignored content id to rate in session
     $ignored = App::$Session->get('content.rate.ignore');
     $ignored[] = $id;
     App::$Session->set('content.rate.ignore', $ignored);
     // find content record
     $record = ContentRecord::find($id);
     if ($record === null || $record->count() < 1) {
         throw new NotFoundException(__('Content item is not founded'));
     }
     // check if author rate him-self content
     if ($record->author_id === $user->getId()) {
         throw new ForbiddenException(__('You can not rate your own content'));
     }
     // initialize model
     $model = new ContentRatingChange($record, $type, $user);
     // check if content items is already rated by this user
     if ($model->isAlreadyRated()) {
         throw new ForbiddenException(__('You have already rate this!'));
     }
     // make rate - add +1 to content rating and author rating
     $model->make();
     return json_encode(['status' => 1, 'rating' => $model->getRating()]);
 }
 /**
  * Prepare conditions to build content list
  * @throws NotFoundException
  */
 public function before()
 {
     // check length of passed terms
     if (!Obj::isString($this->_terms) || Str::length($this->_terms) < self::MIN_QUERY_LENGTH) {
         throw new NotFoundException(__('Search terms is too short'));
     }
     // lets make active record building
     $this->_records = ContentEntity::whereNotIn('id', $this->_skip)->search($this->_terms)->take(self::MAX_ITEMS)->get();
     $this->buildContent();
     parent::before();
 }
Exemple #5
0
 /**
  * Generate random string for comment hash value
  * @return string
  */
 private function generateCommentHash()
 {
     $hash = Str::randomLatinNumeric(mt_rand(32, 128));
     $find = Content::where('comment_hash', '=', $hash)->count();
     // hmmm, is always exist? Chance of it is TOOOO low, but lets recursion re-generate
     if ($find !== 0) {
         return $this->generateCommentHash();
     }
     return $hash;
 }
 /**
  * Find content items on database and return rows as object
  * @return mixed
  * @throws NotFoundException
  */
 private function findItems()
 {
     if (!Obj::isArray($this->_catIds) || count($this->_catIds) < 1) {
         throw new NotFoundException(__('Category is not founded'));
     }
     // calculate selection offset
     $itemPerPage = (int) $this->_configs['itemPerCategory'];
     // check if custom itemlimit defined over model api
     if ($this->_customItemLimit !== false) {
         $itemPerPage = (int) $this->_customItemLimit;
     }
     $offset = $this->_page * $itemPerPage;
     // get all items from categories
     $query = ContentRecord::whereIn('category_id', $this->_catIds)->where('display', '=', 1);
     // save count
     $this->_contentCount = $query->count();
     // apply sort by
     switch ($this->_sort) {
         case 'rating':
             $query = $query->orderBy('rating', 'DESC');
             break;
         case 'views':
             $query = $query->orderBy('views', 'DESC');
             break;
         default:
             $query = $query->orderBy('created_at', 'DESC');
             break;
     }
     // get all items if offset is negative
     if ($itemPerPage < 0) {
         return $query->get();
     }
     // make select based on offset
     return $query->skip($offset)->take($itemPerPage)->get();
 }
Exemple #7
0
 /**
  * Make query to database
  * @return object
  */
 private function makeQuery()
 {
     return Content::select(['contents.*', 'content_categories.path as cpath', 'content_categories.title as ctitle'])->whereIn('contents.category_id', $this->categories)->join('content_categories', 'content_categories.id', '=', 'contents.category_id', 'left outer')->orderBy('contents.created_at', 'DESC')->take($this->count)->get();
 }
Exemple #8
0
 /**
  * Cron schedule action - build content sitemap
  * @throws \Ffcms\Core\Exception\NativeException
  * @throws \Ffcms\Core\Exception\SyntaxException
  */
 public static function buildSitemapSchedule()
 {
     // get records from database as activerecord object
     $contents = \Apps\ActiveRecord\Content::where('display', '=', 1);
     if ($contents->count() < 1) {
         return;
     }
     // get languages if multilanguage enabled
     $langs = null;
     if (App::$Properties->get('multiLanguage')) {
         $langs = App::$Properties->get('languages');
     }
     // build sitemap from content items via business model
     $sitemap = new EntityBuildMap($langs);
     foreach ($contents->get() as $content) {
         $category = $content->getCategory();
         $uri = '/content/read/';
         if (!Str::likeEmpty($category->path)) {
             $uri .= $category->path . '/';
         }
         $uri .= $content->path;
         $sitemap->add($uri, $content->created_at, 'weekly', 0.7);
     }
     // add categories
     $categories = ContentCategory::all();
     foreach ($categories as $item) {
         if ((bool) $item->getProperty('showCategory')) {
             $uri = '/content/list/' . $item->path;
             $sitemap->add($uri, date('c'), 'daily', 0.9);
         }
     }
     // save data to xml file
     $sitemap->save('content');
 }
 /**
  * Update records and set display = 1
  */
 public function make()
 {
     $this->_records->update(['display' => 1]);
 }
 /**
  * Calculate content on moderation
  */
 private function calcContentsModerate()
 {
     $this->contents = Content::where('display', '=', 0)->count();
 }
Exemple #11
0
 /**
  * Publish content on moderate stage
  * @return string
  * @throws NotFoundException
  * @throws SyntaxException
  * @throws NativeException
  */
 public function actionPublish()
 {
     // get ids as array from GET
     $ids = $this->request->query->get('selected');
     if (!Obj::isArray($ids) || count($ids) < 1) {
         throw new NotFoundException(__('Items to publish is not found'));
     }
     // try to find items in db
     $records = ContentEntity::whereIn('id', $ids)->where('display', '=', 0);
     if ($records->count() < 1) {
         throw new NotFoundException(__('Items to publish is not found'));
     }
     // initialize model and operate submit
     $model = new FormContentPublish($records);
     if ($model->send() && $model->validate()) {
         $model->make();
         App::$Session->getFlashBag()->add('success', __('Content is successful published'));
         $this->response->redirect('content/index');
     }
     // draw view output
     return $this->view->render('publish', ['records' => $records->get(), 'model' => $model]);
 }