Advertisement
terorama

test mvc fw

Dec 3rd, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 24.41 KB | None | 0 0
  1. <?php
  2.  
  3. //----------------------------------------------
  4. //               BootStrap
  5. //----------------------------------------------
  6.  
  7.  
  8. define('DB_HOST','');
  9. define('DB_NAME','');
  10. define('DB_USER','');
  11. define('DB_PASSWORD','');
  12.  
  13. DBConn::set(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  14.  
  15. //-------------------------------------
  16.  
  17. $controller = $_GET['controller'];
  18. $action = $_GET['action'];
  19.  
  20.  
  21. $controllerName = $controller;
  22.  
  23. $controller = ucwords($controller);
  24. $model = rtrim($controller,'s');
  25.  
  26. $view = $controller.'View';
  27.  
  28. $controller .= 'Controller';
  29.  
  30.  
  31. $dispatch  = new $controller($model, $view, $controllerName, $action);
  32.  
  33. //----------------------------
  34. $dispatch->dispatch();
  35. //----------------------------
  36. $vblocks = $dispatch->render();
  37.  
  38. ?>
  39. <html><head><title>test mvc page - <?php echo $vblocks['header']; ?></title></head>
  40. <body>
  41.  
  42. <div id="container">
  43.    <div id="header">
  44.       <h1><?php  echo $vblocks['header'];?></h1>
  45.    </div>
  46.  
  47.    <div id="content">
  48.       <?php  echo $vblocks['content']; ?>
  49.    </div>
  50.  
  51.    <div id="sidebar">
  52.       <?php echo $vblocks['sidebar']; ?>
  53.    </div>
  54.    
  55.    <div id="footer">
  56.       <?php echo $vblocks['footer']; ?>
  57.    </div>
  58.  
  59. </body>
  60. </html>
  61.  
  62. <?php
  63.  
  64. //----------------------------------------------
  65. //              base classes
  66. //----------------------------------------------
  67. class DBConn {
  68.    private static $_db=0;
  69.    
  70.    private static $DB_HOST;
  71.    private static $DB_USER;
  72.    private static $DB_PASSWORD;
  73.    private static $DB_NAME;
  74.    
  75.    //----------------------------------
  76.    public static function connect() {
  77.    
  78.       self::$_db = @new mysqli (self::$DB_HOST, self::$DB_USER, self::$DB_PASSWORD, self::$DB_NAME );
  79.      
  80.       if (self::$_db->connect_errno) {
  81.      
  82.          throw new Exception('connection error '.self::$_db->connect_error);
  83.       }
  84.  
  85.    }
  86.    
  87.    //----------------------------------
  88.    public static function set($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME) {
  89.    
  90.       self::$DB_HOST = $DB_HOST;
  91.       self::$DB_USER = $DB_USER;
  92.       self::$DB_PASSWORD = $DB_PASSWORD;
  93.       self::$DB_NAME = $DB_NAME;
  94.    }
  95.    
  96.    //----------------------------------
  97.    public static function get() {
  98.       if (self::$_db==0) {
  99.          self::connect();
  100.       }
  101.      
  102.       return self::$_db;
  103.    }
  104. }
  105.  
  106.  
  107. //----------------------------------------------
  108. //              Controller
  109. //----------------------------------------------
  110. abstract class Controller {
  111.  
  112.    protected $_model;
  113.    protected $_controller;
  114.    protected $_action;
  115.    protected $_view;
  116.    
  117.    protected $_routes;
  118.    
  119.    private $_params=array();
  120.    
  121.    //---------------------------- collect session and input parameters
  122.      
  123.    private function getSessionParams() {
  124.    
  125.       foreach ($_SESSION as $key=>$val) {
  126.          
  127.          foreach ($this->_params as $pkey=>$pval) {
  128.          
  129.             if ($key==$pkey) {
  130.                $this->_params[$pkey]=$_SESSION[$key];
  131.             }
  132.          }
  133.       }
  134.    }
  135.    //--------------------------------
  136.    private function getInputParams() {
  137.      
  138.       foreach ($_REQUEST as $key=>$val) {
  139.          
  140.          foreach ($this->_params as $pkey=>$pval) {
  141.            
  142.             if ($key==$pkey) {
  143.                $this->_params[$pkey]=$_REQUEST[$key];
  144.                
  145.                $_SESSION[$pkey] = $this->_params[$pkey];
  146.             }
  147.          }
  148.       }
  149.    }
  150.    
  151.    //---------------------------------- declare expected GET parameters
  152.    
  153.    abstract protected function declareParams();
  154.    
  155.    //--------------------------------get parameters
  156.    private function getParams() {
  157.    
  158.       $this->getSessionParams();
  159.       $this->getInputParams();
  160.    }
  161.    
  162.    
  163.    //---------------------------------------init
  164.    
  165.    public function __construct($model, $view, $controller, $action) {
  166.      
  167.       $this->_controller = $controller;
  168.       $this->_model = $model;
  169.       $this->_action = $action;
  170.       $this->_view = $view;
  171.      
  172.       $this->$model = & new $model;
  173.       $this->$view = & new $view($action, $controller, $this->_routes);
  174.      
  175.       //-----------------params
  176.       $this->declareParams();
  177.       $this->getParams();
  178.    }
  179.    
  180.    //----------------------------------   properties get handler
  181.    public function __get($name) {
  182.    
  183.       if (substr($name,0,3)=='p__') {
  184.      
  185.          $paramName = substr($name,3);
  186.          
  187.          if (array_key_exists($paramName, $this->_params))
  188.          
  189.             return $this->_params[$paramName];
  190.          else
  191.             return false;
  192.       }
  193.    }
  194.    
  195.    //----------------------------------   common dispatching operations
  196.    
  197.    abstract protected function commonDispatch();
  198.    
  199.    //----------------------------------   dispatch current action
  200.    public function dispatch() {
  201.    
  202.       $this->commonDispatch();
  203.      
  204.       if ((int)method_exists($dispatch, $this->_action)) {
  205.  
  206.          call_user_func (array($dispatch, $this->_action));
  207.       }
  208.    }
  209.    
  210.    //---------------------------------   set view variable
  211.    protected function set($name, $value) {
  212.      
  213.       $this->$view->set($name, $value)
  214.    }
  215.    //---------------------------------   set block of view variables
  216.    protected function setAll($el) {
  217.    
  218.       foreach ($el as $key=>$value) {
  219.          $this->set($key,$value);
  220.       }
  221.    }
  222.    
  223.    //----------------------------   render view
  224.    function render() {
  225.    
  226.       return $this->$view->render();
  227.    }
  228. }
  229.  
  230. //----------------------------------------------
  231. //                   View
  232. //----------------------------------------------
  233. abstract class View {
  234.    
  235.    protected $variables = array();
  236.    protected $_action;
  237.    protected $_controller;
  238.    
  239.    private $_routes;
  240.    
  241.    protected $_blocks =array('header','content','sidebar','footer');
  242.  
  243.    //-------------------------   constructor
  244.    
  245.    public function __construct($action, $controller, $routes) {
  246.    
  247.       $this->_action = $action;
  248.       $this->_controller = $controller;
  249.       $this->_routes = $routes;
  250.    }
  251.    
  252.    //-------------------------   func calls handler
  253.    public function __call($name, $arguments) {
  254.    
  255.       if (substr($name, 0, 6 )=='route_') {
  256.      
  257.          $routeName = substr ($name, 6);
  258.          
  259.          return $this->getRoute($routeName, $arguments[0]);
  260.       }
  261.    }
  262.    
  263.    //-----------------------------------   properties get handler
  264.    public function __get($name) {
  265.      
  266.       if (substr($name,0,2)=='v_') {
  267.          
  268.          $varName = substr($name, 2);
  269.          return $this->get($varName);
  270.       }
  271.    }
  272.    
  273.    //-------------------------   build routes
  274.    
  275.    protected function getRoute($s, $param='') {
  276.    
  277.       $route = $this->_routes[$s];
  278.      
  279.       if (is_array($param))
  280.      
  281.          $zpar = array();
  282.          foreach ($param as $key=>$value) {
  283.          
  284.             $zpar[] = & $param[$key];
  285.          }
  286.          array_unshift($zpar, $route);
  287.          
  288.          $route = call_user_func_array('sprintf', $zpar);
  289.       else
  290.          $route = sprintf($route, param);
  291.      
  292.       return $_SERVER["PHP_SELF"].'?controller='.$this->_controller.'&'.$route;
  293.    }
  294.    
  295.    //-------------------------   set variables
  296.    
  297.    public function set($name, $value) {
  298.      
  299.       $this->variables[$name] = $value;
  300.    }
  301.    
  302.    //-------------------------   get variables
  303.    
  304.    private function get($name) {
  305.    
  306.       if (array_key_exists($varName, $this->variables))
  307.      
  308.          return $this->variables[$name];
  309.       else
  310.          return false;
  311.    }
  312.    
  313.    //--------------   common rendering operations and filling blocks
  314.    
  315.     abstract protected function commonRender();
  316.     abstract protected function renderBlocks();
  317.    
  318.    //-------------------------
  319.    public function render() {
  320.    
  321.       $this->commonRender();
  322.    
  323.       if (method_exists($this, $this->_action))
  324.          call_user_func(array($this, $this->_action));
  325.          
  326.       $this->renderBlocks();
  327.          
  328.       return $_blocks;   
  329.    }
  330. }
  331.  
  332. //----------------------------------------------
  333. //                  DBModel
  334. //----------------------------------------------
  335. class DBModel {
  336.    
  337.    private $_db;
  338.    protected $_table='';
  339.    protected $_idfield='';
  340.    protected $fields = array(); // fn, ft
  341.    
  342.    protected $filter = '';
  343.    protected $order = '';
  344.    
  345.    //------------------------------ constructor
  346.    
  347.    protected function __construct() {
  348.  
  349.       $this->_db = DBConn::get();
  350.    }
  351.    
  352.    //------------------------------ check errors
  353.    
  354.    private function checkResult($result, $err) {
  355.    
  356.       if ($result===false)
  357.          throw new Exception ('db error '.$err, 30044);
  358.    }
  359.    //------------------------------ get single record from model table
  360.    
  361.    protected function getRecordById($id) {
  362.    
  363.        $stmt = $this->_db->prepare(
  364.           'select * from '.$this->_table.' where '.$this->_idfield.'=?');
  365.          
  366.        $stmt->bind_param('i', $id);
  367.        
  368.        $result = $stmt->getResult();
  369.        $this->checkResult($result, $stmt->error);
  370.        
  371.        $row = $result->fetch_array(MYSQLI_ASSOC);
  372.        
  373.        $result->close();
  374.        $stmt->close();
  375.        return $row;
  376.     }
  377.  
  378.    //------------------------------ get N records from model table
  379.    
  380.    protected function getNRecords($start, $num) {
  381.      
  382.       $result = $this->_db->query(
  383.          'select * from '.$this->_table.' '.$this->filter.' '.$this->order);
  384.      
  385.       $this->checkResult($result, $this->_db->error);
  386.          
  387.       $result->data_seek($start);
  388.      
  389.       $i=0;
  390.       $ret = array();
  391.       while (($row = $result->fetch_assoc()) && ($i<$num)) {
  392.      
  393.          $ret[] = $row;
  394.          $i++;
  395.       }
  396.       $result->close();
  397.       return $ret;
  398.    }
  399.  
  400.    //------------------------------ get number of records in model table
  401.    
  402.    protected function getNumRecs() {
  403.    
  404.       $result = $this->_db->query('select count(*) from '.$this->_table);
  405.      
  406.       $this->checkResult($result, $this->_db->error);
  407.      
  408.       $inf = $result->fetch_row();
  409.       $result->close();
  410.      
  411.       return $inf[0];
  412.      
  413.    }
  414.    
  415.    //------------------------------ inserting and updating records
  416.    
  417.    protected function processRecord($type='update',$id,$values) {
  418.      
  419.       $flds = array();
  420.       $types = array();
  421.       $invars = array();
  422.      
  423.       $func = create_function ('$a', 'return $a["fn"];');
  424.       $func4 = create_function('$a','return $a."=?"');
  425.      
  426.       $sarr = array_map($func, $this->fields);
  427.      
  428.       foreach ($values as $key=>$value) {
  429.          
  430.          $flds[] = $key;
  431.          $invars[] = & $values[$key];
  432.            
  433.          $types[] = $this->fields[array_search($key, $sarr)]['ft'];
  434.       }
  435.       //--------------------------
  436.       if ($type=='insert') {
  437.      
  438.          $flds_s = implode(',',$flds);
  439.          $valpos = implode(','array_fill(0, count($flds),'?'));
  440.          
  441.       } else {
  442.          
  443.          $fld_s = implode(',' array_map($func4, $flds));
  444.       }
  445.       //--------------------------
  446.       $types_s = implode('',$types);
  447.      
  448.       array_unshift($invars, $types_s);
  449.      
  450.       //---------------------------
  451.       if ($type=='insert')
  452.          $sql = 'insert into '.$this->_table.'('.$flds_s.') values ('.$valpos.')';
  453.       else
  454.          $sql = 'update '.$this->_table.' set '.$fld_s.
  455.           ' where '.$this->_idfield.'='.mysql_real_escape_string($id);
  456.      
  457.       //---------------------------
  458.       $stmt = $this->_db->prepare ($sql);
  459.    
  460.      
  461.       call_user_func_array(array($stmt,'bind_param'), $invars);
  462.      
  463.       $rez = $stmt->execute();
  464.       $this->checkResult($rez, $stmt->error);
  465.      
  466.       $rid = ($type=='insert') ? $stmt->insert_id : $stmt->affected_rows ;
  467.       $stmt->close();
  468.      
  469.       return($rid);
  470.  
  471.    }
  472.    //-------------------------------------- insert record  
  473.    protected function insertRecord($values) {
  474.    
  475.       $rid = $this->processRecord ('insert', 0, $values);    
  476.       return 'record successfully inserted. record id: '.$rid;
  477.    }
  478.    
  479.    //-------------------------------------- update record
  480.    
  481.    protected function updateRecord($id, $values) {
  482.    
  483.       $affected_rows = $this->processRecord('update', $id, $values);
  484.       return 'record successfully updated. affected rows: '.$affected_rows;
  485.    }
  486.    
  487.    //--------------------------------------- delete record
  488.    protected function deleteRecord($id) {
  489.    
  490.       $sql = 'delete from '.$this->_table.' where '.$this->idfield.'=?';
  491.       $stmt = $this->_db->prepare($sql);
  492.      
  493.       $stmt->bind_param('i', $id);
  494.      
  495.       $rez = $stmt->execute();
  496.       $this->checkResult($rez, $stmt->error);
  497.      
  498.       $affected_rows = $stmt->affected_rows;
  499.       $stmt->close();
  500.      
  501.       return ('record successfully deleted. affected rows: '.$affected_rows);
  502.    }
  503.      
  504.    
  505. }
  506.  
  507. //----------------------------------------------
  508. //                  Model
  509. //----------------------------------------------
  510. class Model extends DBModel {
  511.    
  512.    private $_postvars = array();
  513.    private $_autovars = array();
  514.    
  515.    public $rec_per_page=10;
  516.    
  517.    //--------------------------------   get post vars
  518.    private function getPostVars() {
  519.      
  520.       foreach ($_POST as $key=>$val) {
  521.          foreach ($this->_postvars as $pkey=>$pval) {
  522.          
  523.             if ($key==$pkey) {
  524.                $this->_postvars[$pkey] = $val;
  525.             }
  526.          }
  527.       }
  528.    }  
  529.    //--------------------------------   set auto vars
  530.    
  531.    abstract protected function setAutoVars();
  532.    
  533.    //--------------------------------   prepare to write to database
  534.    private function prepareInfo() {
  535.      
  536.       $this->getPostVars();
  537.       $this->setAutoVars();
  538.      
  539.       return  array_merge($this->_postvars, $this->_autovars);
  540.    }
  541.    
  542.    //--------------------------------   constructor
  543.    public function __construct() {
  544.    
  545.       parent::__construct();  
  546.    }  
  547.    //--------------------------------   set properties handler
  548.    public function __set($name, $value) {
  549.      
  550.       if (substr($name,0,3)==='a__') {
  551.      
  552.          $varName = substr($name,3);
  553.          $this->_autovars[$varName] = $value;
  554.       }
  555.    }      
  556.    //--------------------------------   get single item
  557.    
  558.    public function getSingle($record_id) {
  559.    
  560.       return $this->getRecordById($record_id);
  561.    }
  562.    //--------------------------------   get page
  563.    
  564.    public function getPage($page_id=0) {
  565.      
  566.       return $this->getNRecords($page_id * $this->rec_per_page, $this->rec_per_page);
  567.    }
  568.    //--------------------------------   get number of pages
  569.    public function getNumPages() {
  570.      
  571.       $nrecs = $this->getNumRecs();
  572.      
  573.       return (ceil($nrecs/$this->rec_per_page));
  574.    }  
  575.    //--------------------------------   insert item
  576.    public function insertItem() {
  577.          
  578.       return $this->insertRecord($this->prepareInfo());
  579.    }
  580.    //--------------------------------   update item
  581.    public function updateItem($id) {
  582.    
  583.      
  584.       return $this->updateRecord($id, $this->prepareInfo());
  585.    }
  586.    //--------------------------------   delete item
  587.    public function deleteItem($id) {
  588.    
  589.       return $this->deleteRecord($id);
  590.    }
  591. }
  592.  
  593. //----------------------------------------------
  594. //              controllers
  595. //----------------------------------------------
  596.  
  597. class QuestionsController extends Controller {
  598.  
  599.  
  600.    protected $_routes = array (
  601.                        
  602.                         'add_form'     => 'action=add_form',
  603.                         'insert'       => 'action=insert',
  604.                         'edit_form'    => 'action=edit_form&qid=%d',
  605.                         'update'       => 'action=update&qid=%d',
  606.                         'delete_form'  => 'action=delete_form&qid=%d',
  607.                         'delete'       => 'action=delete&qid=%d',
  608.                         'view_single'  => 'action=view_single&qid=%d',
  609.                         'view_page'    => 'action=view_page&qpageid=%d');
  610.                              
  611.    //----------------------------------- declare params
  612.    protected function declareParams() {
  613.    
  614.       $this->_params = array('qid'=>'','qcategory'=>'','qpageid'=>'');
  615.    }
  616.    
  617.    //----------------------------------- common dispatch
  618.    protected function commonDispatch() {
  619.    
  620.      
  621.       $this->set('nav_categoryname', $this->Question->getCategory($this->p__qcategory));
  622.       $this->set('nav_numpages', $this->Question->getNumPages());
  623.      
  624.       $this->set('nav_pageid', $this->p__qpageid);
  625.  
  626.    }
  627.    
  628.    //---------------------------------  draw single record  
  629.    private function view_single() {
  630.    
  631.       $el = $this->Question->getSingle($this->p__qid);
  632.       $this->setAll($el);
  633.      
  634.    }
  635.    //--------------------------------  draw page
  636.    private function view_page() {
  637.    
  638.       $els = $this->Question->getPage($this->p__qpageid);
  639.       $this->set('items',$els);
  640.    }
  641.    
  642.    //--------------------------------  draw add form
  643.    private function add_form() {
  644.    
  645.      
  646.    }
  647.    //--------------------------------  insert record
  648.    private function insert() {
  649.    
  650.       $this->set('message',$this->Question->insertItem());
  651.    }
  652.    //--------------------------------  draw edit form
  653.    private function edit_form() {
  654.    
  655.       $el = $this->Question->getEntry($this->p__qid);
  656.       $this->setAll($el);
  657.      
  658.    }
  659.    //--------------------------------  update record
  660.    private function update() {
  661.    
  662.       $this->set('message',$this->Question->updateItem($this->p__qid));
  663.    }
  664.    //--------------------------------  delete form
  665.    private function delete_form() {
  666.    }
  667.    //--------------------------------  delete record
  668.    private function delete() {
  669.      
  670.       $this->set('message',$this->Question->deleteItem($this->p__qid));
  671.    }
  672. }
  673. //----------------------------------------------
  674.  
  675. //----------------------------------------------
  676.  
  677. //----------------------------------------------
  678.  
  679.  
  680. //----------------------------------------------
  681. //                 models
  682. //----------------------------------------------
  683. class Question extends Model {
  684.  
  685.    private $_table = 'questions';
  686.    private $_idfield = 'qid';
  687.    
  688.    private $fields = array (
  689.       array('fn'=>'qtitle', 'ft'=>'s'),
  690.       array('fn'=>'qtext','ft'=>'s'),
  691.       array('fn'=>'qcategory','ft'=>'i'),
  692.       array('fn'=>'quser','ft'=>'i'),
  693.       array('fn'=>'qdate','ft'=>'s'));
  694.      
  695.    private $_postvars = array ('qtitle', 'qtext', 'qcategory', 'quser');
  696.    
  697.    protected function setAutoVars() {
  698.    
  699.       $this->a__qdate = date('Y-m-d H:i:s');
  700.    }
  701.    
  702.  
  703. }
  704.  
  705. //----------------------------------------------
  706. //               forms
  707. //----------------------------------------------
  708. class QuestionsForm extends stdForm {
  709.    
  710.    public function __construct($action, $flds) {
  711.      
  712.       parent::_construct( array('post','reset','cancel'),'default');
  713.      
  714.       $fset = $this->addFieldset('Question: ');
  715.      
  716.      
  717.       $fset->addField('TEXT', 'qtitle', nzz($flds['qtitle'],''), 'Input question title');
  718.       $fset->addField('TEXT', 'qcontent', nzz($flds['qcontent'],''),'Input question');
  719.       $fset->addField('TEXT', 'qcategory', nzz($flds['qcategory'],''),'Input category');
  720.       $fset->addField('TEXT', 'qauthor', nzz($flds['qauthor'],''),'Input your name');
  721.    }
  722. }
  723.  
  724. //----------------------------------------------
  725. //                views
  726. //----------------------------------------------
  727.  
  728. class QuestionsView extends View {
  729.  
  730.    private $bcblock = '';
  731.    private $maincontent = '';
  732.    private $navblock = '';
  733.    private $catblock = '';
  734.    private $editblock = '';
  735.    private $infoblock = '';
  736.    
  737.    private $_addurl = ''
  738.    
  739.    
  740.    //----------------------------   draw buttons for posts
  741.    private function _drawButtons($record_id) {
  742.    
  743.       $s = '<div class="entryeditpanel">';
  744.       $s .= '<div class="editbutton"><a href="'.$this->route_edit_form($record_id).'"></a></div>';                   
  745.       $s .= '<div class="deletebutton"><a href="'.$this->route_delete_form($record_id).'"></a></div>';                   
  746.       $s .= '</div>';
  747.      
  748.       return $s;
  749.    }
  750.    
  751.    //----------------------------   draw page entry
  752.    
  753.    private function _drawPageEntry($item) {
  754.    
  755.       extract($item);
  756.      
  757.       $s ='';
  758.       $s .='<div class="entrytitle"><a href="'.$this->route_view_single($qid).'">'.$qtitle.'</a></div>';
  759.       $s .='<div class="entrycontent">'.$qcontent.'</div>';
  760.       $s .='<div class="entrycategory">'.$qcategory.'</div>';
  761.       $s .='<div class="entryuser">'.$quser.'</div>';
  762.       $s .='<div class="entrydate">'.$qdate.'</div>';
  763.      
  764.       $s .= $this->_drawButtons($qid);
  765.      
  766.       return $s;
  767.    }
  768.    
  769.    //----------------------------   draw page
  770.    
  771.    private function _drawPage() {
  772.    
  773.       $s = '<div class="page">'  
  774.       $items = $this->get('items');
  775.      
  776.       for ($i=0; $i<count($items); $i++) {
  777.      
  778.          $s .= $this->_drawPageEntry($items[$i]);
  779.       }
  780.       $s .= '</div>';
  781.      
  782.       return $s;
  783.    }
  784.    
  785.    //----------------------------   draw single item
  786.    private function _drawSingle() {
  787.    
  788.       $s =  '<div class="content">'.$this->v_qcontent.'</div>';
  789.       $s .= '<div class="category">'.$this->v_qcategory.'</div>';
  790.       $s .= '<div class="author">'.$this->v_qauthor.'</div>';
  791.       $s .= '<div class="date">'.$this->v_qdate.'</div>';
  792.      
  793.       return $s;
  794.      
  795.    }
  796.    
  797.    //----------------------------   draw form for adding or editing
  798.    
  799.    private function _drawForm($add_or_edit, $flds=null) {
  800.    
  801.       if ($add_or_edit == 'add')
  802.      
  803.          $action = $this->route_insert();
  804.       else
  805.          $action = $this->route_update($this->v_qid);
  806.          
  807.       $zform = new QuestionsForm($action, $flds);
  808.      
  809.       return $zform->draw();
  810.      
  811.    }
  812.    //----------------------------   draw bread crumbs
  813.    
  814.    private function _drawBreadCrumbs() {
  815.    
  816.       return 'Category: '.$this->v_pos_category_name.' Page: '.$this->v_pos_page_id;
  817.                  
  818.    }
  819.    //----------------------------   draw navigation block
  820.    
  821.    private function _drawNavigation() {
  822.    
  823.       $s ='<div class="navigation"><ul>';
  824.      
  825.       for ($i=0; $i < $this->v_nav_numpages; $i++) {
  826.          
  827.          $s .='<li>';
  828.          
  829.          if ($i== $this->v_nav_pageid) {       
  830.             $s .= $i;          
  831.          } else {        
  832.             $s .= '<a href="'. $this->route_view_page($this->v_nav_pageid).'">'.$i.'</a>';       
  833.          }
  834.          
  835.          $s .= '</li>'
  836.       }
  837.       $s .= '</ul></div>';
  838.       return $s;
  839.    }
  840.    
  841.    //----------------------------   draw edit block
  842.    
  843.    private function _drawEditPanel() {
  844.    
  845.       $s = '<div class="editpanel">';
  846.       $s .= '<div class="addbutton"><a href="'.$this->route_add_form().'"></a></div>';
  847.      
  848.       $s .= '<div class="editbutton"><a href="'.
  849.                                   $this->route_edit_form($this->v_qid).'"></a></div>';
  850.                                  
  851.       $s .= '<div class="deletebutton"><a href="'.
  852.                                   $this->route_delete_form($this->v_qid).'"></a></div>';
  853.       $s .= '</div>';
  854.      
  855.       return $s;
  856.      
  857.    }
  858.    
  859.    //-----------------------------   draw page and navigation
  860.    private function _drawPageNav() {
  861.      
  862.       $this->maincontent = $this->_drawPage();
  863.       $this->navblock = $this->_drawNavigation();
  864.    }
  865.    
  866.    
  867.    //----------------------------   draw common parts
  868.    
  869.    protected function commonRender() {
  870.    
  871.       $this->bcblock = $this->drawBreadCrumbs();
  872.      
  873.      
  874.    }
  875.    //----------------------------   put content into blocks
  876.    
  877.    protected function renderBlocks() {
  878.      
  879.       $this->_blocks['content'] = $this->bcblock.$this->infoblock.$this->maincontent;    
  880.       $this->_blocks['sidebar'] = $this->catblock;
  881.       $this->_blocks['footer'] =  $this->editblock. $this->navblock;
  882.    }
  883.    
  884.    //-------------------------------------------
  885.    //                action handlers
  886.    //-------------------------------------------
  887.    
  888.    //---------------------------   show single record
  889.    
  890.    private function view_single() {
  891.    
  892.       $this->_blocks['header'] = $this->v_qtitle;
  893.      
  894.       $this->maincontent = $this->_drawSingle();     
  895.       $this->editblock = $this->_drawEditPanel();
  896.    }
  897.    
  898.     //---------------------------   show page
  899.    
  900.     private function view_page() {
  901.    
  902.        $this->_drawPageNav();
  903.     }
  904.    
  905.    
  906.    //----------------------------   show add form
  907.    
  908.    private function add_form() {
  909.    
  910.       $this->maincontent = $this->_drawForm('add');
  911.    }
  912.    //----------------------------   show insert record result
  913.    
  914.    private function insert() {
  915.    
  916.       $this->infoblock = $this->v_message;
  917.       $this->_drawPageNav();
  918.    }
  919.    
  920.    //----------------------------   show edit form
  921.    
  922.    private function edit_form() {
  923.    
  924.       $flds = array('qtitle'=> $this->v_qtitle,
  925.                     'qcontent'=> $this->v_qcontent,
  926.                     'qcategory'=> $this->v_qcategory,
  927.                     'qauthor'=> $this->v_qauthor);
  928.      
  929.      
  930.       $this->maincontent = $this->_drawForm('edit', $flds);
  931.    }
  932.    
  933.    //----------------------------   show update record result
  934.    private function update() {
  935.    
  936.       $this->infoblock = $this->v_message;
  937.       $this->_drawPageNav();
  938.    }
  939.    
  940.    //----------------------------   show delete form
  941.    
  942.    private function delete_form() {
  943.    }
  944.    
  945.    //----------------------------   show delete record result
  946.    private function delete() {
  947.    
  948.       $this->infoblock = $this->v_message;
  949.       $this->_drawPageNav();
  950.    }
  951. }
  952.  
  953. //----------------------------------------------
  954.  
  955. //----------------------------------------------
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement