04.01 php saved
Vangel
Tags add more
Articles aka Posts c  
Note
Artiicles controller
  1. <?php
  2.  
  3. class ArticlesController extends AppController {
  4.    
  5.     var $name = 'Articles';
  6.     var $helpers = array('Html', 'Form', 'Javascript', 'Fck');
  7.    
  8. //  var $view = 'Article';   
  9.     function index() {
  10.         $this->Article->recursive = 0;
  11.         $this->set('articles', $this->paginate());
  12.     }
  13.  
  14.     function view($id = null) {
  15.         if (!$id) {
  16.             $this->Session->setFlash(__('Invalid Article.', true));
  17.             $this->redirect(array('action'=>'index'));
  18.         }
  19.         $this->set('article', $this->Article->read(null, $id));
  20.         //$this->Session->read('lang');
  21.     }
  22.  
  23.     function add() {
  24.         if (!empty($this->data)) {
  25.             $this->Article->create();
  26.             if ($this->Article->save($this->data)) {
  27.                 $this->Session->setFlash(__('The Article has been saved', true));
  28.                 //$this->redirect(array('action'=>'index'));
  29.             } else {
  30.                 $this->Session->setFlash(__('The Article could not be saved. Please, try again.', true));
  31.             }
  32.         }
  33.         $sections = $this->Article->Section->find('list');
  34.         $this->set(compact('sections'));
  35.     }
  36.  
  37.     function edit($id = null) {
  38.         if (!$id && empty($this->data)) {
  39.             $this->Session->setFlash(__('Invalid Article', true));
  40.             $this->redirect(array('action'=>'index'));
  41.         }
  42.         if (!empty($this->data)) {
  43.             if ($this->Article->save($this->data)) {
  44.                 $this->Session->setFlash(__('The Article has been saved', true));
  45.                 $this->redirect(array('action'=>'index'));
  46.             } else {
  47.                 $this->Session->setFlash(__('The Article could not be saved. Please, try again.', true));
  48.             }
  49.         }
  50.         if (empty($this->data)) {
  51.             $this->data = $this->Article->read(null, $id);
  52.         }
  53.         $sections = $this->Article->Section->find('list');
  54.         $this->set(compact('sections'));
  55.     }
  56.  
  57.     function delete($id = null) {
  58.         if (!$id) {
  59.             $this->Session->setFlash(__('Invalid id for Article', true));
  60.             $this->redirect(array('action'=>'index'));
  61.         }
  62.         if ($this->Article->del($id)) {
  63.             $this->Session->setFlash(__('Article deleted', true));
  64.             $this->redirect(array('action'=>'index'));
  65.         }
  66.     }
  67.  
  68.  
  69.     function admin_index() {
  70.         $this->Article->recursive = 0;
  71.         $this->set('articles', $this->paginate());
  72.     }
  73.  
  74.     function admin_view($id = null) {
  75.         if (!$id) {
  76.             $this->Session->setFlash(__('Invalid Article.', true));
  77.             $this->redirect(array('action'=>'index'));
  78.         }
  79.         $this->set('article', $this->Article->read(null, $id));
  80.     }
  81.  
  82.     function admin_add() {
  83.         if (!empty($this->data)) {
  84.             $this->Article->create();
  85.             if ($this->Article->save($this->data)) {
  86.                 $this->Session->setFlash(__('The Article has been saved', true));
  87.                 $this->redirect(array('action'=>'index'));
  88.             } else {
  89.                 $this->Session->setFlash(__('The Article could not be saved. Please, try again.', true));
  90.             }
  91.         }
  92.         $sections = $this->Article->Section->find('list');
  93.         $this->set(compact('sections'));
  94.     }
  95.  
  96.     function admin_edit($id = null) {
  97.         if (!$id && empty($this->data)) {
  98.             $this->Session->setFlash(__('Invalid Article', true));
  99.             $this->redirect(array('action'=>'index'));
  100.         }
  101.         if (!empty($this->data)) {
  102.             if ($this->Article->save($this->data)) {
  103.                 $this->Session->setFlash(__('The Article has been saved', true));
  104.                 $this->redirect(array('action'=>'index'));
  105.             } else {
  106.                 $this->Session->setFlash(__('The Article could not be saved. Please, try again.', true));
  107.             }
  108.         }
  109.         if (empty($this->data)) {
  110.             $this->data = $this->Article->read(null, $id);
  111.         }
  112.         $sections = $this->Article->Section->find('list');
  113.         $this->set(compact('sections'));
  114.     }
  115.  
  116.     function admin_delete($id = null) {
  117.         if (!$id) {
  118.             $this->Session->setFlash(__('Invalid id for Article', true));
  119.             $this->redirect(array('action'=>'index'));
  120.         }
  121.         if ($this->Article->del($id)) {
  122.             $this->Session->setFlash(__('Article deleted', true));
  123.             $this->redirect(array('action'=>'index'));
  124.         }
  125.     }
  126.    
  127.     function lang($lng = 'en'){
  128.     $data = $this->Article->findAllByLanguage($lng);
  129.     $this->set('data',$data);
  130. }
  131.  
  132.     function body($id = null){
  133.     if (!$id) {
  134.             $this->Session->setFlash(__('Invalid Article.', true));
  135.             $this->redirect(array('action'=>'index'));
  136.         }
  137.         $this->set('article', $this->Article->read('body_en', $id));
  138.     }
  139.  
  140.     function getAllArticles(){
  141.                   // Bad bad function, ignoring you
  142.         // Configure::write('debug', '0');  //set debug to 0 for this function because debugging info breaks the XMLHttpRequest
  143.     // $this->layout = "ajax"; //this tells the controller to use the Ajax layout instead of the default layout (since we're using ajax . . .)
  144.     $articleArray = array();    //this will hold our data from the database.
  145.  
  146.         $count = $this->Article->findCount(); //counts the number of records in User.
  147.         $articleA = $this->Article->findAll(null,'*','published DESC'); //gets all the User records and sorts them by username alphabetically.
  148.  
  149.     $articleArray = Set::extract($articleA, '{n}.title_en')//convert $userArray into a json-friendly format
  150.     $this->set('total', $count)//send total to the view
  151.     $this->set('articles',$articleArray)//s
  152.     }
  153.    
  154.    
  155.     function beforeRender(){
  156.     // Configure::write('Config.language', 'en');
  157.     $this->Session->write('lang','en');
  158.         $this->Articles->Session = $this->Session;
  159.     }
  160. }
  161.  
  162.      
  163.  
  164. ?>
  165.  
Parsed in 0.312 seconds, using GeSHi 1.0.7.14

Modify this Paste