Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
adminTable.class.php
Go to the documentation of this file.
00001 <?php
00048 class adminTable {
00049         
00053         protected $_registry;
00054         
00058         protected $_table;
00059         
00063         protected $_primary_key;
00064 
00068         protected $_fields;
00069 
00073         protected $_fkeys;
00074         
00078         protected $_sfields;
00079         
00083         protected $_pfields;
00084 
00088         protected $_html_fields;
00089         
00093         protected $_efp;
00094         
00098         protected $_cls_cbk_del;
00099         
00103         protected $_mth_cbk_del;
00104         
00108         protected $_insertion;
00109         
00113         protected $_deletion;
00114 
00118         protected $_edit_deny;
00119 
00123         protected $_changelist_fields;
00124         
00128         protected $_filter_fields;
00129         
00133         protected $_editor;
00134         
00138         protected $_custom_tpl;
00139         
00143         protected $_view;
00144 
00148         protected $_arrow_down_path;
00149 
00153         protected $_arrow_up_path;
00154 
00174         function __construct($table, $opts=null) {
00175 
00176                 $this->_registry = registry::instance();
00177                 $this->_table = $table;
00178                 $this->_view = new view();
00179 
00180                 /* options */
00181                 $this->_insertion = gOpt($opts, 'insertion', true);
00182                 $this->_deletion = gOpt($opts, 'deletion', true);
00183                 $this->_edit_deny = gOpt($opts, 'edit_deny', array());
00184                 $this->_changelist_fields = gOpt($opts, 'changelist_fields', null);
00185                 $this->_editor = gOpt($opts, 'editor', false);
00186                 $this->_export = gOpt($opts, 'export', false);
00187                 $this->_custom_tpl = gOpt($opts, 'custom_tpl', array());
00188                 $this->_efp = gOpt($opts, "efp", 20);
00189                 $this->_cls_cbk_del = gOpt($opts, "cls_callback_delete", null);
00190                 $this->_mth_cbk_del = gOpt($opts, "mth_callback_delete", null); 
00191 
00192                 $structure = $this->_registry->db->getTableStructure($this->_table);
00193 
00194                 $this->_primary_key = $structure['primary_key'];
00195                 $this->_fields = $structure['fields'];
00196                 $this->_fkeys = array();
00197                 $this->_sfields = array();
00198                 $this->_pfields = array();
00199                 $this->_html_fields = array();
00200 
00201                 $this->_arrow_down_path = ROOT."/img/down_arrow-black.png";
00202                 $this->_arrow_up_path = ROOT."/img/up_arrow-black.png";
00203         
00204         }
00205 
00218         public function setForeignKeys($fkeys) {
00219                 $this->_fkeys = $fkeys;
00220                 foreach($this->_fkeys as $k=>$v) {
00221                         $fkts = $this->_registry->db->getTableStructure($v['table']);
00222                         $this->_fkeys[$k]['key'] = $fkts['primary_key'];
00223                 }
00224         }
00225 
00266         public function setSpecialFields($sfields) {
00267                 $this->_sfields = $sfields;
00268                 foreach($this->_sfields as $k=>$v) {
00269                         if($v['type']=='multicheck') {
00270                                 $fkts = $this->_registry->db->getTableStructure($v['table']);
00271                                 $this->_sfields[$k]['key'] = $fkts['primary_key'];
00272                         }
00273                 }
00274         }
00275         
00283         public function setPluginFields($pfields) {
00284 
00285                 foreach($pfields as $k=>$v) {
00286                         if(!isset($this->_registry->plugins[$v['plugin']]))
00287                                 exit(error::syserrorMessage(get_class($this), 'setPluginFields', sprintf(__("cantFindPlugin"), $v['plugin']), __LINE__));
00288                 }
00289                 $this->_pfields = $pfields;
00290 
00291         }
00292 
00300         public function setChangelistFields($fields) {
00301                 
00302                 if(is_array($fields))
00303                         $this->_changelist_fields = $fields;
00304 
00305         }
00306         
00314         public function setFilterFields($filter_fields) {
00315                 
00316                 $this->_filter_fields = $filter_fields;
00317 
00318         }
00319 
00327         public function setHtmlFields($fields) {
00328                 
00329                 if(is_array($fields))
00330                         $this->_html_fields = $fields;
00331 
00332         }
00333 
00341         public function manage() {
00342 
00343                 if(!$this->_primary_key) return __("NoPrimaryKeyTable");
00344 
00345                 $edit = isset($_GET['edit']) ? true : false;
00346                 $insert = isset($_GET['insert']) ? true : false;
00347                 $save = isset($_GET['save']) ? true : false;
00348 
00349                 if($save) {
00350                         $order = cleanInput('post', 'order', 'string');
00351                         $order_param = $order ? "?order=".$order : '';
00352                         $res = $this->saveFields(); 
00353                         if(isset($_POST['submit_c_insert']) || isset($_POST['submit_c_modify'])) {
00354                                 // save and continue editing
00355                                 $_SESSION['adminTable_f_s_'.$this->_table] = $res;
00356                                 header("Location: ".preg_replace("#\?.*$#", "?edit".($order ? "&order=$order" : ""), $_SERVER['REQUEST_URI']));
00357                         }
00358                         else
00359                                 header("Location: ".preg_replace("#\?.*$#", $order_param, $_SERVER['REQUEST_URI']));
00360                 }
00361                 elseif($edit || $insert) return $this->editFields();
00362                 else return $this->view();
00363 
00364                 
00365         }
00366 
00372         public function view() {
00373 
00374                 $order = cleanInput('get', 'order', 'string');
00375 
00376                 $tot_fk = count($this->_fkeys);
00377                 $tot_sf = count($this->_sfields);
00378                 $tot_pf = count($this->_pfields);
00379                 $tot_ff = count($this->_filter_fields);
00380 
00381                 if($tot_ff) $this->setSessionSearch();
00382 
00383                 // get order field and direction
00384                 preg_match("#^([^ ,]*)\s?((ASC)|(DESC))?.*$#", $order, $matches);
00385                 $field_order = isset($matches[1]) ? $matches[1] : null;
00386                 $order_dir = isset($matches[2]) ? $matches[2] : null;
00387 
00388                 $fields_names = $this->_changelist_fields ? $this->_changelist_fields : $this->_registry->db->getFieldsName($this->_table);
00389                 
00390                 $where_pag = $tot_ff ? $this->setWhereClause(false) : null;
00391                 $pag = new pagination($this->_efp, $this->_registry->db->getNumRecords($this->_table, $where_pag, $this->_primary_key));
00392                 $limit = array($pag->start(), $this->_efp);
00393 
00394                 if(count($this->_changelist_fields)) {
00395                         if(!in_array($this->_primary_key, $this->_changelist_fields)) { 
00396                                 array_unshift($this->_changelist_fields, $this->_primary_key);
00397                                 array_unshift($fields_names, $this->_primary_key);
00398                         }
00399                         $field_selection = isset($this->_fkeys[$field_order]) 
00400                                         ? 'a.'.implode(', a.', $this->_changelist_fields)
00401                                         : implode(',', $this->_changelist_fields);
00402                 }
00403                 else 
00404                         $field_selection = isset($this->_fkeys[$field_order]) ? "a.*" : "*"; 
00405 
00406                 $where = $tot_ff ? $this->setWhereClause(isset($this->_fkeys[$field_order])) : null;
00407 
00408                 // different queries if the order field is a foreign key
00409                 if(isset($this->_fkeys[$field_order])) {
00410                         $records = $this->_registry->db->autoSelect($field_selection, array($this->_table." AS a", $this->_fkeys[$field_order]['table']." AS b"), ($where ? $where." AND " : "")."a.$field_order=b.".$this->_fkeys[$field_order]['key'], "b.".$this->_fkeys[$field_order]['order']." $order_dir", $limit);
00411                 }
00412                 else 
00413                         $records = $this->_registry->db->autoSelect($field_selection, $this->_table, $where, $order, $limit);
00414 
00415                 $all = "<span class=\"link\" onclick=\"$$('#atbl_form input[type=checkbox]').setProperty('checked', 'checked');\">".__("all")."</span>";
00416                 $none = "<span class=\"link\" onclick=\"$$('#atbl_form input[type=checkbox]').removeProperty('checked');\">".__("none")."</span>";
00417                 $heads = ($this->_edit_deny != 'all' || $this->_export) ? array("0"=>"$all | $none") : array();
00418                 foreach($fields_names as $fn) {
00419                         if(!$this->_changelist_fields || in_array($fn, $this->_changelist_fields)) {
00420                                 $ord = $order == $fn." ASC" ? $fn." DESC" : $fn." ASC";
00421 
00422                                 if($order == $fn." ASC") {
00423                                         $jsover = "$(this).getNext('img').setProperty('src', '$this->_arrow_down_path')";
00424                                         $jsout = "$(this).getNext('img').setProperty('src', '$this->_arrow_up_path')";
00425                                         $a_style = "visibility:visible";
00426                                         $apath = $this->_arrow_up_path;
00427                                 }
00428                                 elseif($order == $fn." DESC") {
00429                                         $jsover = "$(this).getNext('img').setProperty('src', '$this->_arrow_up_path')";
00430                                         $jsout = "$(this).getNext('img').setProperty('src', '$this->_arrow_down_path')";
00431                                         $js = "$(this).getNext('img').getNext('img').setStyle('visibility', 'visible')";
00432                                         $a_style = "visibility:visible";
00433                                         $apath = $this->_arrow_down_path;
00434                                 }
00435                                 else {
00436                                         $js = '';
00437                                         $jsover = "$(this).getNext('img').setStyle('visibility', 'visible')";
00438                                         $jsout = "$(this).getNext('img').setStyle('visibility', 'hidden')";
00439                                         $a_style = "visibility:hidden";
00440                                         $apath = $this->_arrow_up_path;
00441                                 }
00442 
00443                                 $link = preg_replace("#/p/\d+/#", "/", $_SERVER['REQUEST_URI']);
00444                                 $link = preg_replace("#\?.*#", "", $link);
00445                                 $head_t = anchor($link."?order=$ord", __($fn), array('over'=>$jsover, 'out'=>$jsout));
00446                                 $heads[] = $head_t." <img src=\"$apath\" alt=\"down\" style=\"$a_style\" />";
00447                         }
00448                 }
00449 
00450                 $rows = array();
00451                 foreach($records as $r) {
00452                         $input = "<input type=\"checkbox\" name=\"f[]\" value=\"".$r[$this->_primary_key]."\" />";
00453                         if($tot_fk) $r = $this->parseForeignKeys($r);
00454                         if($tot_sf) $r = $this->parseSpecialFields($r);
00455                         if($tot_pf) $r = $this->parsePluginFields($r);
00456                         $r = $this->parseDateFields($r);
00457                         if($this->_edit_deny=='all' && !$this->_export) $rows[] = $r;
00458                         elseif(is_array($this->_edit_deny) && in_array($r[$this->_primary_key], $this->_edit_deny)) $rows[] = array_merge(array(""), $r);
00459                         else $rows[] = array_merge(array($input), $r);
00460                 }
00461                 
00462 
00463                 $this->_view->setTpl('table');
00464                 $this->_view->assign('class', 'generic wide');
00465                 $this->_view->assign('caption', __("RecordInTable")." ".$this->_table);
00466                 $this->_view->assign('heads', $heads);
00467                 $this->_view->assign('rows', $rows);
00468 
00469                 $table = $this->_view->render();
00470 
00471                 if($this->_edit_deny!='all' || $this->_export) {
00472                         $myform = new form('post', 'atbl_form', array("validation"=>false));
00473                         $formstart = $myform->sform('?edit'.($order ? "&order=$order" : ""), null);
00474                         $formend = $myform->cform();
00475                 }
00476                 else {
00477                         $formstart = '';
00478                         $formend = '';
00479                 }
00480 
00481                 if($this->_edit_deny=='all') {
00482                         $input_edit = '';
00483                         $input_delete = '';
00484                 }
00485                 else {
00486                         $onclick = "var checked = false;
00487                                     var felements = $$('#atbl_form input[type=checkbox]');
00488                                     for(var i=0;i<felements.length;i++) if(felements[i].checked) {checked = true;break;}
00489                                     if(!checked) {alert('".jsVar(__("SelectAtleastRecord"))."'); return false;}";
00490                         $input_edit = $myform->input('submit_edit', 'submit', __("edit"), array("js"=>"onclick=\"$onclick\""));
00491                         if($this->_deletion)
00492                                 $input_delete = $myform->input('submit_delete', 'submit', __("delete"), array("js"=>"onclick=\"$onclick return confirmSubmit('".jsVar(__("ProcedeDeleteSelectedRecords"))."')\""));
00493                         else $input_delete = '';
00494                 }
00495 
00496                 if($this->_export) {
00497                         $onclick = "var checked = false;
00498                                     var felements = $$('#atbl_form input[type=checkbox]');
00499                                     for(var i=0;i<felements.length;i++) if(felements[i].checked) {checked = true;break;}
00500                                     if(!checked) {alert('".jsVar(__("SelectAtleastRecord"))."'); return false;}";
00501                         $input_export_selected = $myform->input('submit_export_selected', 'submit', __("exportSelected"), array("js"=>"onclick=\"$onclick \""));
00502                         $input_export_all = $myform->input('submit_export_all', 'submit', __("exportAll"), array());
00503                         $input_where_query = $myform->hidden('where_query', $where);
00504                 
00505                 }
00506                 else {
00507                         $input_export_selected = null;
00508                         $input_export_all = null;
00509                         $input_where_query = '';        
00510                 }
00511 
00512                 $link_insert = $this->_insertion ? anchor("?insert", __("insertNewRecord"), array('class'=>'submit')) : null;
00513                 
00514 
00515                 if(isset($this->_custom_tpl['view'])) {
00516                         $tpl_name = $this->_custom_tpl['view'];
00517                 }
00518                 elseif($tot_ff) {
00519                         $tpl_name = 'admin_table_filter';
00520                         $this->_view->assign('form_filters_title', __("Filters"));
00521                         $this->_view->assign('form_filters', $this->formFilters());
00522                 }
00523                 else {
00524                         $tpl_name = 'admin_table';
00525                 }
00526 
00527                 $this->_view->setTpl($tpl_name);
00528                 $this->_view->assign('table', $table);
00529                 $this->_view->assign('link_insert', $link_insert);
00530                 $this->_view->assign('formstart', $formstart);
00531                 $this->_view->assign('formend', $formend);
00532                 $this->_view->assign('input_edit', $input_edit);
00533                 $this->_view->assign('input_delete', $input_delete);
00534                 $this->_view->assign('input_where_query', $input_where_query);
00535                 $this->_view->assign('input_export_selected', $input_export_selected);
00536                 $this->_view->assign('input_export_all', $input_export_all);
00537                 $this->_view->assign('psummary', $pag->summary());
00538                 $this->_view->assign('pnavigation', $pag->navigation());
00539 
00540                 return $this->_view->render();
00541         }
00542 
00548         protected function setSessionSearch() {
00549 
00550                 foreach($this->_filter_fields as $fname) {
00551 
00552                         if(!isset($_SESSION[$this->_table.'_'.$fname.'_filter'])) $_SESSION[$this->_table.'_'.$fname.'_filter'] = null;
00553 
00554                 }
00555 
00556                 if(isset($_POST['ats_submit'])) {
00557 
00558                         foreach($this->_filter_fields as $fname) {
00559 
00560                                 $type = $this->_fields[$fname]['type'];
00561 
00562                                 if(isset($_POST[$fname.'_filter'])) {
00563                                         if($type=='int' || $type=='float') {
00564                                                 if($_POST[$fname.'_filter']==='') {
00565                                                         $_SESSION[$this->_table.'_'.$fname.'_filter'] = null;
00566                                                 }
00567                                                 else {
00568                                                         $_SESSION[$this->_table.'_'.$fname.'_filter'] = $this->cleanField($fname."_filter", $type);
00569                                                 }
00570                                         }
00571                                         else {
00572                                                 $_SESSION[$this->_table.'_'.$fname.'_filter'] = $this->cleanField($fname."_filter", $type, array("escape"=>false));
00573                                         }
00574                                 
00575                                 }
00576                                 else {
00577                                         $_SESSION[$this->_table.'_'.$fname.'_filter'] = null; 
00578                                 }
00579                         }
00580                         
00581                 }
00582 
00583         }
00584         
00592         protected function setWhereClause($fkeysorder) {
00593 
00594                 $where_a = array();
00595                 $prefix = $fkeysorder ? "a." : "";
00596 
00597                 foreach($this->_filter_fields as $fname) {
00598                         if($this->_fields[$fname]['type']=='varchar' || $this->_fields[$fname]['type']=='text') {
00599                                 if(isset($_SESSION[$this->_table.'_'.$fname.'_filter']) && $_SESSION[$this->_table.'_'.$fname.'_filter']) {
00600                                         $value = $_SESSION[$this->_table.'_'.$fname.'_filter'];
00601                                         if(preg_match("#^\"([^\"]*)\"$#", $value, $matches))
00602                                                 $where_a[] = $prefix.$fname."='".$matches[1]."'"; 
00603                                         elseif(preg_match("#^\"([^\"]*)$#", $value, $matches))
00604                                                 $where_a[] = $prefix.$fname." LIKE '".$matches[1]."%'"; 
00605                                         else
00606                                                 $where_a[] = $prefix.$fname." LIKE '%".$value."%'"; 
00607                                 }
00608                         }
00609                         else {
00610                                 if(isset($_SESSION[$this->_table.'_'.$fname.'_filter']) && !is_null($_SESSION[$this->_table.'_'.$fname.'_filter'])) {
00611                                         $value = $_SESSION[$this->_table.'_'.$fname.'_filter'];
00612                                         $where_a[] = $prefix.$fname."='".$value."'";
00613                                 }
00614                         }
00615                 }
00616 
00617                 return implode(" AND ", $where_a);
00618 
00619         }
00620 
00626         protected function formFilters() {
00627 
00628                 $myform = new form('post', 'atbl_filter_form', array("validation"=>false));
00629                 $myform->load();
00630 
00631                 $form = $myform->sform('', null);
00632 
00633                 foreach($this->_filter_fields as $fname) {
00634                         $field = $this->_fields[$fname];
00635                         $field['null'] = '';
00636                         $form .= $this->formElement($myform, $fname, $field, 'filter', array("size"=>20, "value"=>htmlInput($_SESSION[$this->_table.'_'.$fname.'_filter'])));
00637                 }
00638 
00639                 $onclick = "onclick=\"$$('#atbl_filter_form *[name$=_filter]').each(function(el) { 
00640                         if(el.get('type')==='text') el.value='';
00641                         else if(el.get('type')==='radio') el.removeProperty('checked');
00642                         else if(el.get('tag')=='select') el.getChildren('option').removeProperty('selected');
00643                         });\"";
00644 
00645                 $input_reset = $myform->input('ats_reset', 'button', __("reset"), array("js"=>$onclick)); 
00646                 $form .= $myform->cinput('ats_submit', 'submit', __("filter"), '', array("text_add"=>' '.$input_reset)); 
00647                 $form .= $myform->cform();
00648 
00649                 return $form;
00650 
00651         }
00652 
00660         public function parseForeignKeys($row) {
00661 
00662                 $res = array();
00663 
00664                 foreach($row as $k=>$v) {
00665                         if(isset($this->_fkeys[$k])) {
00666                                 $fkts = $this->_registry->db->getTableStructure($this->_fkeys[$k]['table']);
00667                                 $fk = $this->_registry->db->autoSelect($this->_fkeys[$k]['field'], $this->_fkeys[$k]['table'], $this->_fkeys[$k]['key']."='$v'" , null);
00668                                 $res[$k] = isset($fk[0]) ? $fk[0][$this->_fkeys[$k]['field']] : null;
00669                         }
00670                         else $res[$k] = $v;
00671                 }
00672 
00673                 return $res;
00674 
00675         }
00676 
00688         public function parseSpecialFields($row, $opts=null) {
00689 
00690                 $res = array();
00691                 foreach($row as $k=>$v) {
00692                         if(isset($this->_sfields[$k])) {
00693                                 if($this->_sfields[$k]['type']=='password') $res[$k] = $v ? (gOpt($opts, 'show_pwd', false) ? $v : "**************") : '';
00694                                 elseif($this->_sfields[$k]['type']=='bool')
00695                                         $res[$k] = $v ? $this->_sfields[$k]['true_label'] : $this->_sfields[$k]['false_label'];
00696                                 elseif($this->_sfields[$k]['type']=='email') {
00697                                         $mailto = isset($this->_sfields[$k]['list_mailto']) && $this->_sfields[$k]['list_mailto'] && gOpt($opts, 'mailto', true) ? true : false;
00698                                         $res[$k] = $v ? ($mailto ? anchor('mailto:'.$v, $v) : $v) : '';
00699                                 }
00700                                 elseif($this->_sfields[$k]['type']=='enum') {
00701                                         $res[$k] = $v ? $this->_sfields[$k]['data'][$v] : '';
00702                                 }
00703                                 elseif($this->_sfields[$k]['type']=='multicheck') {
00704                                         $vf = array();
00705                                         foreach(explode(",", $v) as $vp) {
00706                                                 $fkts = $this->_registry->db->getTableStructure($this->_sfields[$k]['table']);
00707                                                 $fk = $this->_registry->db->autoSelect($this->_sfields[$k]['field'], $this->_sfields[$k]['table'], $this->_sfields[$k]['key']."='$vp'" , null);
00708                                                 $vf[] = isset($fk[0]) ? $fk[0][$this->_sfields[$k]['field']] : '';
00709                                         }
00710                                         $res[$k] = implode(", ", $vf);
00711                                 }
00712                                 elseif($this->_sfields[$k]['type']=='file' || $this->_sfields[$k]['type']=='image') {
00713                                         $sf = $this->_sfields[$k];
00714                                         if($sf['preview'] && $v) {
00715                                                 if($this->_sfields[$k]['type']=='image') {
00716                                                         $res[$k] = "<a title=\"$v\" href=\"".$sf['rel_path']."/$v\">".$v."</span><script>$$('a[href=".$sf['rel_path']."/$v]')[0].cerabox();</script>";
00717                                                 }
00718                                                 else {
00719                                                         $res[$k] = "<a title=\"$v\" href=\"".$sf['rel_path']."/$v\">".$v."</span>";
00720                                                 }
00721                                         }
00722                                         else $res[$k] = $v;
00723                                 }
00724                         }
00725                         else $res[$k] = $v;
00726                 }
00727 
00728                 return $res;
00729 
00730         }
00731 
00739         public function parsePluginFields($row) {
00740 
00741                 $res = array();
00742 
00743                 foreach($row as $k=>$v) {
00744                         if(isset($this->_pfields[$k])) {
00745                                 $plugin = $this->_pfields[$k]['plugin'];
00746                                 $res[$k] = $this->_registry->plugins[$plugin]->adminList($this->_pfields[$k], $v);
00747                         }
00748                         else $res[$k] = $v;     
00749                 }       
00750 
00751                 return $res;
00752         }
00753         
00761         public function parseDateFields($row) {
00762 
00763                 $res = array();
00764 
00765                 $structure = $this->_registry->db->getTableStructure($this->_table);
00766 
00767                 foreach($row as $k=>$v) {
00768                         if($structure['fields'][$k]['type']=='date') $res[$k] = $this->_registry->dtime->view($v, 'date');
00769                         elseif($structure['fields'][$k]['type']=='datetime') $res[$k] = $this->_registry->dtime->view($v);
00770                         elseif($structure['fields'][$k]['type']=='timestamp') $res[$k] = $this->_registry->dtime->view($v);
00771                         else $res[$k] = $v;
00772                 }
00773 
00774                 return $res;
00775 
00776         }
00777 
00783         protected function checkUpload() {
00784                 
00785                 foreach($this->_sfields as $fname=>$finfo) if($finfo['type']=='file' || $finfo['type']=='image') return true;
00786                 return false;
00787 
00788         }
00789 
00801         public function editFields($opts=null) {
00802 
00803                 $insert = (isset($_GET['insert']) || gOpt($opts, 'insert')) ? true : false;
00804                 $order = cleanInput('get', 'order', 'string');
00805                 $order_param = $order ? "?order=".$order : '';
00806                 $submit_edit = cleanInput('post', 'submit_edit', 'string');
00807                 $submit_delete = cleanInput('post', 'submit_delete', 'string');
00808                 $submit_export_selected = cleanInput('post', 'submit_export_selected', 'string');
00809                 $submit_export_all = cleanInput('post', 'submit_export_all', 'string');
00810 
00811                 if($insert && !$this->_insertion) header("Location: ".preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']));
00812 
00813                 $formaction = gOpt($opts, 'action', '?save');
00814                 $f_s = gOpt($opts, "f_s", null);
00815                 if(is_null($f_s)) {
00816                         if(isset($_POST['f'])) $f_s = cleanInputArray('post', 'f', 'string');
00817                         elseif(isset($_SESSION['adminTable_f_s_'.$this->_table])) $f_s = $_SESSION['adminTable_f_s_'.$this->_table]; 
00818                         else $f_s = array();
00819                 }
00820                 if((!$insert && !$submit_export_all) && !$f_s) header("Location: ".preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']));
00821                 if($submit_export_selected) $this->export($f_s);
00822                 if($submit_export_all) $this->export('all', cleanInput('post', 'where_query', 'string', array("escape"=>false)));
00823                 if($submit_delete) {
00824                         if(!$this->_deletion || $this->_edit_deny=='all') header("Location: ".preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']));
00825                         if(count($f_s)) {
00826                                 if($this->_cls_cbk_del && $this->_mth_cbk_del)
00827                                         call_user_func(array($this->_cls_cbk_del,$this->_mth_cbk_del), $this->_registry, $f_s);
00828                                 else {
00829                                         if(is_array($this->_edit_deny) && count($this->_edit_deny)) $f_s = array_diff($f_s, $this->_edit_deny);
00830                                         $this->deleteFiles($f_s);
00831                                         if(count($this->_pfields)) {
00832                                                 foreach($this->_pfields as $k=>$v) {
00833                                                         $this->_registry->plugins[$v['plugin']]->adminDelete($v, $f_s);
00834                                                 }
00835                                         }
00836                                         $where = $this->_primary_key."='".implode("' OR ".$this->_primary_key."='", $f_s)."'";
00837                                         $this->_registry->db->delete($this->_table, $where);
00838                                 }
00839                         }
00840                         header("Location: ".preg_replace("#\?.*$#", $order_param, $_SERVER['REQUEST_URI']));
00841                         exit();
00842                 }
00843 
00844                 $myform = new form('post', 'atbl_form', array("validation"=>true));
00845                 $myform->load();
00846 
00847                 $buffer = $myform->sform($formaction, null, array("upload"=>$this->checkUpload()));
00848                 $buffer .= $myform->hidden('order', $order);
00849 
00850                 if($insert) {
00851                         foreach($this->_fields as $fname=>$field) {
00852                                 if($field['extra']!='auto_increment') $buffer .= $this->formElement($myform, $fname, $field, null);
00853                         }
00854                 }
00855                 elseif(count($f_s)) {
00856                         if($this->_edit_deny=='all') header("Location: ".preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']));
00857                         foreach($f_s as $f) {
00858                                 if(!is_array($this->_edit_deny) || !in_array($f, $this->_edit_deny)) {
00859                                         $content = $this->formRecord($f, $myform);
00860                                         if(array_key_exists($this->_primary_key, $this->_fkeys)) {
00861                                                 $fk = $this->_fkeys[$this->_primary_key];
00862                                                 $records = $this->_registry->db->autoSelect($fk['field'], $fk['table'], $fk['key']."='$f'" , null);
00863                                                 $value_p = $records[0][$fk['field']];
00864                                         }
00865                                         else $value_p = $f;
00866                                         $buffer .= $myform->fieldset(__("Record")." ".$this->_primary_key." = $value_p", $content);
00867                                 }
00868                         }
00869                 }
00870 
00871                 $buffer .= $myform->input('submit_'.($insert ? "insert" : "modify"), 'submit', __('save'), array());
00872                 $buffer .= "&#160;".$myform->input('submit_c_'.($insert ? "insert" : "modify"), 'submit', __('saveContinueEditing'), array());
00873 
00874                 $buffer .= $myform->cform();
00875                 
00876                 if($this->_editor) $buffer .= chargeEditor($this->_registry, "#atbl_form div[class=html]");
00877 
00878                 if($insert && isset($this->_custom_tpl['insert'])) {
00879                         $this->_view->setTpl($this->_custom_tpl['insert']);
00880                         $this->_view->assign('form', $buffer);
00881                         return $this->_view->render();
00882                 }
00883                 elseif(isset($this->_custom_tpl['edit'])) {
00884                         $this->_view->setTpl($this->_custom_tpl['edit']);
00885                         $this->_view->assign('form', $buffer);
00886                         return $this->_view->render();
00887                 }       
00888                 else {
00889                         return $buffer;
00890                 }
00891 
00892         }
00893 
00901         protected function deleteFiles($f_s) {
00902         
00903                 if(!count($this->_sfields)) return 0;
00904 
00905                 foreach($f_s as $fid) {
00906                         foreach($this->_sfields as $fname=>$fopt) {
00907                                 if($fopt['type']=='file') {
00908                                         $rows = $this->_registry->db->autoSelect($fname, $this->_table, $this->_primary_key."='$fid'");
00909                                         $filename = $rows[0][$fname];
00910                                         @unlink($fopt['path'].DS.$filename);    
00911                                 }       
00912                                 elseif($fopt['type']=='image') {
00913                                         $rows = $this->_registry->db->autoSelect($fname, $this->_table, $this->_primary_key."='$fid'");
00914                                         $filename = $rows[0][$fname];
00915                                         @unlink($fopt['path'].DS.$filename);    
00916                                         if($fopt['make_thumb']) {
00917                                                 $prefix_thumb = isset($fopt['prefix_thumb']) ? $fopt['prefix_thumb'] : 'thumb_';
00918                                                 @unlink($fopt['path'].DS.$prefix_thumb.$filename);      
00919                                         }
00920                                 }
00921                         }
00922                 }
00923 
00924                 return 1;
00925 
00926         }
00927 
00941         public function formRecord($pk, $myform=null, $formaction=null) {
00942 
00943                 $buffer = '';
00944                 if(!$myform) {
00945                         if(!$formaction) $formaction = '?save';
00946                         $myform = new form('post', 'atbl_form', array("validation"=>true));
00947                         $myform->load();
00948                         $buffer .= $myform->sform($formaction, null, array("upload"=>$this->checkUpload()));
00949                 }       
00950 
00951                 $buffer .= $myform->hidden($this->_primary_key."[]", $pk);
00952                 foreach($this->_fields as $fname=>$field) {
00953                         if($fname != $this->_primary_key && $field['extra']!='auto_increment') 
00954                                 $buffer .= $this->formElement($myform, $fname, $field, $pk);
00955                 }
00956 
00957                 if(!$myform) $buffer .= $myform->cform();
00958 
00959                 return $buffer;
00960 
00961         }
00962 
00977         protected function formElement($myform, $fname, $field, $id, $opts=null) {
00978         
00979                 $id_f = preg_replace("#\s#", "_", $id); // replace spaces with '_' in form names as POST do itself
00980 
00981                 $required = $field['null']=='NO' ? true : false;
00982 
00983                 if(isset($opts['value'])) {
00984                         $value = gOpt($opts, 'value', '');
00985                 }
00986                 else {
00987                         $records = $this->_registry->db->autoSelect("*", $this->_table, $this->_primary_key."='$id'", null);
00988                         $value = count($records) ? $records[0][$fname] : null;
00989                 }
00990 
00991                 if(array_key_exists($fname, $this->_sfields)) {
00992                         if($this->_sfields[$fname]['type']=='password') { 
00993                                 $label = ($id && isset($this->_sfields[$fname]['edit_label'])) 
00994                                         ? $this->_sfields[$fname]['edit_label'] 
00995                                         : (isset($this->_sfields[$fname]['insert_label']) ? $this->_sfields[$fname]['insert_label']:'');
00996                                 $req = $id ? false : true;
00997                                 return $myform->cinput($fname."_".$id_f, 'password', '', array(htmlVar(__($fname)), $label), array("required"=>$req, "size"=>gOpt($opts, 'size', 40), "maxlength"=>$field['max_length']));
00998                         }
00999                         elseif($this->_sfields[$fname]['type']=='bool') {
01000                                 $t_l =  $this->_sfields[$fname]['true_label'];
01001                                 $f_l =  $this->_sfields[$fname]['false_label'];
01002                                 $dft =  isset($this->_sfields[$fname]['default']) ? $this->_sfields[$fname]['default'] : 0;
01003                                 return $myform->cradio($fname."_".$id_f, $myform->retvar($fname."_".$id_f, $value), array(1=>$t_l,0=>$f_l), $dft, htmlVar(__($fname)), array("required"=>$required));
01004                         }
01005                         elseif($this->_sfields[$fname]['type']=='enum') {
01006                                 return $myform->cselect($fname."_".$id_f, $myform->retvar($fname."_".$id_f, $value), $this->_sfields[$fname]['data'], htmlVar(__($fname)), array("required"=>$required));
01007                         }
01008                         elseif($this->_sfields[$fname]['type']=='email') {
01009                                 return $myform->cinput($fname."_".$id_f, 'email', $myform->retvar($fname."_".$id_f, $value), htmlVar(__($fname)), array("required"=>$required)); 
01010                         }
01011                         elseif($this->_sfields[$fname]['type']=='multicheck') {
01012                                 $sf = $this->_sfields[$fname];
01013                                 $options = $this->_registry->db->autoSelect(array($sf['key']." AS value", $sf['field']." AS label"), $sf['table'], $sf['where'], $sf['order']);
01014                                 return $myform->cmulticheckbox($fname."_".$id_f."[]", $myform->retvar($fname."_".$id_f, explode(",", $value)), $options, htmlVar(__($fname)), array("required"=>$required));
01015                         }
01016                         elseif($this->_sfields[$fname]['type']=='file' || $this->_sfields[$fname]['type']=='image') {
01017                                 $sf = $this->_sfields[$fname];
01018                                 $preview = isset($sf['preview']) ? $sf['preview'] : false;
01019                                 $rel_path = $sf['rel_path'];
01020                                 return $myform->cinput_file($fname."_".$id_f, $myform->retvar($fname, $value), $sf['label'], array("required"=>$required, "extensions"=>$sf['extensions'], "preview"=>$preview, "rel_path"=>$rel_path));
01021                         }
01022                 }
01023                 elseif(array_key_exists($fname, $this->_fkeys)) {
01024                         $fk = $this->_fkeys[$fname];
01025                         $options = $this->_registry->db->autoSelect(array($fk['key'], $fk['field']), $fk['table'], $fk['where'], $fk['order']);
01026                         $data = array();
01027                         foreach($options as $rec) 
01028                                 $data[htmlInput($rec[$fk['key']])] = htmlVar($rec[$fk['field']]);
01029                         return $myform->cselect($fname."_".$id_f, $myform->retvar($fname."_".$id_f, $value), $data, htmlVar(__($fname)), array("required"=>$required));
01030                 }
01031                 elseif(array_key_exists($fname, $this->_pfields)) {
01032                         return $this->_registry->plugins[$this->_pfields[$fname]['plugin']]->formAdmin($this->_pfields[$fname], $fname."_".$id_f, $fname, $field, $myform, $myform->retvar($fname."_".$id_f, $value));
01033                 }
01034                 elseif($field['type'] == 'int') {
01035                         return $myform->cinput($fname."_".$id_f, 'text', $myform->retvar($fname, $value), htmlVar(__($fname)), array("required"=>$required, "size"=>$field['n_int'], "maxlength"=>$field['n_int']));
01036                 }
01037                 elseif($field['type'] == 'float' || $field['type'] == 'double' || $field['type'] == 'decimal') {
01038                         return $myform->cinput($fname."_".$id_f, 'text', $myform->retvar($fname."_".$id_f, $value), htmlVar(__($fname)), array("required"=>$required, "size"=>($field['n_int']+1+$field['n_precision']), "maxlength"=>($field['n_int']+1+$field['n_precision'])));
01039                 }
01040                 elseif($field['type'] == 'varchar') {
01041                         $size = gOpt($opts, 'size', null) ? gOpt($opts, 'size') : ($field['max_length']<40 ? $field['max_length'] : 40);
01042                         return $myform->cinput($fname."_".$id_f, 'text', $myform->retvar($fname."_".$id_f, $value), htmlVar(__($fname)), array("required"=>$required, "size"=>$size, "maxlength"=>$field['max_length']));
01043                 }
01044                 elseif($field['type'] == 'text') {
01045                         return $myform->ctextarea($fname."_".$id_f, $myform->retvar($fname."_".$id_f, $value), htmlVar(__($fname)), array("required"=>$required, "cols"=>45, "rows"=>6, "editor"=>(in_array($fname, $this->_html_fields) && $this->_editor)  ? true : false));
01046                 }
01047                 elseif($field['type'] == 'date') {
01048                         return $myform->cinput_date($fname."_".$id_f, $myform->retvar($fname."_".$id_f, $value), htmlVar(__($fname)), array("required"=>$required));
01049                 }
01050                 elseif($field['type'] == 'datetime') {
01051                         return $myform->cinput_datetime($fname."_".$id_f, $myform->retvar($fname."_".$id_f, $value), htmlVar(__($fname)), array("required"=>$required));
01052                 }
01053 
01054         }
01055 
01061         public function saveFields() {
01062 
01063                 $myform = new form('post', 'atbl_form', array("validation"=>false));
01064                 $myform->save();
01065 
01066                 // save and continue editing clear session
01067                 if(isset($_SESSION['adminTable_f_s_'.$this->_table])) unset($_SESSION['adminTable_f_s_'.$this->_table]);
01068 
01069                 $res = array();
01070                 $pkeys = cleanInputArray('post', $this->_primary_key, 'string');
01071                 $insert = false;
01072                 if(!$pkeys) { 
01073                         $pkeys = array(0=>null); 
01074                         $insert = true; 
01075                         if(!$this->_insertion) {
01076                                 header("Location: ".preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']));
01077                                 exit();
01078                         }
01079                 }
01080                 else {
01081                         if($this->_edit_deny=='all') {
01082                                 header("Location: ".preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']));
01083                                 exit();
01084                         }
01085                 }
01086 
01087                 if(count($pkeys)) {
01088                         foreach($pkeys as $pk) {
01089                                 $res[] = $this->saveRecord($pk, $pkeys);
01090                         }
01091                 }
01092 
01093                 return $res;
01094 
01095         }
01096 
01105         protected function saveRecord($pk, $pkeys) {
01106 
01107                 if(!in_array($pk, $this->_edit_deny)) {
01108                         $res = array();
01109                         if(is_null($pk)) {
01110                                 $pkf = $pk;
01111                                 $insert = true;
01112                         }
01113                         else {
01114                                 $pkf = preg_replace("#\s#", "_", $pk); // POST replaces spaces with '_'
01115                                 $insert = false;
01116                         }
01117 
01118                         $model = new model($pk, $this->_table);
01119                         $model->setIdName($this->_primary_key);
01120 
01121                         $structure = $this->_registry->db->getTableStructure($this->_table);
01122 
01123                         foreach($this->_fields as $fname=>$field) 
01124                                 if(array_key_exists($fname, $this->_sfields)) 
01125                                         $this->cleanSpecialField($model, $fname, $pkf, $field['type'], $insert);
01126                                 elseif(array_key_exists($fname, $this->_pfields))                                       
01127                                         $this->_registry->plugins[$this->_pfields[$fname]['plugin']]->cleanField($this->_pfields[$fname], $model, $fname, $pkf, $insert);
01128                                 elseif(isset($_POST[$fname."_".$pkf]) && ($fname != $this->_primary_key || is_null($pk)) && $field['extra']!='auto_increment' && in_array($fname, $this->_html_fields)) 
01129                                         $model->{$fname} = $this->cleanField($fname."_".$pkf, 'html');
01130                                 elseif(isset($_POST[$fname."_".$pkf]) && ($fname != $this->_primary_key || is_null($pk)) && $field['extra']!='auto_increment') 
01131                                         $model->{$fname} = $this->cleanField($fname."_".$pkf, $field['type']);
01132                         
01133                         $res = $model->saveData($insert);
01134 
01135                         if(count($this->_pfields)) 
01136                                 foreach($this->_pfields as $fname=>$pf) 
01137                                         if(method_exists($this->_registry->plugins[$pf['plugin']], 'afterModelSaved'))
01138                                                         $this->_registry->plugins[$pf['plugin']]->afterModelSaved($pf, $model, $fname, $this->_primary_key, $insert);
01139 
01140                         if(!$res) {
01141                                 if(!$insert) $_SESSION['adminTable_f_s_'.$this->_table] = $pkeys;
01142                                 $link_error = preg_replace("#\?.*$#", "?".($insert ? "insert" : "edit"), $_SERVER['REQUEST_URI']);
01143                                 $error = $this->_registry->db->getError();
01144 
01145                                 if($error['error']==1001) {
01146                                         $field = $structure['keys'][$error['key']-1];
01147                                         if(isset($this->_fkeys[$field])) {
01148                                                 $fk = $this->_fkeys[$field];
01149                                                 $er_values = $this->_registry->db->autoSelect(array($fk['field']), $fk['table'], $fk['key']."='".$error['value']."'", null);
01150                                                 $er_value = substr($er_values[0][$fk['field']], 0, 50);
01151                                         }
01152                                         else $er_value = substr($error['value'], 0, 50);
01153 
01154                                         $errormsg = sprintf(__('duplicateKeyEntryError'), $error['value'], $field);
01155                                 }
01156                                 exit(error::errorMessage(array('error'=>$errormsg), $link_error));
01157                                 
01158                         }
01159 
01160                         return $model->{$this->_primary_key};
01161                 }
01162         }
01163 
01175         protected function cleanField($name, $type, $opts=null) {
01176 
01177                 if(isset($opts['escape'])) {
01178                         $options = array("escape"=>gOpt($opts, 'escape', true));
01179                 }
01180                 else {
01181                         $options = array();
01182                 }
01183         
01184                 if($type=='int') return cleanInput('post', $name, 'int');
01185                 elseif($type=='float' || $type=='double' || $type=='decimal') return cleanInput('post', $name, 'float');
01186                 elseif($type=='varchar' || $type=='text') return cleanInput('post', $name, 'string', $options);
01187                 elseif($type=='html') return cleanInput('post', $name, 'html', $options);
01188                 elseif($type=='date') return cleanInput('post', $name, 'date');
01189                 elseif($type=='datetime') return cleanInput('post', $name, 'datetime');
01190 
01191         }
01192 
01204         protected function cleanSpecialField($model, $fname, $pk, $type, $insert) {
01205                 
01206                 if($this->_sfields[$fname]['type']=='password') {
01207                         if(!$insert && !cleanInput('post', $fname.'_'.$pk, 'string')) return 0;
01208 
01209                         if(PWD_HASH=='md5') $model->{$fname} = md5(cleanInput('post', $fname.'_'.$pk, 'string'));       
01210                         elseif(PWD_HASH=='sha1') $model->{$fname} = sha1(cleanInput('post', $fname.'_'.$pk, 'string')); 
01211                         else $model->{$fname} = cleanInput('post', $fname.'_'.$pk, 'string');   
01212                 }
01213                 elseif($this->_sfields[$fname]['type']=='bool') $model->{$fname} = cleanInput('post', $fname.'_'.$pk, 'int');
01214                 elseif($this->_sfields[$fname]['type']=='enum') $model->{$fname} = cleanInput('post', $fname.'_'.$pk, $this->_sfields[$fname]['key_type']);
01215                 elseif($this->_sfields[$fname]['type']=='email') $model->{$fname} = cleanInput('post', $fname.'_'.$pk, 'email', $options);
01216                 elseif($this->_sfields[$fname]['type']=='multicheck') {
01217                         $checked = cleanInputArray('post', $fname.'_'.$pk, $this->_sfields[$fname]['value_type']);
01218                         $model->{$fname} = implode(",", $checked);
01219                 }
01220                 elseif($this->_sfields[$fname]['type']=='file') {
01221                         $link_error = preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']);
01222                         $sf = $this->_sfields[$fname];
01223                         $opts['check_content'] = isset($sf['check_content']) ? $sf['check_content'] : true;
01224                         $opts['contents'] = isset($sf['contents_allowed']) ? $sf['contents_allowed'] : null;
01225                         $myform = new form('post', 'atbl_form', array("validation"=>false));
01226                         $model->{$fname} = $myform->uploadFile($fname.'_'.$pk, $sf['extensions'], $sf['path'], $link_error, $opts);
01227                 }
01228                 elseif($this->_sfields[$fname]['type']=='image') {
01229                         $link_error = preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']);
01230                         $sf = $this->_sfields[$fname];
01231                         $opts['resize'] = isset($sf['resize']) ? $sf['resize'] : false;
01232                         $opts['scale'] = isset($sf['scale']) ? $sf['scale'] : false;
01233                         $opts['enlarge'] = isset($sf['resize_enlarge']) ? $sf['resize_enlarge'] : false;
01234                         $opts['make_thumb'] = isset($sf['make_thumb']) ? $sf['make_thumb'] : false;
01235                         $opts['prefix'] = isset($sf['prefix']) ? $sf['prefix'] : '';
01236                         $opts['prefix_thumb'] = isset($sf['prefix_thumb']) ? $sf['prefix_thumb'] : 'thumb_';
01237                         $opts['resize_width'] = isset($sf['resize_width']) ? $sf['resize_width'] : null;
01238                         $opts['resize_height'] = isset($sf['resize_height']) ? $sf['resize_height'] : null;
01239                         $opts['thumb_width'] = isset($sf['thumb_width']) ? $sf['thumb_width'] : null;
01240                         $opts['thumb_height'] = isset($sf['thumb_height']) ? $sf['thumb_height'] : null;
01241                         $myform = new form('post', 'atbl_form', array("validation"=>false));
01242                         $model->{$fname} = $myform->uploadImage($fname.'_'.$pk, $sf['extensions'], $sf['path'], $link_error, $opts);
01243                 }
01244 
01245         }
01246 
01255         protected function export($f_s, $where='') {
01256 
01257                 if(!is_array($f_s) && $f_s!='all') {
01258                         header("Location: ".preg_replace("#\?.*$#", "", $_SERVER['REQUEST_URI']));
01259                         exit();
01260                 }
01261 
01262                 if(is_array($f_s) && count($f_s)) $rids = implode(",", $f_s);
01263                 elseif(!$where) $rids = '*';
01264                 else {
01265                         $rids_a = array();
01266                         $records = $this->_registry->db->autoSelect($this->_primary_key, $this->_table, $where);
01267                         foreach($records as $r) $rids_a[] = $r[$this->_primary_key];
01268                         $rids = implode(",", $rids_a);
01269                 }               
01270 
01271                 $expObj = new export($this->_registry, array("table"=>$this->_table, "pkey"=>$this->_primary_key, "sfields"=>$this->_sfields, "fkeys"=>$this->_fkeys));
01272                 $expObj->setRids($rids);
01273 
01274                 $expObj->exportData($this->_table.'_'.$this->_registry->dtime->now('%Y%m%d').'.csv', 'csv');
01275 
01276                 exit();
01277         
01278         }
01279 
01280 }
01281 
01282 ?>