Advertisement
terorama

mvc / questionsview.inc.php

Dec 22nd, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.93 KB | None | 0 0
  1. <?php
  2.  
  3. //----------------------------------------------
  4. //            questions form
  5. //----------------------------------------------
  6. class QuestionsForm extends stdForm {
  7.    
  8.    public function __construct($action, $cancel_action, $flds) {
  9.    
  10.       $submit_action = array ('return check_form(this);',
  11.       "\r\n".'function check_form(f) {
  12.          for (var i=0; i<f.elements.length; i++) {
  13.             if (f.elements[i].value=="") {
  14.                alert(f.elements[i].name+" should be filled");
  15.                return false;}
  16.          } return true;}'."\r\n");
  17.      
  18.       parent::__construct( $action, $submit_action);
  19.      
  20.       $fset = $this->addFieldset('Question: ');
  21.      
  22.       $cancel_script = array ('back_to_list();',
  23.          'function back_to_list() {window.location.assign("'.$cancel_action.'");}');
  24.      
  25.      
  26.       $this->addButton('SUBMIT','submit', 'post');
  27.       $this->addButton('BUTTON','cancel', 'cancel', $cancel_script);
  28.      
  29.       //$this->_w($flds, __METHOD__);
  30.      
  31.       $fset->addField('TEXT', 'qtitle', $this->nzz($flds['qtitle'],''), 'Input question title');
  32.       $fset->addField('TEXT', 'qtext', $this->nzz($flds['qtext'],''),'Input question');
  33.       $fset->addField('TEXT', 'qcategory', $this->nzz($flds['qcategory'],''),'Input category');
  34.       $fset->addField('TEXT', 'quser', $this->nzz($flds['quser'],''),'Input your name');
  35.    }
  36. }
  37.  
  38. //----------------------------------------------
  39. //           questions view  
  40. //----------------------------------------------
  41.  
  42. class QuestionsView extends View {
  43.  
  44.    private $bcblock = '';
  45.    private $maincontent = '';
  46.    private $navblock = '';
  47.    private $catblock = '';
  48.    private $editblock = '';
  49.    private $infoblock = '';
  50.    
  51.    private $_addurl = '';
  52.    
  53.    
  54.    //----------------------------   draw buttons for posts
  55.    private function _drawButtons($record_id) {
  56.    
  57.       $s = '<div class="entryeditpanel">';
  58.       $s .= '<div class="editbutton"><a href="'.$this->route_edit_form($record_id).'">edit</a></div>';                   
  59.       $s .= '<div class="deletebutton"><a href="'.$this->route_delete_form($record_id).'">delete</a></div>';                     
  60.       $s .= '</div>';
  61.      
  62.       return $s;
  63.    }
  64.    
  65.    //----------------------------   draw page entry
  66.    
  67.    private function _drawPageEntry($item) {
  68.    
  69.       extract($item);
  70.      
  71.       $s ='';
  72.       $s .='<div class="entrytitle"><a href="'.$this->route_view_single($qid).'">'.$qtitle.'</a></div>';
  73.       $s .='<div class="entrycontent">'.$qtext.'</div>';
  74.       $s .='<div class="entrycategory">'.$qcategory.'</div>';
  75.       $s .='<div class="entryuser">'.$quser.'</div>';
  76.       $s .='<div class="entrydate">'.$qdate.'</div>';
  77.      
  78.       $s .= $this->_drawButtons($qid);
  79.      
  80.       return $s;
  81.    }
  82.    
  83.    //----------------------------   draw page
  84.    
  85.    private function _drawPage() {
  86.    
  87.       $s = '<div class="page">';  
  88.       $items = $this->v_items;
  89.      
  90.       for ($i=0; $i<count($items); $i++) {
  91.      
  92.          $s .= $this->_drawPageEntry($items[$i]);
  93.       }
  94.       $s .= '</div>';
  95.      
  96.       return $s;
  97.    }
  98.    
  99.    //----------------------------   draw single item
  100.    private function _drawSingle() {
  101.    
  102.       $s =  '<div class="content">'.$this->v_qtext.'</div>';
  103.       $s .= '<div class="category">'.$this->v_qcategory.'</div>';
  104.       $s .= '<div class="user">'.$this->v_quser.'</div>';
  105.       $s .= '<div class="date">'.$this->v_qdate.'</div>';
  106.      
  107.       return $s;
  108.      
  109.    }
  110.    
  111.    //----------------------------   draw form for adding or editing
  112.    
  113.    private function _drawForm($add_or_edit, $flds=null) {
  114.    
  115.       if ($add_or_edit == 'add')
  116.      
  117.          $action = $this->route_insert();
  118.       else
  119.          $action = $this->route_update($this->v_qid);
  120.          
  121.       $zform = new QuestionsForm($action, $this->route_view_page($this->v_nav_pageid), $flds);
  122.      
  123.       return $zform->draw();
  124.      
  125.    }
  126.    //----------------------------   draw bread crumbs
  127.    
  128.    private function _drawBreadCrumbs() {
  129.    
  130.       return 'Category: '.$this->v_nav_categoryname.' Page: '.$this->v_nav_pageid;
  131.                  
  132.    }
  133.    //----------------------------   draw navigation block
  134.    
  135.    private function _drawNavigation() {
  136.    
  137.       $s ='<div class="navigation"><ul>';
  138.      
  139.       for ($i=0; $i < $this->v_nav_numpages; $i++) {
  140.          
  141.          $s .='<li>';
  142.          
  143.          if ($i== $this->v_nav_pageid) {       
  144.             $s .= ($i+1);          
  145.          } else {        
  146.             $s .= '<a href="'. $this->route_view_page($i).'">'.($i+1).'</a>';        
  147.          }
  148.          
  149.          $s .= '</li>';
  150.       }
  151.       $s .= '</ul></div>';
  152.       return $s;
  153.    }
  154.    
  155.    //----------------------------   draw edit block
  156.    
  157.    private function _drawEditPanel($single=true) {
  158.    
  159.       $s = '<div class="editpanel">';
  160.       $s .= '<div class="addbutton"><a href="'.$this->route_add_form().'">add question</a></div>';
  161.      
  162.       if ($single) {
  163.      
  164.          $s .= '<div class="editbutton"><a href="'.
  165.                                   $this->route_edit_form($this->v_qid).'">edit question</a></div>';
  166.                                  
  167.          $s .= '<div class="deletebutton"><a href="'.
  168.                                   $this->route_delete_form($this->v_qid).'">delete question</a></div>';
  169.                                  
  170.          $s .= '<div class="backtolistbutton"><a href="'.
  171.                                   $this->route_view_page($this->v_nav_pageid).'">back to list</a></div>';        
  172.       }      
  173.       $s .= '</div>';
  174.      
  175.       return $s;
  176.      
  177.    }
  178.    
  179.    //-----------------------------   draw page and navigation
  180.    private function _drawPageNav() {
  181.      
  182.       $this->maincontent = $this->_drawPage();
  183.       $this->navblock = $this->_drawNavigation();
  184.       $this->editblock = $this->_drawEditPanel(false);
  185.    }
  186.    
  187.    
  188.    //----------------------------   draw common parts
  189.    
  190.    protected function commonRender() {
  191.    
  192.       $this->bcblock = $this->drawBreadCrumbs();
  193.      
  194.      
  195.    }
  196.    //----------------------------   put content into blocks
  197.    
  198.    protected function renderBlocks() {
  199.      
  200.       $this->_blocks['content'] = $this->bcblock.$this->infoblock.$this->maincontent;    
  201.       $this->_blocks['sidebar'] = $this->catblock;
  202.       $this->_blocks['footer'] =  $this->editblock. $this->navblock;
  203.    }
  204.    
  205.    //-------------------------------------------
  206.    //                action handlers
  207.    //-------------------------------------------
  208.    
  209.    //---------------------------   show single record
  210.    
  211.    public function view_single() {
  212.    
  213.       $this->_blocks['header'] = $this->v_qtitle;
  214.      
  215.       $this->maincontent = $this->_drawSingle();     
  216.       $this->editblock = $this->_drawEditPanel();
  217.    }
  218.    
  219.     //---------------------------   show page
  220.    
  221.     public function view_page() {
  222.    
  223.        $this->_drawPageNav();
  224.        
  225.     }
  226.    
  227.    
  228.    //----------------------------   show add form
  229.    
  230.    public function add_form() {
  231.    
  232.       $this->maincontent = $this->_drawForm('add');
  233.    }
  234.    //----------------------------   show insert record result
  235.    
  236.    public function insert() {
  237.    
  238.       $this->infoblock = $this->v_message.
  239.          '<br/><a href="'.$this->route_view_single($this->v_newrecord_id).'">view record</a>';
  240.          
  241.       $this->_drawPageNav();
  242.    }
  243.    
  244.    //----------------------------   show edit form
  245.    
  246.    public function edit_form() {
  247.    
  248.       $flds = array('qtitle'=> $this->v_qtitle,
  249.                     'qtext'=> $this->v_qtext,
  250.                     'qcategory'=> $this->v_qcategory,
  251.                     'quser'=> $this->v_quser);
  252.      
  253.      
  254.       $this->maincontent = $this->_drawForm('edit', $flds);
  255.    }
  256.    
  257.    //----------------------------   show update record result
  258.    public function update() {
  259.    
  260.       $this->infoblock = $this->v_message;
  261.       $this->_drawPageNav();
  262.    }
  263.    
  264.    //----------------------------   show delete form
  265.    
  266.    public function delete_form() {
  267.    
  268.       $this->infoblock = $this->v_message.'<br/>'.
  269.          '<a href="'.$this->route_delete($this->v_nav_qid).'">delete</a><br/>'.
  270.          '<a href="'.$this->route_view_page($this->v_nav_pageid).'">cancel</a>';
  271.          
  272.    }
  273.    
  274.    //----------------------------   show delete record result
  275.    public function delete() {
  276.    
  277.       $this->infoblock = $this->v_message;
  278.       $this->_drawPageNav();
  279.    }
  280. }
  281.  
  282.  
  283. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement