public function renderOrder() { $sectionId = $this->getInt('id'); $direction = $this->getStr('direction'); $blockGroup = new ArticleSection($sectionId); $oldOrder = $blockGroup->order; $lastOrder = ArticleSection::collection(['article_id' => $blockGroup->articleId])->count(); if ($oldOrder == 1 && $direction == 'up' || $oldOrder == $lastOrder && $direction == 'down') { // Invalid Order Action Redirect::to('/admin/article/' . $blockGroup->articleId . '/edit')->now(); } else { $oldOrder = (int) $oldOrder; switch ($direction) { case 'up': $swapOrder = $oldOrder - 1; $swapOrder = $swapOrder < 1 ? 0 : $swapOrder; break; case 'down': $swapOrder = $oldOrder + 1; break; } $swapBlockGroup = ArticleSection::collection()->loadWhere(['article_id' => $blockGroup->articleId, 'order' => $swapOrder])->first(); if ($swapBlockGroup !== null) { $swapBlockGroup->order = $oldOrder; $swapBlockGroup->saveChanges(); } $blockGroup->order = $swapOrder; $blockGroup->saveChanges(); Redirect::to('/admin/article/' . $blockGroup->articleId . '/edit')->now(); } }
/** * Add example articles * * @return $this */ protected function _addArticles() { echo 'Adding Articles: '; $count = 0; /** @var Category[] $categories */ $categories = Category::collection(); /** @var Platform[] $platforms */ $platforms = Platform::collection(); foreach ($categories as $category) { $articleTitles = $this->_getTitleArray('Article', rand(1, 3)); foreach ($articleTitles as $articleTitle) { $article = new Article(); $article->categoryId = $category->id(); $article->title = $articleTitle; $article->slug = Strings::urlize($articleTitle); $article->subTitle = $this->_getExampleContent(rand(4, 20)); $article->saveChanges(); $blockCount = rand(2, 6); $i = 0; do { $section = new ArticleSection(); $section->articleId = $article->id(); $section->saveChanges(); foreach ($platforms as $platform) { $block = new ArticleSectionBlock(); $block->articleSectionId = $section->id(); $block->platformId = $platform->id(); $block->title = $this->_getExampleContent(3); $block->content = $this->_getExampleContent(rand(10, 30)); $block->saveChanges(); } $i++; } while ($i <= $blockCount); $count++; } } echo $count . PHP_EOL; return $this; }