Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
group.controller.php
Go to the documentation of this file.
00001 <?php
00028 class groupController extends controller {
00029 
00033         private $_class_privilege;
00034 
00038         private $_admin_privilege;
00039 
00045         function __construct() {
00046 
00047                 parent::__construct();
00048 
00049                 $this->_cpath = dirname(__FILE__);
00050                 $this->_mdl_name = "group";
00051 
00052                 // privileges
00053                 $this->_class_privilege = $this->_mdl_name;
00054                 $this->_admin_privilege = 1;
00055         }
00056 
00063         public function manage() {
00064         
00065                 access::check($this->_class_privilege, $this->_admin_privilege, array("exitOnFailure"=>true));
00066 
00067                 $id = cleanInput('get', 'id', 'int');
00068 
00069                 if($id || cleanInput('get', 'action', 'string')=='new') { 
00070                         $g = new group($id);
00071                         return $this->manageGroup($g);
00072                 }
00073                 
00074                 $text = __("ManageGroupsExp");
00075 
00076                 $heads = array(__("label"), __("description"), __("privileges"));
00077                 $rows = array();
00078                 foreach(group::get($this->_registry) as $gid) {
00079                         $g = new group($gid);
00080                         $gpriv = array();
00081                         foreach(explode(",", $g->privileges) as $pid) {
00082                                 $p = new privilege($pid);
00083                                 $gpriv[] = $p->label;
00084                         }
00085                         $label = $g->id == 1 
00086                                 ? htmlVar($g->label)
00087                                 : anchor($this->_router->linkHref($this->_mdl_name, 'manage', array("id"=>$gid)), htmlVar($g->label));
00088 
00089                         $rows[] = array($label, htmlVar($g->description), implode(", ", $gpriv));       
00090                 }
00091 
00092                 $this->_view->setTpl('table');
00093                 $this->_view->assign('caption', __("GroupsInSystem"));
00094                 $this->_view->assign('class', 'wide generic');
00095                 $this->_view->assign('heads', $heads);
00096                 $this->_view->assign('rows', $rows);
00097 
00098                 $table = $this->_view->render();
00099 
00100                 $link_insert = anchor($this->_router->linkHref($this->_mdl_name, 'manage', array("action"=>"new")), __("insertNewRecord"), array('class'=>'submit'));
00101 
00102                 $this->_view->setTpl('group_manage_list');
00103                 $this->_view->assign('title', __("ManageGroups"));
00104                 $this->_view->assign('text', $text);
00105                 $this->_view->assign('link_insert', $link_insert);
00106                 $this->_view->assign('table', $table);
00107 
00108                 return $this->_view->render();
00109         }
00110 
00117         private function manageGroup($g) {
00118                 
00119                 $form = $this->formGroup($g);
00120 
00121                 $title = $g->id ? __("Edit")." \"".htmlVar($g->label)."\"" : __("NewGroup");
00122                 $this->_view->setTpl('group_manage');
00123                 $this->_view->assign('title', $title);
00124                 $this->_view->assign('form', $form);
00125 
00126                 return $this->_view->render();
00127 
00128         }
00129 
00136         private function formGroup($g) {
00137 
00138                 $myform = new form('post', 'group_form', array("validation"=>true));
00139                 $myform->load('group_form');
00140 
00141                 $required = '';
00142                 $buffer = $myform->sform($this->_router->linkHref($this->_mdl_name, 'saveGroup'), $required);
00143                 $buffer .= $myform->hidden('id', $g->id);
00144 
00145                 if($g->id > 5 || !$g->id) $buffer .= $this->formGroupData($g, $myform);
00146                 else $buffer .= "<p>".htmlVar($g->description)."</p>";
00147 
00148                 $buffer .= $this->formPrivileges($g, $myform);
00149 
00150                 $buffer .= $myform->input('submit_edit', 'submit', __("edit"), array("class"=>"left"));
00151                 if($g->id>5) {
00152                         $onclick = "onclick=\"if(confirm('Sicuro di voler procedere con l\'eliminazione?')) location.href='".$this->_router->linkHref($this->_mdl_name, 'deleteGroup', array("id"=>$g->id))."'\"";
00153                         $buffer .= " ".$myform->input('submit_delete', 'button', __("delete"), array("class"=>"right", "js"=>$onclick));
00154                 }
00155                 $buffer .= clearFloat();
00156 
00157                 $buffer .= $myform->cform();
00158 
00159                 return $buffer;
00160 
00161         }
00162 
00170         private function formGroupData($g, $myform) {
00171 
00172                 $content = $myform->cinput('label', 'text', $myform->retvar('label', htmlInput($g->label)), __("label"), array("required"=>true, "size"=>40, "maxlength"=>200));
00173                 $content .= $myform->ctextarea('description', $myform->retvar('description', htmlInput($g->description)), __("description"), array("required"=>false, "cols"=>60, "rows"=>4));
00174 
00175                 return $myform->fieldset(__("GeneralData"), $content);
00176 
00177         }
00178 
00186         private function formPrivileges($g, $myform) {
00187         
00188                 $buffer = '';
00189 
00190                 $privileges_ids = privilege::get($this->_registry);
00191 
00192                 $old_ctg = null;
00193                 $odd = true;
00194                 $form_left = ""; 
00195                 $form_right = ""; 
00196                 $checked = explode(",", $g->privileges);
00197 
00198                 $i = 0;
00199                 $odd = false;
00200                 $tot = count($privileges_ids);
00201                 foreach($privileges_ids as $pid) {
00202                         $i++;
00203                         $p = new privilege($pid);
00204                         if($old_ctg != $p->category) {
00205                                 if(!is_null($old_ctg)) {
00206                                         $field = $myform->cmulticheckbox("pids[]", $checked, $mcelements, $label, array("label_class"=>"block"));
00207                                         if($odd) $form_left .= $field;
00208                                         else $form_right .= $field;
00209                                 }
00210                                 $mcelements = array();
00211                                 $old_ctg = $p->category;
00212                                 $label = htmlVar($p->category);
00213                                 $odd = !$odd;
00214                         }
00215                         $mcelements[] = array("label"=>htmlVar($p->label)." ".tooltip("(?)", htmlVar($p->label), htmlVar($p->description), array("class"=>"help")), "value"=>$p->id);
00216                         if($i==$tot) {
00217                                 $field = $myform->cmulticheckbox("pids[]", $checked, $mcelements, $label, array("label_class"=>"block"));
00218                                 if($odd) $form_left .= $field;
00219                                 else $form_right .= $field;
00220                         }
00221 
00222                 }
00223 
00224                 $view = new view();
00225                 $view->setTpl('group_form_privilege');
00226                 $view->assign('form_left', $form_left);
00227                 $view->assign('form_right', $form_right);
00228 
00229                 $content = $view->render();
00230 
00231                 return $myform->fieldset(__("ManagePrivileges"), $content);
00232                         
00233         }
00234 
00240         public function saveGroup() {
00241         
00242                 access::check($this->_class_privilege, $this->_admin_privilege, array("exitOnFailure"=>true));
00243 
00244                 $id = cleanInput('post', 'id', 'int');
00245                 $g = new group($id);
00246 
00247                 if($g->id == 1) header("Location: ".$this->_router->linkHref($this->_mdl_name, 'manage'));
00248 
00249                 if($g->id > 5 || !$g->id) {
00250                         $g->label = cleanInput('post', 'label', 'string');
00251                         $g->description = cleanInput('post', 'description', 'string');
00252                 }
00253                 $checked_ids = cleanInputArray('post', 'pids', 'int');
00254                 $g->privileges = $checked_ids ? implode(",", $checked_ids) : '';
00255 
00256                 $g->saveData();
00257 
00258                 header("Location: ".$this->_router->linkHref($this->_mdl_name, 'manage'));
00259 
00260         }
00261 
00267         public function deleteGroup() {
00268         
00269                 access::check($this->_class_privilege, $this->_admin_privilege, array("exitOnFailure"=>true));
00270 
00271                 $id = cleanInput('get', 'id', 'int');
00272                 $g = new group($id);
00273 
00274                 if($g->id < 6) header("Location: ".$this->_router->linkHref($this->_mdl_name, 'manage'));
00275 
00276                 $g->deleteData();
00277 
00278                 header("Location: ".$this->_router->linkHref($this->_mdl_name, 'manage'));
00279 
00280         }
00281 }
00282 
00283 ?>