Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //----------------------------------------------
- // interfaces
- //----------------------------------------------
- interface IInjectable {
- public function __construct($injector);
- }
- interface IDebug {
- public function reset();
- public function errorHandler($number, $message, $file, $line);
- public function exceptionHandler($exception);
- public function shutdownHandler();
- public function writeInfo($info, $className, $method);
- public function debugMode($mode);
- }
- interface IDBConn {
- public function connect();
- public function set($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);
- public function get();
- }
- interface IDecorator {
- public function addEventListener($event, $obj, $proc);
- }
- interface IForm {
- }
- interface IDBModel {
- public function getTableName($tabname);
- public function getRecordById($model, $id);
- public function getNRecords($model, $start, $num);
- public function getNumRecs($model);
- public function insertRecord($model, $values);
- public function updateRecord($model, $id, $values);
- public function deleteRecord($model, $id);
- }
- interface IController {
- public function dispatch();
- public function render($retblocks=true, $tmpl='');
- }
- interface IModel {
- public function getSingle($record_id);
- public function getPage($page_id=0);
- public function getAll();
- public function getNumPages();
- public function insertItem();
- public function updateItem($id, $vals=array());
- public function deleteItem($id);
- }
- interface IView {
- public function setup($controllerName, $action, $routes);
- public function render($retblocks, $tmpl );
- }
- //----------------------------------------------
- // base classes
- //----------------------------------------------
- // Injector
- //----------------------------------------------
- class Injector {
- private $_instances = array();
- public function set($instance) {
- array_push($this->_instances, $instance);
- }
- public function get($interface) {
- foreach ($this->_instances as $instance) {
- $className = get_class($instance);
- $refl = new ReflectionClass($className);
- if ($refl->implementsInterface($interface))
- return $instance;
- }
- }
- }
- //----------------------------------------------
- // Decorator
- //----------------------------------------------
- abstract class Decorator implements IDecorator {
- protected $_subscribers =array();
- //-------------------------------
- public function addEventListener($event, $obj, $proc) {
- if (!isset($this->_subscribers[$event]))
- $this->_subscribers[$event]=array();
- $subscriber = array();
- $subscriber['object'] = $obj;
- $subscriber['procedure'] = $proc;
- $this->_subscribers[$event][] = $subscriber;
- }
- //-------------------------------
- protected function dispatchEvent($event) {
- $args = func_get_args();
- array_shift($args);
- $subscribers = $this->_subscribers[$event];
- if (!is_array($subscribers))
- return;
- $s ='';
- foreach ($subscribers as $subscriber) {
- $s .= call_user_func_array(array($subscriber['object'], $subscriber['procedure']), $args);
- }
- return $s;
- }
- }
- //----------------------------------------------
- // Controller
- //----------------------------------------------
- abstract class Controller implements IController {
- private $_debug;
- protected $_model;
- protected $_view;
- protected $_routes;
- protected $_controller_name;
- protected $_action_name;
- protected $_params=array();
- //---------------------------- debug information
- protected function _w($info, $method) {
- $className = get_class($this);
- $this->_debug->writeInfo($info, $className, $method);
- }
- //---------------------------- collect session and input parameters
- private function getSessionParams() {
- if (!isset($_SESSION))
- return;
- foreach ($_SESSION as $key=>$val) {
- foreach ($this->_params as $pkey=>$pval) {
- if ($key==$pkey) {
- $this->_params[$pkey]=$_SESSION[$key];
- }
- }
- }
- $this->_w( $this->_params, __METHOD__);
- }
- //--------------------------------
- private function getInputParams() {
- foreach ($_REQUEST as $key=>$val) {
- foreach ($this->_params as $pkey=>$pval) {
- if ($key==$pkey) {
- $this->_params[$pkey]=$_REQUEST[$key];
- $_SESSION[$pkey] = $this->_params[$pkey];
- }
- }
- }
- }
- //---------------------------------- get related controllers
- public function related() {
- return array();
- }
- //---------------------------------- declare expected GET parameters
- protected function declareParams() {
- $this->_params[$this->_model->_idfield] = '';
- $this->_params['dbkey'] = $this->_model->_idfield;
- $this->_params['filter'.$this->_controller_name] = '';
- }
- //--------------------------------get parameters
- private function getParams() {
- $this->getSessionParams();
- $this->getInputParams();
- if (preg_match('/[A-Za-z0-9_ ]*/', $this->_params['filter'.$this->_controller_name]))
- $this->_model->_filterstr = $this->_params['filter'.$this->_controller_name];
- else
- throw new Exception('filter security violation');
- }
- //---------------------------------------init
- public function __construct($injector, $controllerName, $action) {
- $this->_debug = $injector->get('IDebug');
- $this->_controller_name = $controllerName;
- $this->_action_name = $action;
- $this->_model = $injector->get('IModel');
- $this->_view = $injector->get('IView');
- $func = create_function('$a',' return str_replace("[dbkey]","'.$this->_model->_idfield.'", $a);');
- $this->_routes = array_map ($func, $this->_routes );
- $this->_view ->setup($controllerName, $action, $this->_routes);
- //-----------------params
- $this->declareParams();
- $this->getParams();
- }
- //---------------------------------- properties get handler
- public function __get($name) {
- if (substr($name,0,3)=='p__') {
- $paramName = substr($name,3);
- return $this->get($paramName);
- }
- }
- //-------------------------------- get property
- public function get($name) {
- if (array_key_exists($name, $this->_params))
- return $this->_params[$name];
- else
- return false;
- }
- //---------------------------------- common dispatching operations
- abstract protected function commonDispatch();
- //--------------------------------- set view variable
- protected function set($name, $value) {
- $this->_view->set($name, $value);
- }
- //--------------------------------- set block of view variables
- protected function setAll($el) {
- foreach ($el as $key=>$value) {
- $this->set($key,$value);
- }
- }
- //---------------------------------- dispatch current action
- public function dispatch() {
- $this->commonDispatch();
- //$this->_w($this->_action,__METHOD__);
- if ((int)method_exists($this, $this->_action_name)) {
- call_user_func (array($this, $this->_action_name));
- }
- }
- //-------------------------------------- render view
- public function render($retblocks=true, $tmpl='') {
- return $this->_view->render($retblocks, $tmpl);
- }
- }
- //----------------------------------------------
- // form input
- //----------------------------------------------
- class stdInput {
- private $_type;
- public $_name;
- private $_value;
- private $_values = array();
- private $_label = '';
- private $_labels = array();
- private $_group_caption;
- private $_handlers = array();
- //-------------------------------- constructor
- public function __construct($type, $name, $value, $label='', $group_caption='') {
- $this->_type = $type;
- $this->_name = $name;
- $this->_group_caption = $group_caption;
- //------------------------- values
- $this->setValue($value);
- //------------------------- labels
- if (is_array($label)) {
- for ($i=0; $i<count($label); $i++) {
- $this->_labels[] = $label[$i];
- }
- } else {
- $this->_label = $label;
- }
- }
- //------------------------------------------- set field values
- public function setValue($value) {
- //-------------------- checkboxes and radiogroups
- if (is_array($value)) {
- $this->_values = array();
- for ($i=0; $i<count($value); $i++) {
- $el = array();
- if (substr($value[$i], strlen($value[$i])-1,1)=='*') {
- $el['checked']=true;
- $el['value'] = substr($value[$i],0,strlen($value[$i])-1);
- } else {
- $el['checked']=false;
- $el['value'] = $value[$i];
- }
- $this->_values[] = $el;
- }
- } //------- single components ( text / password / button / submit /reset )
- else {
- $this->_value = $value;
- }
- }
- //------------------------------------------- add handler
- public function addHandler ($handler) {
- $this->_handlers[] = array ('event'=>$handler[0], 'handler'=>$handler[1]);
- }
- //------------------------------ draw input
- public function draw() {
- $s = '';
- $handlers_str = '';
- for ($i=0; $i<count($this->_handlers); $i++) {
- $handlers_str .= $this->_handlers[$i]['event'].'="'.$this->_handlers[$i]['handler'].'" ';
- }
- switch (strtolower($this->_type)) {
- //--------------------------
- case 'text': case 'password': case 'submit':
- case 'reset': case 'button': case 'file': case 'hidden':
- if ($this->_label) {
- $s .= "<label for=\"{$this->_name}\">{$this->_label}</label>";
- }
- $s .= "<input $handlers_str type=\"{$this->_type}\" ".
- "name=\"{$this->_name}\" value=\"{$this->_value}\" />";
- break;
- //--------------------------
- case 'textarea':
- if ($this->_label)
- $s .= "<p>{$this->_label}</p>";
- $s .= "<textarea $handlers_str rows=\"10\" cols=\"90\" name=\"{$this->_name}\">{$this->_value}</textarea><br/>";
- break;
- //--------------------------
- case 'radio': case 'checkbox':
- $s .= "<label for=\"{$this->_name}\">{$this->_group_caption}</label>";
- for ($i=0; $i<count($this->_values); $i++) {
- //$checked = ($this->_values[$i]['checked']) ? 'checked' : '';
- $checked = ($this->_values[$i]['value']==$this->_value) ? 'checked' : '';
- $s .= "<p><input $handlers_str type=\"{$this->_type}\" ".
- " name=\"{$this->_name}\" value=\"{$this->_values[$i]['value']}\" $checked />";
- $s .= $this->_labels[$i].'</p>';
- }
- break;
- //--------------------------
- case 'select':
- $s .= "<label for=\"{$this->_name}\">{$this->_group_caption}</label>";
- $s .= "<select $handlers_str name=\"{$this->_name}\">";
- for ($i=0; $i<count($this->_values); $i++) {
- $selected = ($this->_values[$i]['value']==$this->_value) ? 'selected' : '';
- $s .= "<option value=\"{$this->_values[$i]['value']}\" $selected >";
- $s .= $this->_labels[$i].'</option>';
- }
- $s .= '</select><br/>';
- break;
- }
- return $s;
- }
- }
- //----------------------------------------------
- // form fieldset
- //----------------------------------------------
- class stdFieldset {
- public $fields = array();
- private $legend;
- //------------------------------------ constructor
- public function __construct($legend) {
- $this->legend = $legend;
- }
- //------------------------------
- public function hasField($name) {
- for ($i=0; $i<count($this->fields); $i++) {
- if ($this->fields[$i]->_name == $name)
- return true;
- }
- return false;
- }
- //------------------------------
- public function getField($name) {
- for ($i=0; $i<count($this->fields); $i++) {
- if ($this->fields[$i]->_name == $name)
- return $this->fields[$i];
- }
- return false;
- }
- //------------------------------ add field
- public function addField($type, $name, $value, $label, $group_caption='') {
- $a = new stdInput ($type, $name, $value, $label, $group_caption);
- $this->fields[] = & $a;
- }
- //------------------------------ draw field
- public function draw() {
- $s = "<fieldset><legend>{$this->legend}</legend>";
- for ($i=0; $i<count($this->fields); $i++)
- $s .= $this->fields[$i]->draw();
- $s .= '</fieldset>';
- return $s;
- }
- }
- //----------------------------------------------
- // Form
- //----------------------------------------------
- class stdForm implements IForm {
- private $_debug;
- public $_model;
- protected $_id;
- private $_action_route;
- protected $scripts = array();
- private $fieldsets = array();
- private $hidden_fields = array();
- private $buttons = array();
- //------------------------- debug information
- protected function _w($info, $method) {
- $className = get_class($this);
- $this->_debug->writeInfo($info, $className, $method);
- }
- //------------------------- check if empty
- public function nzz($var, $default) {
- return (isset($var)) ? $var : $default;
- }
- //------------------------- constructor
- public function __construct($injector ) {
- $this->_debug = $injector->get('IDebug');
- $this->_model = $injector->get('IModel');
- $this->_id = 'form_'.rand(10,5000);
- }
- //-------------------------- setup field values
- public function set_field_values($flds) {
- foreach ($flds as $name=>$value) {
- foreach ($this->fieldsets as $key=>$fieldset) {
- if ($fieldset->hasField($name))
- $this->fieldsets[$key]->getField($name)->setValue($value);
- }
- for ($i=0; $i<count($this->hidden_fields); $i++) {
- if ($this->hidden_fields[$i]->_name == $name)
- $this->hidden_fields[$i]->setValue($value);
- }
- for ($i=0; $i<count($this->buttons); $i++) {
- if ($this->buttons[$i]->_name == $name)
- $this->buttons[$i]->setValue($value);
- }
- }
- }
- //-------------------------- set form action route
- public function set_action_route ($action_route) {
- $this->_action_route = $action_route;
- }
- //-------------------------- add fieldset
- public function addFieldset ($legend) {
- $a = new stdFieldset($legend);
- $this->fieldsets[] = & $a;
- return $a;
- }
- //-------------------------- add hidden field
- public function addHidden ($name, $value) {
- $this->hidden_fields[] = new stdInput ('HIDDEN', $name, $value);
- }
- //-------------------------- add button
- public function addButton ($type, $name, $value, $onclick='') {
- if (is_array($onclick)) {
- $this->scripts[$name] = array ('handler'=>$onclick[0], 'script'=>$onclick[1]);
- }
- $a = new stdInput ($type, $name, $value);
- if (is_array($onclick))
- $a->addHandler(array('onclick', $onclick[0]));
- $this->buttons[] = & $a;
- }
- //--------------------------- draw header
- private function drawHeader() {
- $s = '';//<script type="text/javascript">';
- foreach ($this->scripts as $script) {
- $s .= $script['script'];
- }
- //$s .= '</script>';
- $subm = (isset($this->scripts['onsubmit'])) ?
- 'onsubmit="'.$this->scripts['onsubmit']['handler'].'"':'';
- $s .= '<form '.$subm.' id="'.$this->_id.'" action="'.$this->_action_route.'" method="post" '.
- ' enctype="application/x-www-form-urlencoded" >';
- return $s;
- }
- //--------------------------- draw footer
- private function drawFooter() {
- return '</form>';
- }
- //--------------------------- draw form
- public function draw() {
- $s = '';
- $s .= $this->drawHeader();
- $func = create_function('$a',' echo $a->draw();');
- ob_start();
- array_walk($this->fieldsets, $func);
- array_walk($this->hidden_fields, $func);
- array_walk($this->buttons, $func);
- $s .= ob_get_contents();
- ob_end_clean();
- $s .= $this->drawFooter();
- return $s;
- }
- }
- //----------------------------------------------
- // View
- //----------------------------------------------
- abstract class View implements IView, IInjectable {
- private $_debug;
- protected $_decorator;
- public $_form;
- protected $variables = array();
- protected $_controller_name;
- protected $_action_name;
- protected $_routes;
- protected $_blocks =array('header'=>'','content'=>'','sidebar'=>'','footer'=>'');
- protected $_stdtemplate = '<div id="%viewid%">
- <div class="header">%header%</div>
- <div class="content">%content%</div>
- <div class="sidebar">%sidebar%</div>
- <div class="footer">%footer%</div></div>';
- //---------------------------- debug information
- protected function _w($info, $method) {
- $className = get_class($this);
- $this->_debug->writeInfo($info, $className, $method);
- }
- //------------------------- constructor
- public function __construct($injector) {
- $this->_debug = $injector->get('IDebug');
- $this->_decorator = $injector->get('IDecorator');
- $this->_form = $injector->get('IForm');
- }
- //------------------------- transfer information from controller
- public function setup($controllerName, $action, $routes) {
- $this->_controller_name = $controllerName;
- $this->_action_name = $action;
- $this->_routes = $routes;
- }
- //------------------------- func calls handler
- public function __call($name, $arguments) {
- //$this->_w(array('name'=>$name, 'args'=>$arguments),__METHOD__);
- if (substr($name, 0, 6 )=='route_') {
- $routeName = substr ($name, 6);
- return $this->getRoute($routeName, isset($arguments[0]) ? $arguments[0] :'');
- }
- }
- //----------------------------------- properties get handler
- public function __get($name) {
- if (substr($name,0,2)=='v_') {
- $varName = substr($name, 2);
- return $this->get($varName);
- }
- }
- //------------------------- build routes
- protected function getRoute($s, $param='') {
- $route = $this->_routes[$s];
- //$this->_w(array('routeName'=>$s, 'route'=>$route),__METHOD__);
- if (is_array($param)) {
- $zpar = array();
- foreach ($param as $key=>$value) {
- $zpar[] = & $param[$key];
- }
- array_unshift($zpar, $route);
- $route = call_user_func_array('sprintf', $zpar);
- }
- else
- $route = sprintf($route, $param);
- return $_SERVER["PHP_SELF"].'?controller='.$this->_controller_name.'&'.$route;
- }
- //------------------------- set variables
- public function set($name, $value) {
- $this->variables[$name] = $value;
- }
- //------------------------- get variables
- public function get($name) {
- if (array_key_exists($name, $this->variables))
- return $this->variables[$name];
- else
- return false;
- }
- //-------------- common rendering operations and filling blocks
- abstract protected function renderBlocks();
- //------------------------- output view to template
- private function output($tmpl) {
- if ($tmpl=='')
- $tmpl= $this->_stdtemplate;
- $c = get_class($this);
- $c = strtolower(substr($c, 0, strpos($c,'View')));
- $blocks = array_values($this->_blocks);
- array_unshift($blocks , $c);
- $s = str_replace (array('%viewid%','%header%','%content%','%sidebar%','%footer%'), $blocks, $tmpl);
- return $s;
- }
- //------------------------- render view
- public function render($retblocks, $tmpl ) {
- if (method_exists($this, $this->_action_name))
- call_user_func(array($this, $this->_action_name));
- $this->renderBlocks();
- if ($retblocks) {
- $c = get_class($this);
- $pref = substr($c,0, strpos($c,'View'));
- $prefixf = create_function('$a', 'return "'.$pref.'_".$a;');
- return
- array_combine(
- array_map($prefixf, array_keys($this->_blocks)),
- array_values($this->_blocks));
- }
- else
- return $this->output($tmpl);
- }
- }
- //----------------------------------------------
- // Model
- //----------------------------------------------
- abstract class Model implements IModel, IInjectable {
- private $_db_model;
- public $_table='';
- public $_idfield='';
- public $_orderstr='';
- public $_filterstr='';
- public $fields = array(); // fn, ft, fc
- public $_postvars = array();
- public $_mandatory = array();
- private $_autovars = array();
- protected $_injector;
- public $rec_per_page=50;
- //-------------------------------- get post vars
- private function getPostVars() {
- foreach ($_POST as $key=>$val) {
- foreach ($this->_postvars as $pkey=>$pval) {
- if ($key==$pkey) {
- $this->_postvars[$pkey] = $val;
- }
- }
- }
- }
- //-------------------------------- set auto vars
- abstract protected function setAutoVars();
- //-------------------------------- prepare to write to database
- private function prepareInfo() {
- $this->getPostVars();
- //$this->_w($this->_postvars,__METHOD__);
- $this->setAutoVars();
- $zff = create_function ('$a','return $a[\'fn\'];');
- $zfn = create_function ('$a','return is_array($a[\'fc\']) ? $a[\'fs\'] : $a[\'fc\'];');
- $titles = array_combine(array_map($zff, $this->fields), array_map($zfn, $this->fields));
- $errs = array();
- foreach ($this->_postvars as $key=>$value) {
- if (isset($this->_mandatory[$key])) {
- $z = $this->_mandatory[$key];
- if (isset($z['funcs'])) {
- foreach ($z['funcs'] as $func) {
- $fn = create_function('$a',$func);
- $value = call_user_func($fn, $value);
- }
- }
- if (isset($z['checks'])) {
- foreach ($z['checks'] as $check) {
- $fn = create_function('$a', $check['func']);
- if (call_user_func($fn, $value)==false) {
- $errs[] = sprintf( $check['message'], $titles[$key]);
- }
- }
- }
- $this->_postvars[$key] = $value;
- }
- }
- if (count($errs)>0)
- return implode('<br/', $errs);
- return array_merge($this->_postvars, $this->_autovars);
- }
- //-------------------------------- constructor
- public function __construct($injector) {
- $this->_injector = $injector;
- $this->_db_model = $injector->get('IDBModel');
- $this->_table = $this->_db_model->getTableName($this->_table);
- }
- //-------------------------------- set properties handler
- public function __set($name, $value) {
- if (substr($name,0,3)==='a__') {
- $varName = substr($name,3);
- $this->_autovars[$varName] = $value;
- }
- }
- //-------------------------------- get single item
- public function getSingle($record_id) {
- $el = $this->_db_model->getRecordById($this, $record_id);
- $out = array();
- foreach ($el as $key=>$val) {
- //--------------------------------
- foreach ($this->fields as $fld) {
- if ($fld['fn']==$key) {
- if (array_key_exists($fld['fn'], $this->_postvars)) {
- $out[$key] = $val;
- }
- break;
- }
- }
- //--------------------------------
- if ($key==$this->_idfield) {
- $out['idfield'] = $val;
- }
- }
- return $out;
- }
- //-------------------------------- get page
- public function getPage($page_id=0) {
- if (!is_numeric($page_id))
- $page_id = 0;
- $els = $this->_db_model->getNRecords($this, $page_id * $this->rec_per_page, $this->rec_per_page);
- $out = array();
- foreach ($els as $el) {
- $out_row = array();
- foreach ($el as $key=>$val) {
- //-------------------------
- foreach ($this->fields as $fld) {
- if ($fld['fn']==$key) {
- //-------------------------
- if (array_key_exists($key, $this->_listvars)) {
- $outel = array();
- if (isset($fld['fl'])) {
- $pos = array_search($val, $fld['fl']);
- if ($pos===false)
- $outel['value'] = $val;
- else
- $outel['value'] = $fld['fc'][$pos];
- $outel['caption'] = $fld['fs'];
- } else {
- $outel['value'] = $val;
- $outel['caption'] = $fld['fc'];
- }
- $out_row[$key] = $outel;
- }
- //-------------------------
- break;
- }
- }
- //-------------------------
- if ($key==$this->_idfield) {
- $out_row['idfield']=$val;
- }
- //------------------------
- }
- $out[] = $out_row;
- }
- return $out;
- }
- //-------------------------------- get all
- public function getAll() {
- return $this->_db_model->getNRecords($this, 0, 1000);
- }
- //-------------------------------- get number of pages
- public function getNumPages() {
- $nrecs = $this->_db_model->getNumRecs($this);
- return (ceil($nrecs/$this->rec_per_page));
- }
- //-------------------------------- insert item
- public function insertItem() {
- $inf = $this->prepareInfo();
- if (is_string($inf))
- return $inf;
- return $this->_db_model->insertRecord($this, $inf );
- }
- //-------------------------------- update item
- public function updateItem($id, $vals=array()) {
- if (count($vals)>0)
- foreach ($vals as $key=>$val) {
- $inf[$key] = $val;
- } else
- $inf = $this->prepareInfo();
- if (is_string($inf))
- return $inf;
- return $this->_db_model->updateRecord($this, $id, $inf );
- }
- //-------------------------------- delete item
- public function deleteItem($id) {
- return $this->_db_model->deleteRecord($this, $id);
- }
- }
- //----------------------------------------------
- // Form connected to model
- //----------------------------------------------
- class ModelForm extends stdForm {
- protected $_mandatory_fields;
- protected $_field_hints;
- public function __construct($injector) {
- parent::__construct( $injector);
- $fset = $this->addFieldset('');
- foreach ($this->_model->fields as $fld) {
- if (array_key_exists($fld['fn'], $this->_model->_postvars)) {
- if (isset($fld['fh'])) {
- $this->_field_hints[$fld['fn']] = $fld['fh'];
- }
- if (array_key_exists($fld['fn'], $this->_model->_mandatory)) {
- $this->_mandatory_fields[$fld['fn']] = $this->_model->_mandatory[$fld['fn']];
- }
- if (isset($fld['fl'])) {
- $fset->addField('SELECT',$fld['fn'],$fld['fl'],$fld['fc'], $fld['fs']);
- } else
- if ($fld['fc']=='hidden')
- $this->addHidden($fld['fn'],'');
- else
- if ((isset($fld['fs'])) && ($fld['fs']=='textarea'))
- $fset->addField('TEXTAREA', $fld['fn'],'', $fld['fc']);
- else
- $fset->addField('TEXT', $fld['fn'], '', $fld['fc']);
- }
- }
- }
- }
- //----------------------------------------------
- // Easy form draft
- //----------------------------------------------
- class EasyForm extends ModelForm {
- public function __construct($injector) {
- parent::__construct( $injector);
- $fn = create_function('$a,$key','echo "\'".$key."\' : \'".$a."\',";');
- $fn4 = create_function('$a,$key',
- ' if (isset($a["jcheck"])) echo "\'".$key."\' : {
- \'jfunc\' : \'".$a["jcheck"]["func"]."\',
- \'jmessage\' : \'".$a["jcheck"]["message"]."\'
- },";');
- ob_start();
- ?>
- <style type="text/css">
- .tooltip {
- position:absolute;
- padding:10px;
- border-radius: 5px;
- -moz-border-radius: 5px;
- -webkit-border-radius: 5px;
- -o-border-radius: 5px;
- font-size:12px;
- border: solid 1px #999;
- background: #fffff9;
- }
- .errorhint {
- text-align: center;
- color: red;
- }
- </style>
- <script type="text/javascript">
- var data_%id% = {
- 'hints':{
- <?php
- array_walk($this->_field_hints, $fn);
- ?> 'inf': 4},
- 'mandatory':{
- <?php
- array_walk($this->_mandatory_fields, $fn4);
- ?> 'inf': 4},
- }
- //-----------------------------
- var funcs_%id% = {
- //-----------------------------
- init : function () {
- var frm = jQuery('#%id%');
- frm.bind ('submit',
- funcs_%id%.submit
- );
- var hints = data_%id%.hints;
- for (var key in hints) {
- if (key=='inf')
- continue;
- el = jQuery('input[name="'+key+
- '"],select[name="'+key+'"],textarea[name="'+key+'"]',frm);
- el.each (function() {
- jQuery(this).hover(funcs_%id%.hover_in(hints[key]),funcs_%id%.hover_out);
- jQuery(this).bind('click',funcs_%id%.hover_out);
- jQuery(this).bind('click',funcs_%id%.clear_errors);
- })
- }
- },
- //-----------------------------
- clear_errors : function () {
- var frm = jQuery('#%id%');
- jQuery('.errorhint',frm).remove();
- },
- //-----------------------------
- submit : function() {
- funcs_%id%.clear_errors();
- var correct = true;
- var mandatory = data_%id%.mandatory;
- for (var key in mandatory) {
- if (key=='inf')
- continue;
- var el = jQuery('input[name="'+key+
- '"],select[name="'+key+'"],textarea[name="'+key+'"]',jQuery(this));
- var func = mandatory[key].jfunc;
- el.each ( function() {
- var zval = jQuery(this).val();
- var fn4 ='('+func+')(\''+zval.replace(/'/g, "\\'")+'\')';
- console.log(fn4);
- var tezz = eval(fn4);
- if (tezz==false) {
- jQuery(this).after(jQuery('<div/>',{'class':'errorhint'}).html(mandatory[key].jmessage));
- correct = false;
- }});
- }
- return correct;
- },
- //-----------------------------
- hover_in: function(hint) {
- return function() {
- funcs_%id%.show_hint(this, hint);
- }
- },
- //-----------------------------
- hover_out: function() {
- if (this.tmin) {
- clearTimeout(this.tmin);
- }
- },
- //-----------------------------
- show_hint: function(el, hint) {
- if (!el.hintdiv) {
- el.hintdiv = jQuery('<div/>',{'class':'tooltip'}).css('display','none').html(hint);
- jQuery(document.body).append(el.hintdiv);
- el.hintdiv.css('top', jQuery(el).offset().top - 20);
- el.hintdiv.css('left', jQuery(el).offset().left + 400);
- }
- if (el.vis==true) {
- return;
- }
- el.tmin = setTimeout (function () {
- el.vis = true;
- el.hintdiv.fadeIn('slow');
- el.tmout = setTimeout( function() {
- el.hintdiv.fadeOut('slow');
- el.vis = false;
- }, 3000);
- }, 1000);
- }
- //-----------------------------
- }
- //-----------------------------
- jQuery(document).ready (
- funcs_%id%.init
- )
- </script>
- <?php
- $base_action = ob_get_clean();
- $base_action = str_replace(array('%id%'),array($this->_id), $base_action);
- $this->scripts['base'] = array ('handler'=>'base', 'script'=>$base_action);
- $this->addButton('SUBMIT','submit', 'post');
- $this->addButton('BUTTON','cancel', 'cancel');
- }
- //------------------------------
- public function setCancelRoute($route) {
- $this->addHidden('cancelroute',$route);
- }
- }
- //-------------------------------------------------
- // Easy controller draft
- //-------------------------------------------------
- class EasyController extends Controller {
- protected $_routes = array (
- 'add_form' => 'action4=add_form',
- 'insert' => 'action4=insert',
- 'edit_form' => 'action4=edit_form&[dbkey]=%d',
- 'update' => 'action4=update&[dbkey]=%d',
- 'delete_form' => 'action4=delete_form&[dbkey]=%d',
- 'delete' => 'action4=delete&[dbkey]=%d',
- 'view_single' => 'action4=view_single&[dbkey]=%d',
- 'view_page' => 'action4=view_page&pageid=%d');
- //----------------------------------- declare params
- protected function declareParams() {
- parent::declareParams();
- $this->_params['pageid']='';
- }
- //----------------------------------- common dispatch
- protected function commonDispatch() {
- $this->set('nav_numpages', $this->_model->getNumPages());
- $this->set('nav_pageid', $this->p__pageid);
- $this->set('nav_id', $this->get($this->p__dbkey));
- }
- //--------------------------------------------
- protected function set_current_page() {
- $els = $this->_model->getPage($this->p__pageid);
- $this->set('items',$els);
- }
- //--------------------------------------------
- protected function set_single_page() {
- $el = $this->_model->getSingle($this->get($this->p__dbkey));
- $this->set('item', $el);
- }
- //--------------------------------- draw single record
- public function view_single() {
- $this->set_single_page();
- }
- //-------------------------------- draw page
- public function view_page() {
- $this->set_current_page();
- }
- //-------------------------------- insert record
- public function insert() {
- $inf = $this->_model->insertItem();
- if (is_array($inf)) {
- $this->set('message', $inf['message']);
- $this->set('newrecord_id', $inf['rid']);
- } else
- $this->set('message', $inf);
- $this->set('nav_numpages', $this->_model->getNumPages());
- $this->set_current_page();
- }
- //-------------------------------- draw edit form
- public function edit_form() {
- $this->set_single_page();
- }
- //-------------------------------- update record
- public function update() {
- $this->set('message',$this->_model->updateItem($this->get($this->p__dbkey)));
- $this->set_current_page();
- }
- //-------------------------------- delete form
- public function delete_form() {
- $this->set('message', 'really delete record ?');//.$this->get($this->p__dbkey).' ?');
- }
- //-------------------------------- delete record
- public function delete() {
- $this->set('message',$this->_model->deleteItem($this->get($this->p__dbkey)));
- $this->set('nav_numpages', $this->_model->getNumPages());
- $this->set_current_page();
- }
- }
- //----------------------------------------------
- // Easy view draft
- //----------------------------------------------
- class EasyView extends View {
- //---------------------------- put content into blocks
- protected function renderBlocks() {
- $this->_blocks['header'] = $this->_decorator->getHeader();
- $this->_blocks['content'] = $this->_decorator->getContent();
- $this->_blocks['footer'] = $this->_decorator->getFooter();
- }
- //-------------------------------------------
- // action handlers
- //-------------------------------------------
- //--------------------------- show single record
- public function view_single() {
- $this->_decorator->_drawSingle($this);
- $this->_decorator->_drawEditPanel($this);
- }
- //--------------------------- show page
- public function view_page() {
- $this->_decorator->_drawPageEditNav($this);
- }
- //---------------------------- show add form
- public function add_form() {
- $this->_decorator->_drawForm(&$this, 'add');
- }
- //---------------------------- show insert record result
- public function insert() {
- /*$this->_decorator->setMessage( $this->v_message.
- '<br/><a href="'.$this->route_view_single($this->v_newrecord_id).'">view record</a>');*/
- $this->_decorator->setMessage( $this->v_message);
- $this->_decorator->_drawPageEditNav($this);
- }
- //---------------------------- show edit form
- public function edit_form() {
- $this->_decorator->_drawForm(&$this, 'edit');
- }
- //---------------------------- show update record result
- public function update() {
- $this->_decorator->setMessage($this->v_message);
- $this->_decorator->_drawPageEditNav($this);
- }
- //---------------------------- show delete form
- public function delete_form() {
- $this->_decorator->setMessage($this->v_message.'<br/>'.
- '<a href="'.$this->route_delete($this->v_nav_id).'">delete</a><br/>'.
- '<a href="'.$this->route_view_page($this->v_nav_pageid).'">cancel</a>');
- }
- //---------------------------- show delete record result
- public function delete() {
- /*$this->_decorator->setMessage($this->v_message);*/
- $this->_decorator->_drawPageEditNav($this);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement