/**
  * Prepare breadcrumbs
  *
  * @param \Magefan\Blog\Model\Post $post
  * @throws \Magento\Framework\Exception\LocalizedException
  * @return void
  */
 protected function _addBreadcrumbs(\Magefan\Blog\Model\Post $post)
 {
     if ($this->_scopeConfig->getValue('web/default/show_cms_breadcrumbs', ScopeInterface::SCOPE_STORE) && ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs'))) {
         $breadcrumbsBlock->addCrumb('home', ['label' => __('Home'), 'title' => __('Go to Home Page'), 'link' => $this->_storeManager->getStore()->getBaseUrl()]);
         $breadcrumbsBlock->addCrumb('blog', ['label' => __('Blog'), 'title' => __('Go to Blog Home Page'), 'link' => $this->_url->getBaseUrl()]);
         $breadcrumbsBlock->addCrumb('blog_post', ['label' => $post->getTitle(), 'title' => $post->getTitle()]);
     }
 }
 /**
  * Before model save
  * @param  \Magefan\Blog\Model\Post $model
  * @param  \Magento\Framework\App\Request\Http $request
  * @return void
  */
 protected function _beforeSave($model, $request)
 {
     if ($links = $request->getParam('links')) {
         foreach (array('post', 'product') as $key) {
             $param = 'related' . $key . 's';
             if (!empty($links[$param])) {
                 $ids = array_unique(array_map('intval', explode('&', $links[$param])));
                 if (count($ids)) {
                     $model->setData('related_' . $key . '_ids', $ids);
                 }
             }
         }
     }
 }
Beispiel #3
0
 /**
  * Retrieve post short content
  * @param  \Magefan\Blog\Model\Post $post
  *
  * @return string
  */
 public function getShorContent($post)
 {
     $content = $this->_filterProvider->getPageFilter()->filter($post->getContent());
     $pageBraker = '<!-- pagebreak -->';
     if ($p = mb_strpos($content, $pageBraker)) {
         $content = mb_substr($content, 0, $p);
         try {
             libxml_use_internal_errors(true);
             $dom = new \DOMDocument();
             $dom->loadHTML('<?xml encoding="UTF-8">' . $content);
             $body = $dom->getElementsByTagName('body');
             if ($body && $body->length > 0) {
                 $body = $body->item(0);
                 $_content = new \DOMDocument();
                 foreach ($body->childNodes as $child) {
                     $_content->appendChild($_content->importNode($child, true));
                 }
                 $content = $_content->saveHTML();
             }
         } catch (\Exception $e) {
         }
     }
     return $content;
 }
Beispiel #4
0
 /**
  * Before model save
  * @param  \Magefan\Blog\Model\Post $model
  * @param  \Magento\Framework\App\Request\Http $request
  * @return void
  */
 protected function _beforeSave($model, $request)
 {
     /* Prepare dates */
     $dateFilter = $this->_objectManager->create('Magento\\Framework\\Stdlib\\DateTime\\Filter\\Date');
     $data = $model->getData();
     $filterRules = [];
     foreach (['publish_time', 'custom_theme_from', 'custom_theme_to'] as $dateField) {
         if (!empty($data[$dateField])) {
             $filterRules[$dateField] = $dateFilter;
         }
     }
     $inputFilter = new \Zend_Filter_Input($filterRules, [], $data);
     $data = $inputFilter->getUnescaped();
     $model->setData($data);
     /* Prepare author */
     if (!$model->getAuthorId()) {
         $authSession = $this->_objectManager->get('Magento\\Backend\\Model\\Auth\\Session');
         $model->setAuthorId($authSession->getUser()->getId());
     }
     /* Prepare relative links */
     $data = $request->getPost('data');
     $links = isset($data['links']) ? $data['links'] : null;
     if ($links && is_array($links)) {
         foreach (['post', 'product'] as $linkType) {
             if (!empty($links[$linkType]) && is_array($links[$linkType])) {
                 $linksData = [];
                 foreach ($links[$linkType] as $item) {
                     $linksData[$item['id']] = ['position' => $item['position']];
                 }
                 $links[$linkType] = $linksData;
             }
         }
         $model->setData('links', $links);
     }
     /* Prepare images */
     $data = $model->getData();
     foreach (['featured_img', 'og_img'] as $key) {
         if (isset($data[$key]) && is_array($data[$key])) {
             if (!empty($data[$key]['delete'])) {
                 $model->setData($key, null);
             } else {
                 if (isset($data[$key][0]['name']) && isset($data[$key][0]['tmp_name'])) {
                     $image = $data[$key][0]['name'];
                     $model->setData($key, Post::BASE_MEDIA_PATH . DIRECTORY_SEPARATOR . $image);
                     $imageUploader = $this->_objectManager->get('Magefan\\Blog\\ImageUpload');
                     $imageUploader->moveFileFromTmp($image);
                 } else {
                     if (isset($data[$key][0]['name'])) {
                         $model->setData($key, $data[$key][0]['name']);
                     }
                 }
             }
         } else {
             $model->setData($key, null);
         }
     }
 }