Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2.  
  3. Class PostsController extends AppController {
  4.  
  5.     var $name = 'Posts';
  6.     var $helpers = array('Time');
  7.     var $layout = 'default';
  8.    
  9.    
  10.     function index(){
  11.         # tanpa pagination
  12.         #$this->data = $this->Post->find('all');
  13.        
  14.         # dengan pagination
  15.         $this->paginate = array(
  16.             'limit' => 2,
  17.             'order' => 'Post.id DESC',
  18.         );
  19.         $this->data = $this->paginate('Post');
  20.        
  21.        
  22.         # log akan ditulis ke fail activity.log
  23.         $ip = $_SERVER['REMOTE_ADDR'];
  24.         $this->log("User view Post index from $ip",'activity');
  25.     }
  26.    
  27.     function add(){
  28.         if($this->data){
  29.             # render false
  30.             #debug( $this->data );
  31.             #$this->autoRender = false;
  32.             #die();
  33.            
  34.             # cuba save ke db
  35.             if($this->Post->save($this->data) ){
  36.            
  37.                 $this->Session->setFlash('Post added');
  38.                 $this->redirect('index');
  39.             }else{
  40.                 $this->data['errors'] = $this->Post->invalidFields();
  41.             }
  42.         }
  43.    
  44.         # keluarkan category untuk dropdown
  45.         #$categories= $this->Post->Category->find('list');
  46.         #$this->set('categories',$categories);
  47.         $this->set('categories', $this->Post->Category->find('list'));
  48.        
  49.        
  50.         # gunakan add.bk
  51.         $this->render('add2');
  52.                
  53.     }
  54.  
  55.     function view($id=null){
  56.  
  57.         $this->data = $this->Post->findById($id);
  58.     }
  59.  
  60.     function edit($id =  null){
  61.        
  62.         # user cuba update data
  63.         if($this->data){
  64.             if( $this->Post->save( $this->data )) {
  65.                 $this->Session->setFlash('Updated');
  66.                 $this->redirect('index');
  67.             } else {
  68.                 $this->Session->setFlash('Failed to update');
  69.                 $this->data['errors'] = $this->Post->invalidFields();
  70.             }
  71.         } // tamat form update
  72.    
  73.    
  74.         # checking kalau tak passing id
  75.         # redirect ke index
  76.         if(empty($id)){
  77.             $this->Session->setFlash('Invalid id');
  78.             $this->redirect('index');
  79.         }
  80.        
  81.         # fetch data dari db untuk bagi ke FORM
  82.         # var $this->data akan digunakan dalam view
  83.         # secara automatik
  84.         $this->data = $this->Post->findById($id);
  85.         $this->set('categories', $this->Post->Category->find('list'));
  86.        
  87.        
  88.         # check data wujud dalam database ?
  89.         if( empty( $this->data )){
  90.             # log kan ke file
  91.             # APP/tmp/error.log
  92.             $ip = $_SERVER['REMOTE_ADDR'];
  93.             $this->log("Post Invalid id from $ip");
  94.             # log akan ditulis ke fail activity.log
  95.             #$this->log("Post Invalid id from $ip",'activity');
  96.  
  97.             # bagi mesej dan redirect
  98.             $this->Session->setFlash('Data does not exist');
  99.             $this->redirect('index');
  100.            
  101.         }
  102.         #debug($this->data);
  103.     }
  104.    
  105.     function delete($id = null){
  106.    
  107.         if($this->Post->delete($id)){
  108.             # delete data berjaya
  109.             $this->Session->setFlash("Post #$id deleted");
  110.         } else {
  111.             $this->Session->setFlash("Post #$id deletion failed");
  112.         }
  113.         $this->redirect('index');
  114.     }
  115.    
  116.    
  117.     function search(){
  118.         $query = null;
  119.         $category_id = null;
  120.        
  121.         # listkan category
  122.         $this->set('categories', $this->Post->Category->find('list'));
  123.  
  124.         if($this->data || $this->passedArgs){
  125.             if($this->data){
  126.                 if(!empty($this->data['Search']['category_id'])){
  127.                     $category_id = $this->data['Search']['category_id'];
  128.                 }
  129.                 if(!empty($this->data['Search']['query'])){
  130.                     $query = $this->data['Search']['query'];
  131.                 }
  132.             }
  133.             if($this->passedArgs){
  134.                 if(!empty($this->passedArgs['category_id'])){
  135.                     $category_id = $this->passedArgs['category_id'];
  136.                 }
  137.                 if(!empty($this->passedArgs['query'])){
  138.                     $query = $this->passedArgs['query'];
  139.                 }
  140.             }          
  141.             # jika $category_id == 0,
  142.             # maksudnya user nak search semua category
  143.  
  144.             if($category_id == 0 || $category_id == null) {    
  145.                
  146.                 # search guna conditions
  147.                 $options = array(
  148.                     'conditions' => array(
  149.                                         array(  'or' => array(
  150.                                                     'Post.title   LIKE' => "%$query%",
  151.                                                     'Post.summary LIKE' => "%$query%",
  152.                                                     'Post.content LIKE' => "%$query%",
  153.                                                     //'Post.category_id ' => $category_id,
  154.                                    
  155.                                                         ), // or
  156.                                                        
  157.                                         ),
  158.                                     )
  159.                 );
  160.                
  161.             } else {
  162.  
  163.                 $options = array(
  164.                     'conditions' => array(
  165.                                         array(  'or' => array(
  166.                                                     'Post.title   LIKE' => "%$query%",
  167.                                                     'Post.summary LIKE' => "%$query%",
  168.                                                     'Post.content LIKE' => "%$query%",
  169.                                                     //'Post.category_id ' => $category_id,
  170.                                                         ), // or
  171.                                                 'and' => array('Post.category_id ' => $category_id),           
  172.                                                        
  173.                                         ),
  174.                                     )
  175.                 );
  176.                
  177.            
  178.             } // tamat checking category
  179.  
  180.             $this->paginate = array(
  181.                 'limit' => 1,
  182.                 'order' => 'Post.id DESC',
  183.             );
  184.             $results =  $this->paginate('Post', $options['conditions']);
  185.             $this->set(compact('results','query','category_id'));
  186.         }
  187.     }
  188. } // tamat class