/** * {@inheritdoc} */ public function generate(array $parameters = []) { $id = $parameters['id']; $category_id = isset($parameters['category_id']) ? $parameters['category_id'] : 0; if (!isset($this->cacheEntries[$id . '-' . $category_id])) { if (!($file = File::find($id))) { throw new RouteNotFoundException('File not found.'); } if ($category_id && !($category = Category::find($category_id))) { throw new RouteNotFoundException('Category not found.'); } $this->addCache($file, $category_id, isset($category) ? $category->slug : App::config('bixie/download')->get('root_category', 'root')); } $meta = $this->cacheEntries[$id . '-' . $category_id]; $parameters['slug'] = $meta['slug']; unset($parameters['id']); unset($parameters['category_id']); return $parameters; }
/** * @Saving */ public static function saving($event, Category $category) { $db = self::getConnection(); $i = 2; $id = $category->id; if (!$category->slug) { $category->slug = $category->title; } // Ensure unique slug while (self::where(['slug = ?', 'parent_id= ?'], [$category->slug, $category->parent_id])->where(function ($query) use($id) { if ($id) { $query->where('id <> ?', [$id]); } })->first()) { $category->slug = preg_replace('/-\\d+$/', '', $category->slug) . '-' . $i++; } // Update own path $path = '/' . $category->slug; if ($category->parent_id && ($parent = Category::find($category->parent_id))) { $path = $parent->path . $path; } else { // set Parent to 0, if old parent is not found $category->parent_id = 0; } // Update children's paths if ($id && $path != $category->path) { $db->executeUpdate('UPDATE ' . self::getMetadata()->getTable() . ' SET path = REPLACE (' . $db->getDatabasePlatform()->getConcatExpression($db->quote('//'), 'path') . ", '//{$category->path}', '{$path}')" . ' WHERE path LIKE ' . $db->quote($category->path . '//%')); } $category->path = $path; // Set priority if (!$id) { $category->priority = 1 + $db->createQueryBuilder()->select($db->getDatabasePlatform()->getMaxExpression('priority'))->from(self::getMetadata()->getTable())->where(['parent_id' => $category->parent_id])->execute()->fetchColumn(); } }
/** * @Route("/updateOrder", methods="POST") * @Request({"categories": "array"}, csrf=true) */ public function updateOrderAction($categories = []) { foreach ($categories as $data) { if ($category = Category::find($data['id'])) { $category->priority = $data['order']; $category->parent_id = $data['parent_id'] ?: 0; $category->save(); } } return ['message' => 'success']; }