/** * 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; }
/** * List content items * @return string * @throws \Ffcms\Core\Exception\SyntaxException * @throws \Ffcms\Core\Exception\NativeException */ public function actionIndex() { // set current page and offset $page = (int) $this->request->query->get('page'); $offset = $page * self::ITEM_PER_PAGE; $query = null; // get query type (trash, category, all) $type = $this->request->query->get('type'); if ($type === 'trash') { $query = ContentEntity::onlyTrashed(); } elseif ($type === 'moderate') { // only items on moderate $query = ContentEntity::where('display', '=', 0); } elseif (Obj::isLikeInt($type)) { // sounds like category id ;) $query = ContentEntity::where('category_id', '=', (int) $type); } else { $query = new ContentEntity(); $type = 'all'; } // build pagination $pagination = new SimplePagination(['url' => ['content/index', null, null, ['type' => $type]], 'page' => $page, 'step' => self::ITEM_PER_PAGE, 'total' => $query->count()]); // build listing objects $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); return $this->view->render('index', ['records' => $records, 'pagination' => $pagination, 'type' => $type]); }
/** * 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; }
/** * 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'); }
/** * Calculate content on moderation */ private function calcContentsModerate() { $this->contents = Content::where('display', '=', 0)->count(); }