Esempio n. 1
0
 /**
  * Set model attributes from record object
  */
 public function before()
 {
     if ($this->_record->id === null) {
         $this->_new = true;
         // pass owner id category from construct model
         $this->dependId = $this->_tmpDependId < 1 ? 1 : $this->_tmpDependId;
     } else {
         // make tmp id for frontend form
         $this->id = $this->_record->id;
         $path = $this->_record->path;
         // nesting levels
         if (Str::contains('/', $path)) {
             $nestedPath = explode('/', $path);
             $this->path = array_pop($nestedPath);
             $this->_pathNested = implode('/', $nestedPath);
             // get owner category id by nesting path
             $owner = ContentCategory::getByPath($this->_pathNested);
             if ($owner !== null) {
                 $this->dependId = $owner->id;
             }
         } else {
             $this->path = $path;
         }
         // set data from record
         $this->title = $this->_record->title;
         $this->description = $this->_record->description;
         if ($this->_record->configs !== null && !Str::likeEmpty($this->_record->configs)) {
             $this->configs = $this->_record->configs;
         }
     }
 }
Esempio n. 2
0
 /**
  * Prepare model attributes from passed objects
  * @throws ForbiddenException
  */
 public function before()
 {
     $this->id = $this->_content->id;
     $this->title = $this->_content->getLocaled('title');
     $this->text = $this->_content->getLocaled('text');
     // check if title and text are exists
     if (Str::length($this->title) < 1 || Str::length($this->text) < 1) {
         throw new ForbiddenException();
     }
     // get meta data
     $this->metaTitle = $this->_content->getLocaled('meta_title');
     if (Str::likeEmpty($this->metaTitle)) {
         $this->metaTitle = $this->title;
     }
     $this->metaDescription = $this->_content->getLocaled('meta_description');
     $tmpKeywords = $this->_content->getLocaled('meta_keywords');
     $this->metaKeywords = explode(',', $tmpKeywords);
     // set content date, category data
     $this->createDate = Date::humanize($this->_content->created_at);
     $this->catName = $this->_category->getLocaled('title');
     $this->catPath = $this->_category->path;
     // set user data
     if (App::$User->isExist($this->_content->author_id)) {
         $this->authorId = $this->_content->author_id;
         $profile = App::$User->identity($this->authorId)->getProfile();
         $this->authorName = $profile->getNickname();
     }
     $this->source = $this->_content->source;
     $this->views = $this->_content->views + 1;
     // check for dependence, add '' for general cat, ex: general/depend1/depend2/.../depend-n
     $catNestingArray = Arr::merge([0 => ''], explode('/', $this->catPath));
     if ($catNestingArray > 1) {
         // latest element its a current nesting level, lets cleanup it
         array_pop($catNestingArray);
         $catNestingPath = null;
         foreach ($catNestingArray as $cPath) {
             $catNestingPath .= $cPath;
             // try to find category by path in db
             $record = ContentCategory::getByPath($catNestingPath);
             if ($record !== null && $record->count() > 0) {
                 // if founded - add to nesting data
                 $this->catNesting[] = ['name' => $record->getLocaled('title'), 'path' => $record->path];
             }
             if (!Str::likeEmpty($catNestingPath)) {
                 $catNestingPath .= '/';
             }
         }
     }
     // build array of category nesting level
     $this->catNesting[] = ['name' => $this->catName, 'path' => $this->catPath];
     // get gallery images and poster data
     $galleryPath = '/upload/gallery/' . $this->_content->id;
     // check if gallery folder is exist
     if (Directory::exist($galleryPath)) {
         $originImages = File::listFiles($galleryPath . '/orig/', ['.jpg', '.png', '.gif', '.jpeg', '.bmp', '.webp'], true);
         // generate poster data
         if (Arr::in($this->_content->poster, $originImages)) {
             // original poster
             $posterName = $this->_content->poster;
             $this->posterFull = $galleryPath . '/orig/' . $posterName;
             if (!File::exist($this->posterFull)) {
                 $this->posterFull = null;
             }
             // thumb poster
             $posterSplit = explode('.', $posterName);
             array_pop($posterSplit);
             $posterCleanName = implode('.', $posterSplit);
             $this->posterThumb = $galleryPath . '/thumb/' . $posterCleanName . '.jpg';
             if (!File::exist($this->posterThumb)) {
                 $this->posterThumb = null;
             }
         }
         // generate full gallery
         foreach ($originImages as $image) {
             $imageSplit = explode('.', $image);
             array_pop($imageSplit);
             $imageClearName = implode('.', $imageSplit);
             // skip image used in poster
             if (Str::startsWith($imageClearName, $this->_content->poster)) {
                 continue;
             }
             $thumbPath = $galleryPath . '/thumb/' . $imageClearName . '.jpg';
             if (File::exist($thumbPath)) {
                 $this->galleryItems[$thumbPath] = $galleryPath . '/orig/' . $image;
             }
         }
     }
     // set rating data
     $this->rating = $this->_content->rating;
     $ignoredRate = App::$Session->get('content.rate.ignore');
     $this->canRate = true;
     if (Obj::isArray($ignoredRate) && Arr::in((string) $this->id, $ignoredRate)) {
         $this->canRate = false;
     }
     if (!App::$User->isAuth()) {
         $this->canRate = false;
     } elseif ($this->authorId === App::$User->identity()->getId()) {
         $this->canRate = false;
     }
     // update views count
     $this->_content->views += 1;
     $this->_content->save();
 }
Esempio n. 3
0
 /**
  * Show content item
  * @throws NotFoundException
  * @throws \Ffcms\Core\Exception\SyntaxException
  * @throws \Ffcms\Core\Exception\NativeException
  * @return string
  */
 public function actionRead()
 {
     // get raw path without controller-action
     $rawPath = $this->request->getPathWithoutControllerAction();
     $arrayPath = explode('/', $rawPath);
     // get category and content item path as string
     $contentPath = array_pop($arrayPath);
     $categoryPath = implode('/', $arrayPath);
     // try to find category object by string pathway
     $categoryRecord = ContentCategory::getByPath($categoryPath);
     // if no categories are available for this path - throw exception
     if ($categoryRecord === null || $categoryRecord->count() < 1) {
         throw new NotFoundException();
     }
     // try to find content entity record
     $contentRecord = ContentEntity::where('path', '=', $contentPath)->where('category_id', '=', $categoryRecord->id);
     $trash = false;
     // if no entity is founded for this path lets try to find on trashed
     if ($contentRecord === null || $contentRecord->count() !== 1) {
         // check if user can access to content list on admin panel
         if (!App::$User->isAuth() || !App::$User->identity()->getRole()->can('Admin/Content/Index')) {
             throw new NotFoundException();
         }
         // lets try to find in trashed
         $contentRecord->withTrashed();
         // no way, lets throw exception
         if ($contentRecord->count() !== 1) {
             throw new NotFoundException();
         }
         // set trashed marker for this item
         $trash = true;
     }
     // lets init entity model for content transfer to view
     $model = new EntityContentRead($categoryRecord, $contentRecord->first());
     $search = null;
     // check if similar search is enabled for item category
     if ((int) $model->getCategory()->getProperty('showSimilar') === 1 && $trash === false) {
         $search = new EntityContentSearch($model->title, $model->id);
     }
     // define read event
     App::$Event->run(static::EVENT_CONTENT_READ, ['model' => $model]);
     // render view output
     return $this->view->render('read', ['model' => $model, 'search' => $search, 'trash' => $trash, 'configs' => $this->getConfigs()]);
 }