Phalcon - Gestión de categorías

index.phtml

<?php echo $this->getContent(); ?>  

<div align = "right"> 
   <?php echo \Phalcon\Tag::linkTo(array("categories/new", "Create Categories", "class" => "btn")) ?> 
</div>  

<div align = "center">  
   <h1>Search categories</h1>  
   <?php echo \Phalcon\Tag::form(array("categories/search", "autocomplete" => "off")) ?> 
   <table align = "center"> 
      <tr> 
         <td align = "right"> 
            <label for = "id">Id</label> 
         </td> 
         <td align = "left"> 
            <?php echo \Phalcon\Tag::textField(array("id", "type" => "numeric")) ?> 
         </td> 
      </tr> 
      
      <tr> 
         <td align = "right"> 
            <label for = "name">Name</label> 
         </td> 
         <td align = "left"> 
            <?php echo \Phalcon\Tag::textField(array("name", "size" => 30)) ?> 
         </td> 
      </tr> 
      
      <tr> 
         <td align = "right"> 
            <label for = "slug">Slug</label> 
         </td> 
         <td align = "left"> 
            <?php echo \Phalcon\Tag::textField(array("slug", "size" => 30)) ?> 
         </td> 
      </tr> 
      
      <tr> 
         <td></td> 
         <td><?php echo \Phalcon\Tag::submitButton(array("Search", "class" => "btn")) ?></td> 
      </tr> 
   
   </table> 
</div>

Edit.phtml

<?php use Phalcon\Tag as Tag; ?>  
<?php echo $this->getContent(); ?>  
<?php echo \Phalcon\Tag::form("categories/save") ?>  

<table width = "100%"> 
   <tr> 
      <td align = "left"><?php echo Tag::linkTo(array("categories", "Back", "class" => "btn")) ?></td> 
      <td align = "right"><?php echo Tag::submitButton(array("Save", "class" => "btn")) ?></td> 
   <tr> 
</table>  

<div align = "center"> 
   <h1>Edit categories</h1> 
</div>  

<table align = "center"> 
   <tr> 
      <td align = "right"> 
         <label for = "name">Name</label> 
      </td> 
      <td align = "left"> 
         <?php echo Tag::textField(array("name", "size" => 30)) ?> 
      </td> 
   </tr> 
   
   <tr> 
      <td align = "right"> 
         <label for = "slug">Slug</label> 
      </td> 
      <td align = "left"> 
         <?php echo Tag::textField(array("slug", "size" => 30)) ?> 
      </td> 
   </tr> 

</table>

Search.phtml

<?php $this->getContent(); ?>  
<table width = "100%"> 
   <tr> 
      <td align = "left"> 
         <?php echo \Phalcon\Tag::linkTo(array("categories/index", "Go Back", "class" => "btn")); ?> 
      </td> 
      
      <td align = "right"> 
         <?php echo \Phalcon\Tag::linkTo(array("categories/new", "Create a Category", "class" => "btn")); ?> 
      </td> 
   <tr> 
</table>  

<table class = "browse table" align = "center" width = "60%"> 
   <thead> 
      <tr> 
         <th>Id</th> 
         <th>Name</th> 
         <th>Slug</th> 
      </tr> 
   </thead> 

   <tbody> 
      <?php 
      if(isset($page->items)) { 
         foreach($page->items as $categories) { ?> 
            <tr> 
               <td>
                  <?php echo $categories->id ?>
               </td> 
               
               <td>
                  <?php echo $categories->name ?>
               </td> 
               
               <td>
                  <?php echo $categories->slug ?>
               </td> 
               
               <td>
                  <?php echo \Phalcon\Tag::linkTo(array("categories/edit/".$categories->id, "Edit")); ?>
               </td> 
               
               <td>
                  <?php echo \Phalcon\Tag::linkTo(array("categories/delete/".$categories->id, "Delete")); ?>
               </td> 
            </tr> 
         <?php } 
      } ?> 
   </tbody> 
</table>  

<div class = "pagination" align = "center"> 
   <ul> 
      <li><?php echo \Phalcon\Tag::linkTo("categories/search", "First") ?></li> 
      <li><?php echo \Phalcon\Tag::linkTo("categories/search?page = ".$page>before, "Previous") ?></li> 
      <li><?php echo \Phalcon\Tag::linkTo("categories/search?page = ".$page>next, "Next") ?></li> 
      <li><?php echo \Phalcon\Tag::linkTo("categories/search?page = ".$page>last, "Last") ?></li> 
   </ul> 
</div>

La lógica empresarial se desarrolla en CategoriesController que incluye métodos para crear, editar y buscar categorías.

<?php  
use \Phalcon\Tag as Tag, \Phalcon\Mvc\Model\Criteria;  

class CategoriesController extends ControllerBase {  
   public function indexAction() { 
      $this->session->conditions = null; 
   }  
   public function searchAction() {  
      $numberPage = 1; 
      if ($this->request->isPost()) { 
         $query = Criteria::fromInput($this->di, "Categories", $_POST); 
         $this->session->conditions = $query->getConditions(); 
      } else { 
         $numberPage = $this->request->getQuery("page", "int"); 
         if ($numberPage <= 0) { 
            $numberPage = 1; 
         } 
      }    
      $parameters = array(); 
         if ($this->session->conditions) { 
            $parameters["conditions"] = $this->session->conditions; 
         } 
      // $parameters["order"] = "id";  
      $categories = Categories::find($parameters); 
         if (count($categories) == 0) { 
            $this->flash->notice("The search did not find any categories"); 
            return $this->dispatcher->forward(array(
               "controller" => "categories", "action" => "index" 
            )); 
         }  
      $paginator = new \Phalcon\Paginator\Adapter\Model(array( 
         "data" => $categories, "limit"=> 10, "page" => $numberPage 
      )); 
      $page = $paginator->getPaginate();  
      $this->view->setVar("page", $page); 
   }  
   public function newAction() {  
   }  
   public function editAction($id) { 
      $request = $this->request; 
      
      if (!$request->isPost()) {  
         $categories = Categories::findFirst(array( 
            'id = :id:', 'bind' => array('id' => $id) 
         )); 
         if (!$categories) { 
            $this->flash->error("The category was not found"); 
            return $this->dispatcher->forward(array( 
               "controller" => "categories", "action" => "index" 
            )); 
         } 
         $this->view->setVar("id", $categories->id);  
         Tag::displayTo("id", $categories->id); 
         Tag::displayTo("name", $categories->name); 
         Tag::displayTo("slug", $categories->slug); 
      } 
   } 
   public function createAction() { 
      if (!$this->request->isPost()) { 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "index" 
         )); 
      }  
      $categories = new Categories(); 
      $categories->id = $this->request->getPost("id"); 
      $categories->name = $this->request->getPost("name"); 
      $categories->slug = $this->request->getPost("slug"); 
      
      if (!$categories->save()) { 
         foreach ($categories->getMessages() as $message) { 
            $this->flash->error((string) $message); 
         } 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "new" 
         )); 
      } else { 
         $this->flash->success("The category was created successfully"); 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "index" 
         )); 
      } 
   }  
   public function saveAction() { 
      if (!$this->request->isPost()) { 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "index" 
         )); 
      }  
      $category = Categories::findFirst(array( 
         'id = :id:', 'bind' => array('id' => $this->request->getPost("id")) 
      )); 
      if (!$category) { 
         $this->flash->error("The category does not exist"); 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "index" 
         )); 
      }  
      $categories->id = $this->request->getPost("id"); 
      $categories->name = $this->request->getPost("name"); 
      $categories->slug = $this->request->getPost("slug");  
      
      if (!$categories->save()) { 
         foreach ($categories->getMessages() as $message) { 
            $this->flash->error((string) $message); 
         } 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "edit", "params" => array($categories->id) 
         ));  
      } else { 
         $this->flash->success("categories was updated successfully"); 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "index" 
         )); 
      } 
   }  
   public function deleteAction($id) { 
      $categories = Categories::findFirst(array( 
         'id = :id:', 'bind' => array('id' => $id) 
      )); 
      if (!$categories) { 
         $this->flash->error("The category was not found"); 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "index" 
         )); 
      }  
      if (!$categories->delete()) { 
         foreach ($categories->getMessages() as $message) { 
            $this->flash->error((string) $message); 
         } 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "search" 
         )); 
      } else { 
         $this->flash->success("The category was deleted"); 
         return $this->dispatcher->forward(array( 
            "controller" => "categories", "action" => "index" 
         )); 
      } 
   } 
}

El código anterior produce la siguiente salida.

La lista de categorías se mostrará como se ve en la siguiente captura de pantalla.