Guest User

Untitled

a guest
Jul 20th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Components;
  4.  
  5. use Nette\Application\UI\Form;
  6. class ArticleMenu extends \Nette\Application\UI\Control  {
  7.  
  8.    
  9.  
  10. /** @var Articles */  
  11. private $articles;  
  12. /** @var string */
  13. public $categoryID;
  14.    
  15.  
  16. /**
  17. * @param Articles $articles
  18.  * @param string $categoryID
  19. */
  20. public function __construct($categoryID, \App\Model\Articles $articles) {
  21.     parent::__construct();
  22.     $this->articles = $articles;
  23.     $this->categoryID = $categoryID;
  24.  
  25.    
  26. }
  27.    
  28.     protected function createComponentArticleMenu() {
  29.        
  30.      $form = new Form();
  31.      
  32.      $form->addSelect('category','Kategorie', $this->articles->getArticleCategories()->fetchPairs('id', 'name'))
  33.     ->setPrompt('VΕ‘echny člΓ‘nky');
  34.      $form->addSubmit('submit', 'Zobrazit');    
  35.      return $form;
  36. }
  37.  
  38.     public function articleMenuSuceeded($form, $categoryID) {
  39.      
  40.  
  41.     $this->articles = !empty($categoryID) ?
  42.      $this->articles->getByCategoryId($categoryID) :
  43.      $this->articles->fetchAll();
  44.      $form->onSuccess[] = $this->articleMenuSuceeded;
  45.      $this->redirect('this', ['categoryID' => $form->values->category]);
  46.      
  47.  
  48.     }
  49.  
  50.     public function render() {
  51.  
  52.        
  53.     $this['articleMenu']->render();
  54.    
  55.    
  56.     }
  57. }
  58.  
  59. interface IArticleMenuFactory {
  60.         /**
  61.      * @param string $categoryID
  62.      * @return ArticleMenu
  63.      */
  64.     function create($categoryID);
  65. }
  66.  
  67. <?php
  68.  
  69.  
  70.  
  71. namespace App\Model;
  72.  
  73. /**
  74.  * Description of Articles
  75.  *
  76.  * @author Freestyler
  77.  */
  78. class Articles extends Base {
  79.    
  80.    
  81.  
  82.     /*
  83.      * Get all articles from db (limited to 10)
  84.      */
  85.     public function fetchAll() {
  86.     return $this->database->table('articles')          
  87.             ->limit('10');
  88.     }
  89.    
  90.     /**
  91.      * Add article to db
  92.      */
  93.     public function addArticle() {
  94.    
  95.    
  96.     }
  97.  
  98.     /**
  99.      *
  100.      * Get all categories
  101.      */
  102.     public function getArticleCategories() {
  103.    
  104.     return $this->database->table('categories');
  105.            
  106.     }
  107.     /*
  108.      * Get specific article based on category_id
  109.      */
  110.    
  111.     public function getArticleByCategoryId($categoryID) {
  112.        
  113.     return $this->database->table('articles')
  114.             ->where('categories_id', $categoryID);
  115.    
  116.    }
  117.    
  118.  
  119. }
  120. config.neon
  121. #
  122. # SECURITY WARNING: it is CRITICAL that this file & directory are NOT accessible directly via a web browser!
  123. #
  124. # If you don't protect this directory from direct web access, anybody will be able to see your passwords.
  125. # http://nette.org/security-warning
  126. #
  127. parameters:
  128.  
  129.  
  130. php:
  131.     date.timezone: Europe/Prague
  132.     # zlib.output_compression: yes
  133.  
  134.  
  135. nette:
  136.     application:
  137.         errorPresenter: Error
  138.         mapping:
  139.            
  140.  
  141.     session:
  142.         expiration: 14 days
  143.  
  144.  
  145. services:
  146.    
  147.     authenticator: App\Model\Authenticate
  148.     - App\RouterFactory
  149.     - App\Model\Articles   
  150.     -  
  151.     implement: IArticleMenuFactory
  152.     parameters: [categoryID]
  153.     arguments: [%categoryID%]
  154.     router: @App\RouterFactory::createRouter
  155.  
  156. <?php
  157.  
  158. namespace AdminModule;
  159.  
  160.  
  161.  
  162. /**
  163.  * Homepage presenter.
  164.  */
  165. class HomepagePresenter extends BasePresenter
  166. {
  167.  
  168.     /**
  169.      * @var App\Components\IArticleMenuFactory
  170.      * @inject
  171.      */
  172.    
  173.     public $articleMenuFactory;
  174.    
  175.     protected function createComponentArticleMenu()
  176.     {
  177.     return $this->articleMenuFactory->create();
  178.  
  179.     }
  180.     /**
  181.       * Fetch all articles to table
  182.       */
  183.     public function beforeRender() {
  184.    
  185.         $this->template->articles = $this->articles->fetchAll();   
  186.    
  187.     }  
  188.    
  189.     }
Advertisement
Add Comment
Please, Sign In to add comment