Exemplo n.º 1
0
 /**
  * Save category relationships
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function save(AbstractController $controller, Application $application)
 {
     $type = null;
     $contentId = null;
     if ($_POST && $controller->hasView() && null !== $controller->view()->id && null !== $controller->view()->form && $controller->view()->form instanceof \Pop\Form\Form) {
         $categories = $controller->view()->form->categories;
         $type = $controller->view()->form->category_type;
         $contentId = $controller->view()->id;
         // Clear categories
         if (null !== $type && null !== $contentId) {
             if (!is_array($contentId)) {
                 $contentId = [$contentId];
             }
             foreach ($contentId as $id) {
                 $itemId = $type == 'media' ? 'media_id' : 'content_id';
                 $c2c = new Table\CategoryItems();
                 $c2c->delete([$itemId => $id]);
             }
             if (is_array($categories) && count($categories) > 0) {
                 foreach ($categories as $category) {
                     foreach ($contentId as $id) {
                         if ($type == 'media') {
                             $fields = ['category_id' => $category, 'content_id' => null, 'media_id' => $id, 'order' => (int) $_POST['category_order_' . $category]];
                         } else {
                             $fields = ['category_id' => $category, 'content_id' => $id, 'media_id' => null, 'order' => (int) $_POST['category_order_' . $category]];
                         }
                         $catItem = new Table\CategoryItems($fields);
                         $catItem->save();
                     }
                 }
             }
         }
     }
 }