. if (!defined('EG')) die('Direct access not allowed!'); //class to manage the scaffold of the controller class Scaffold { protected $_type = null; //the type of the scaffold. It can be 'main' or 'form' protected $_queryType = null; //it can be insert or update protected $_primaryKey = null; //the primary key of the table protected $_controller = null; //the name of the controller public $application = null; //the name of the application public $applicationUrl = null; //the url of the application public $controller = null; //the name of the controller public $model = null; //the reference to the model associated with the controller public $viewArgs = array(); //the associative array representing the status args of the main action of the controller. public $params = array(); //associative array containing the parameters of the scaffold public $html = array(); //associative array containing the HTML of the scaffold ('pageList'=>HTML,..) public $mainMenu = null; //the reference to the MenuHelper object public $pageList = null; //the reference to the PageDivisionHelper object public $itemList = null; //the reference to the ListHelper object public $popupMenu = null; //the reference to the PopupHelper object public $form = null; //the reference to the form object public $entries = null; //the entries of the form (string having entries separated by comma) public $values = array(); //the values inserted in the form (taken from the table if $this->queryType === 'update' or if an error occured during the databse query, otherwise taken from the $_POST array) //the list of fields of the select query public $fields = null; //instance of Lang_{language}_Generic public $strings = null; public static $autoParams = array( 'mainAction' => 'main', 'modifyAction' => 'form/update', 'associateAction' => 'associate', 'panelController' => 'panel', 'pageList' => true, 'pageVariable' => 'page', 'recordPerPage' => 20, 'mainMenu' => 'panel,add', 'formMenu' => 'panel,back', 'popup' => false, 'popupType' => 'exclusive' ); public function __construct($type,$application, $controller,$model,$viewArgs,$params = null) { $this->_type = $type; $this->application = $application; $this->controller = $controller; $this->model = $model; $this->viewArgs = $viewArgs; //get the generic language class $this->strings = Factory_Strings::generic(Params::$language); $autoParams = self::$autoParams; if (!array_key_exists("postSubmitValue",$autoParams)) { $autoParams['postSubmitValue'] = $this->strings->gtext('Save'); } //set the $this->scaffold->params array if (is_array($params)) { foreach ($params as $key => $value) { $autoParams[$key] = $value; } } $this->params = $autoParams; $this->applicationUrl = isset($application) ? $application . "/" : null; } //ad some clauses to the select query //whereArray = array ($table_field => $value) public function appendWhereQueryClause($whereArray) { $this->model->appendWhereQueryClause($whereArray); } //set clauses to the select query //whereArray = array ($table_field => $value) public function setWhereQueryClause($whereArray) { $this->model->setWhereQueryClause($whereArray); } //initialize the main scaffold (ListHelper) //$recordList: field of the table to show, $primaryKey: the key of the table public function loadMain($recordList,$primaryKey,$theme = 'edit,del') { $this->_primaryKey = $primaryKey; if (is_array($recordList) or strcmp($recordList,'') !== 0) { $recordListArray = is_array($recordList) ? $recordList : explode(',',$recordList); foreach ($recordListArray as $record) { if (preg_match('/\[\[checkbox\]\](\;)(.*?)(\;)/',$record,$matches)) { $this->itemList->addItem("checkbox",encode($matches[2]),";".$matches[2].";","",";".$primaryKey.";"); } else if (preg_match('/\[\[checkbox\:(.*?)\]\](\;)(.*?)(\;)/',$record,$matches)) { $this->itemList->addItem("checkbox",encode($matches[3]),";".$matches[3].";",$matches[1],";".$primaryKey.";"); } else if (preg_match('/\[\[input\]\](\;)(.*?)(\;)/',$record,$matches)) { $this->itemList->addItem("input",encode($matches[2]),";".$matches[2].";",";".$primaryKey.";"); } else if (preg_match('/\[\[ledit\]\](\;)(.*?)(\;)/',$record,$matches)) { $this->itemList->addItem("link",$this->applicationUrl . $this->controller.'/'.$this->params['modifyAction']."/;$primaryKey;","",";".$matches[2].";"); } else if (strstr($record, ';')) { $this->itemList->addItem("simpleText","$record"); } else { $this->itemList->addItem("simpleText",";$record;"); } } } $themeArray = explode(',',$theme); if (strcmp($theme,'') !== 0) { foreach ($themeArray as $el) { if (preg_match('/ledit\|(.*)/',$el,$matches)) { $this->itemList->addItem('ledit',$matches[1],'Edit','Edit'); } else { switch ($el) { case 'moveup': $this->itemList->addItem('moveupForm',$this->applicationUrl . $this->controller.'/'.$this->params['mainAction'],";".$primaryKey.";"); break; case 'movedown': $this->itemList->addItem('movedownForm',$this->applicationUrl . $this->controller.'/'.$this->params['mainAction'],";".$primaryKey.";"); break; case 'link': $this->itemList->addItem('associateForm',$this->applicationUrl . $this->controller.'/'.$this->params['associateAction'],";".$primaryKey.";"); break; case 'edit': $this->itemList->addItem('editForm',$this->applicationUrl . $this->controller.'/'.$this->params['modifyAction'],";".$primaryKey.";"); break; case 'del': $this->itemList->addItem('delForm',$this->applicationUrl . $this->controller.'/'.$this->params['mainAction'],";".$primaryKey.";"); break; case 'ledit': $this->itemList->addItem('ledit',$this->applicationUrl . $this->controller.'/'.$this->params['modifyAction'].'/;'.$primaryKey.';','Edit','Edit'); break; } } } } } //initialize the form //$queryType = insert/update //$action: the action of the form (controller/action/queryString) public function loadForm($queryType,$action,$method = 'POST',$enctype = null) { $this->queryType = $queryType; $submitName = $this->model->getSubmitName($queryType); $value = $this->params['postSubmitValue']; $viewStatus = Url::createUrl($this->viewArgs); $this->model->setForm($action.$viewStatus,array($submitName => $value),$method,$enctype); $this->form = $this->model->form; } //function to obtain the values to use in the form //$func = function to validate the values //$id = the id of the record (used if $_POST[$this->m[$this->model]->identifierName] is not present) public function getFormValues($func = 'sanitizeHtml',$id = null,$defaultValues = array(),$functionsIfFromDb = array()) { if ($this->_type === 'form') { $this->values = $this->model->getFormValues($this->queryType,$func,$id,$defaultValues,$functionsIfFromDb); } } //set the head of the table //$columnsName: name of the columns. It has to be a comma-separated list of strings public function setHead($columnsName) { $this->itemList->setHead($columnsName); } //method to set the type of the entries of the form //$entries: string containing the list of the entries where each entry is separated by comma: entry1,entry2,entry3 //$entryType: associative array that describes the entries of the form. The key is the entry name while the value is the entry type (textarea,inputText,etc) public function setFormEntries($entries = 'model',$entryType = array(),$optionsArray = array()) { if ($this->_type === 'form') { if ($entries === 'model') { $this->entries = $this->model->fields; if ($this->queryType === 'update') { $this->entries .= ','. $this->model->identifierName; } } else { $this->entries = null; } $entriesArray = explode(',',$this->entries); if (isset($this->form)) { foreach ($entriesArray as $entry) { $type = isset($entryType[$entry]) ? $entryType[$entry] : 'InputText'; $options = isset($optionsArray[$entry]) ? $optionsArray[$entry] : null; $this->form->setEntry($entry,$type,$options); } if ($this->queryType === 'update') { $this->form->setEntry($this->model->identifierName,'Hidden'); } } else { throw new Exception('form object has not been initialized. Call the scaffold->loadForm method before'); } } } //add an item to the list of items public function addItem($type, $action = '', $field = '', $name = '', $value = '', $title = '') { if ($this->_type === 'main') { $this->itemList->addItem($type, $action, $field, $name, $value, $title); } } //update the table public function update($methodsList = '',$id = null) { $this->model->updateTable($methodsList,$id); } //method to create the HTML of the scaffold //$values: the values to insert in the from entries public function render($values = null,$subset = null) { if ($this->_type === 'main') { $recordNumber = $this->model->rowNumber(); if (isset($this->viewArgs[$this->params['pageVariable']])) { $page = $this->viewArgs[$this->params['pageVariable']]; } else { $this->params['pageList'] = false; } $recordPerPage = $this->params['recordPerPage']; if ($this->params['pageList'] === true) { $this->model->limit = $this->pageList->getLimit($page,$recordNumber,$recordPerPage); $this->html['pageList'] = $this->pageList->render((int)($page-2),5); $position = array($page,$this->pageList->getNumbOfPages()); } else { $this->model->limit = null; $this->html['pageList'] = null; $position = array(1,1); } $values = $this->model->getTable($this->fields); $primaryKey = $this->_primaryKey; //pass the variable position $this->itemList->position = $position; $this->html['main'] = $this->itemList->render($values); $this->html['menu'] = $this->mainMenu->render($this->params['mainMenu']); $popupHtml = null; if ($this->params['popup'] === true) { $this->html['popup'] = $this->popupMenu->render(); $popupHtml = "
\n".$this->html['popup']."\n
\n"; } $this->html['all'] = "\n".$this->model->notice."\n $popupHtml \n
\n".$this->html['main']."\n
\n"."
\n
\n".$this->strings->gtext('pages').": ".$this->html['pageList']."
\n
\n\n"; } else if ($this->_type === 'form') { $subset = (!isset($subset)) ? $this->entries : $subset; $values = (!isset($values)) ? $this->values : $values; $this->html['menu'] = $this->mainMenu->render($this->params['formMenu']); $this->html['main'] = $this->form->render($values,$subset); $this->html['all'] = "\n".$this->model->notice."\n
\n".$this->html['main']."
\n"; } return $this->html['all']; } }