/**
  * {@inheritdoc}
  */
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {
     /* Create categories */
     $categoryData = ['title' => 'Category {n}', 'is_active' => 1, 'store_ids' => [0], 'path' => 0];
     $categoriesId = [];
     for ($i = 1; $i < 4; $i++) {
         $data = $categoryData;
         $data['title'] = str_replace('{n}', $i, $data['title']);
         $category = $this->_categoryFactory->create()->setData($data)->save();
         $categoriesId[] = $parentCategoryId = $category->getId();
         for ($j = 1; $j < 3; $j++) {
             $data = $categoryData;
             $data['title'] = str_replace('{n}', $i . '.' . $j, $data['title']);
             $data['path'] = $parentCategoryId;
             $category = $this->_categoryFactory->create()->setData($data)->save();
             $categoriesId[] = $parentCategoryId = $category->getId();
         }
     }
     /* Create Posts */
     $postData = ['title' => 'Article {n}', 'is_active' => 1, 'store_ids' => [0], 'categories' => [], 'relatedposts_links' => [], 'author_id' => 0];
     $postsId = [];
     $authorsId = $this->_authorFactory->create()->getCollection()->getAllIds();
     $time = time() - 86400 * 200;
     for ($i = 1; $i < 23; $i++) {
         $data = $postData;
         $data['title'] = str_replace('{n}', $i, $data['title']);
         $data['content'] = str_replace('{n}', $i, $this->_getPostContent());
         $data['publish_time'] = $time + 86400 * $i * 3;
         $data['creation_time'] = $data['publish_time'];
         $data['update_time'] = $data['publish_time'];
         if (count($categoriesId)) {
             for ($x = 0; $x < 2; $x++) {
                 $cid = $categoriesId[rand(0, count($categoriesId) - 1)];
                 $data['categories'][$cid] = $cid;
             }
         }
         if (count($postsId)) {
             for ($x = 0; $x < 5; $x++) {
                 $pid = $postsId[rand(0, count($postsId) - 1)];
                 $data['relatedposts_links'][$pid] = $pid;
             }
         }
         if (count($authorsId)) {
             $data['author_id'] = $authorsId[rand(0, count($authorsId) - 1)];
         }
         $post = $this->_postFactory->create()->setData($data)->save();
         $postsId[] = $post->getId();
     }
 }
示例#2
0
 /**
  * Retrieve category id by identifier
  * @param  string $identifier
  * @return int
  */
 protected function _getAuthorId($identifier)
 {
     if (is_null($this->_authorId)) {
         $author = $this->_authorFactory->create();
         $this->_authorId = $author->checkIdentifier($identifier);
     }
     return $this->_authorId;
 }
示例#3
0
 /**
  * Retrieve post author
  * @return \Magefan\Blog\Model\Author | false
  */
 public function getAuthor()
 {
     if (!$this->hasData('author')) {
         $author = false;
         if ($authorId = $this->getData('author_id')) {
             $_author = $this->_authorFactory->create();
             $_author->load($authorId);
             if ($_author->getId()) {
                 $author = $_author;
             }
         }
         $this->setData('author', $author);
     }
     return $this->getData('author');
 }