pl = $plname; $this->table = $table; $this->idfield = $idfield; $this->idfield_editable = $editid; $r = XDB::iterator("SHOW COLUMNS FROM $table"); $this->vars = array(); while ($a = $r->next()) { // desc will be the title of the column $a['desc'] = $a['Field']; $a['display'] = true; if (substr($a['Type'],0,8) == 'varchar(') { // limit editing box size $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9); if ($a['Size'] > 40) $a['Size'] = 40; // if too big, put a textarea $a['Type'] = ($a['Maxlength']<200)?'varchar':'varchar200'; } elseif ($a['Type'] == 'text' || $a['Type'] == 'mediumtext') $a['Type'] = 'textarea'; elseif (substr($a['Type'],0,4) == 'set(') { // get the list of options $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7))); $a['Type'] = 'set'; } elseif (substr($a['Type'],0,5) == 'enum(') { // get the list of options $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8))); $a['Type'] = 'enum'; } elseif (substr($a['Type'],0,10) == 'timestamp(') { $a['Type'] = 'timestamp'; } $this->vars[$a['Field']] = $a; } $this->vars[$idfield]['desc'] = 'id'; } // called before editing $entry function prepare_edit(&$entry) { foreach ($this->vars as $field => $descr) { if ($descr['Type'] == 'set') { // get the list of options selected $selected = explode(',', $entry[$field]); $entry[$field] = array(); foreach ($selected as $option) $entry[$field][$option] = 1; } if ($descr['Type'] == 'timestamp') { // set readable timestamp $date =& $entry[$field]; $date = preg_replace('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', '\3/\2/\1 \4:\5:\6', $date); } } return $entry; } // change display of a field function describe($name, $desc, $display) { $this->vars[$name]['desc'] = $desc; $this->vars[$name]['display'] = $display; } // add a join table, when deleting a row corresponding entries will be deleted in these tables function add_join_table($name,$joinid,$joindel,$joinextra="") { if ($joindel) $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):""); } // call when done function apply(&$page, $action, $id = false) { $page->changeTpl('table-editor.tpl'); $list = true; if ($action == 'delete') { foreach ($this->jtables as $table => $j) XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id); XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id); $page->trig("L'entrée ".$id." a été supprimée."); } if ($action == 'edit') { $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id); $entry = $r->fetchOneAssoc(); $page->assign('entry', $this->prepare_edit($entry)); $page->assign('id', $id); $list = false; } if ($action == 'new') { if (!$this->idfield_editable) { $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}"); $page->assign('id', $r->fetchOneCell()); } $list = false; } if ($action == 'update') { $values = ""; $cancel = false; foreach ($this->vars as $field => $descr) { if ($values) $values .= ','; if (($field == $this->idfield) && !$this->idfield_editable) $val = "'".addslashes($id)."'"; elseif ($descr['Type'] == 'set') { $val = ""; if (Post::has($field)) foreach (Post::v($field) as $option) { if ($val) $val .= ','; $val .= $option; } $val = "'".addslashes($val)."'"; } elseif (Post::has($field)) { $val = Post::v($field); if ($descr['Type'] == 'timestamp') { $val = preg_replace('/([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', '\3\2\1\4\5\6', $val); } $val = "'".addslashes($val)."'"; } else { $cancel = true; $page->trig("Il manque le champ ".$field); } $values .= $val; } if (!$cancel) { if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new') XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post::v($this->idfield), $id); XDB::execute("REPLACE INTO {$this->table} VALUES ($values)"); if ($id !== false) $page->trig("L'entrée ".$id." a été mise à jour."); else $page->trig("Une nouvelle entrée a été créée."); } else $page->trig("Impossible de mette à jour."); } if ($list) { $it = XDB::iterator("SELECT * FROM {$this->table}"); $this->nbfields = 0; foreach ($this->vars as $field => $descr) if ($descr['display']) $this->nbfields++; $page->assign('list', $it); } $page->assign('t', $this); } } ?>