table = $table; $this->rows = $db->getData($table, '*', 'id', 'asc'); $this->columns = $columns; } function __destruct() { } function DbTable($table, $columns) { if (version_compare(PHP_VERSION, "5.0.0", "<")) { $this->__construct($table, $columns); register_shutdown_function(array($this, "__destruct")); } } function render() { if ($this->useDataTable) { $dataTable = 'data-table'; } else { $dataTable = ''; } ?> rows as $row) { static::_printRow($row); } if ($this->needCreate) { static::_printCreate(); } ?>
columns as $col) { $col->printHeader(); } if ($this->showActions) { echo "Action"; } } private function _printRow($row, $create = false) { if ($create) { echo ""; } else { echo ""; } $pk = $action = ''; if (!$create) { $pk = $row['id']; $action = 'update'; } foreach ($this->columns as $col) { if (!empty($row[$col->dbCol])) { $value = $row[$col->dbCol]; } else { $value = ''; } if (!empty($col->tdClass)) { $tdClass = "class='$this->tdClass'"; } else { $tdClass = ""; } echo ""; echo $col->sprintCell($pk, $value, $action); echo ""; } if ($this->showActions) { echo ''; if ($create) { echo " "; } else { echo " "; if ($this->needEdit) { echo "  "; } } echo ""; } echo ""; } private function _printCreate() { self::_printRow(array(), true); } private function _printJS() { if (self::$renderedJS) { return; } self::$renderedJS = true; if (empty($this->ajaxHandler)) { throw new Exception("Please set ajaxHandler!"); } $ajaxCols = array(); foreach ($this->columns as $col) { $ajaxCols[] = "'$col->dbCol'"; } $ajaxCols = implode(", ", $ajaxCols); ?> tableExists($table)) { http_response_code(400); die("Wrong table name: $table!"); } $rows = $db->getData($table, '*', array('id' => "$pk")); if (!empty($rows)) { $row = $rows[0]; } if (empty($format)) { $title = ucwords(str_replace("_", " ", $table)) . ": Details for " . $row[$dbCol]; } else { $title = sprintf($format, $row[$dbCol]); } http_response_code(200); ?>

$val) { $attr = ucwords(str_replace("_", " ", $key)); echo ""; } ?>
$attr$val
tableExists($table)) { http_response_code(400); die("Wrong table name: $table!"); } $rows = $db->getData($table, '*', array('id' => "$pk")); if (!empty($rows)) { $row = $rows[0]; } $title = ucwords(str_replace("_", " ", $table)) . ": Editing $dbCol = {$row[$dbCol]}"; http_response_code(200); ?>

$val) { $attr = ucwords(str_replace("_", " ", $key)); if (!empty($columns[$key])) { $xValue = $columns[$key]->sprintCell($pk, $val, 'update'); } else { $xValue = "$val"; } echo ""; } ?>
$attr$xValue
putMetaData($table, $row); break; case 'subscribe_meta': // fake table name $table = 'product_meta'; if (in_array($name, array('pt1', 'pt2', 'pt3'))) { $multiRow = array(); $n = substr($name, -1); list($p, $t) = self::decodePT1($value, $n); $multiRow[] = array("name" => "p$n", "value" => $p, 'product_id' => $pk); $multiRow[] = array("name" => "t$n", "value" => $t, 'product_id' => $pk); $status = $db->putData($table, $multiRow); } else { $row['name'] = $name; $row['value'] = $value; $row['product_id'] = $pk; $status = $db->putMetaData($table, $row); } break; case 'product_meta': // Special because both name and value are editable $row['id'] = $pk; $row[$name] = $value; $status = $db->putRowData($table, $row); break; case 'templates': $row['name'] = $name; $row['value'] = $value; $row['category_id'] = $pk; $status = $db->putMetaData($table, $row); break; default: http_response_code(400); die("Unknown table accessed: $table"); } return $status; } // AJAX CRUD implementation. Create. static function create($table) { // creates a new DB record if (!EZ::isLoggedIn()) { http_response_code(400); die("Please login before modifying $table!"); } global $db; if (!$db->tableExists($table) && $table != 'subscribe_meta') { http_response_code(400); die("Wrong table name: $table!"); } $row = $_REQUEST; if (!empty($row['pk'])) { http_response_code(400); die("Primary key supplied for new record"); } unset($row['id']); if (empty($row)) { http_response_code(400); die("Empty data"); } switch ($table) { case 'links': case 'link_products': if (!empty($row['category_id'])) { $row['category_id'] = EZ::getCatId($row['category_id']); } if (!empty($row['status'])) { $row['status_date'] = self::mkDateString(time()); } break; case 'categories': if ($row['name'] == 'Empty' || empty($row['name'])) { http_response_code(400); die("Empty name!"); } break; case 'product_meta': break; default: http_response_code(400); die("Unknown table accessed: $table"); } if (isset($row['active']) && (trim($row['active']) == 'Active') || trim($row['active']) == 'Yes') { $row['active'] = 1; } else { $row['active'] = 0; } $lastInsertId = $db->getInsertId(); if (!$db->putRowData($table, $row)) { http_response_code(400); die("Database Insert Error in $table!"); } $newInserId = $db->getInsertId(); if ($lastInsertId == $newInserId) { http_response_code(400); die("Database Insert Error in $table, duplicate unique key!"); } http_response_code(200); return $newInserId; } // AJAX CRUD implementation. Delete. static function read($table) { // Not used. Only for completenss. if (!EZ::isLoggedIn()) { http_response_code(400); die("Please login before reading anything from $table!"); } global $db; if (!$db->tableExists($table)) { http_response_code(400); die("Wrong table name: $table!"); } $posted_pk = ''; extract($_POST, EXTR_PREFIX_ALL, 'posted'); if (empty($posted_pk)) { http_response_code(400); die("Empty primary key to read!"); } http_response_code(200); return $db->getDataEx($table, '*', array('id' => 'pk')); } // AJAX CRUD implementation. Update. static function update($table, $meta = false) { // updates an existing DB record if (!EZ::isLoggedIn()) { http_response_code(400); die("Please login before modifying $table!"); } global $db; if (!$db->tableExists($table) && $table != 'subscribe_meta') { http_response_code(400); die("Wrong table name: $table!"); } $row = array(); $posted_pk = $posted_name = $posted_value = $posted_validator = ''; extract($_POST, EXTR_PREFIX_ALL, 'posted'); if (empty($posted_pk)) { http_response_code(400); die("Empty primary key"); } if (empty($posted_name)) { http_response_code(400); die("Empty name ($posted_name) in data"); } if (!isset($posted_value)) { // Checkbox, unchecked $posted_value = 0; } if (is_array($posted_value)) { // Checkbox (from checklist), checked $posted_value = 1; } if (!empty($posted_validator)) { // a server-side validator is specified $fun = "validate_$posted_validator"; if (method_exists('DbTable', $fun)) { $valid = self::$fun($posted_value); } else { http_response_code(400); die("Unknown validator ($posted_validator) specified"); } if ($valid !== true) { http_response_code(400); die("$valid"); } } if ($meta) { $status = self::updateMetaData($table, $posted_pk, $posted_name, $posted_value); } else { $row['id'] = $posted_pk; $row[$posted_name] = $posted_value; $status = $db->putRowData($table, $row); } if (!$status) { http_response_code(400); die("Database Insert Error in $table!"); } http_response_code(200); exit(); } // AJAX CRUD implementation. Delete. static function delete($table) { if (!EZ::isLoggedIn()) { http_response_code(400); die("Please login before deleting anything from $table!"); } global $db; if (!$db->tableExists($table)) { http_response_code(400); die("Wrong table name: $table!"); } $posted_pk = ''; extract($_POST, EXTR_PREFIX_ALL, 'posted'); if (empty($posted_pk)) { http_response_code(400); die("Empty primary key to delete!"); } $table = $db->prefix($table); $sql = "DELETE FROM $table WHERE `id` = $posted_pk"; $db->query($sql); http_response_code(200); } static function getId($table, $when) { global $db; $row = $db->getData($table, 'id', $when); return $row[0]['id']; } } class DbColumn { var $dbCol, $heading, $width, $type, $align, $source, $validator; var $xClass, $tdClass, $noEdit; function __construct($dbCol) { $this->dbCol = $dbCol; $this->heading = ucwords(str_replace("_", " ", $this->dbCol)); $this->type = "text"; } function __destruct() { } function DbColumn($dbCol) { if (version_compare(PHP_VERSION, "5.0.0", "<")) { $this->__construct($dbCol); register_shutdown_function(array($this, "__destruct")); } } function printHeader() { if (!empty($this->tdClass)) { $tdClass = "class='$this->tdClass'"; } else { $tdClass = ""; } if (!empty($this->width)) { if (empty($this->minWidth)) { $style = "style='width:$this->width'"; } else { $style = "style='width:$this->width;min-width:$this->minWidth'"; } } echo "$this->heading"; } function sprintCell($pk, $value, $action) { $xedit = 'xedit'; $dataValue = $dataValidator = $dataSource = $dataAction = ""; if (!empty($value)) { $dataValue = "data-value='$value'"; } if (!empty($action)) { $dataAction = "data-action='$action'"; } if (!empty($this->validator)) { $dataValidator = "data-validator='$this->validator'"; } $dataType = "data-type='$this->type'"; switch ($this->type) { case 'category': $dataType = "data-type='select'"; $dataValue = "data-value='$value'"; if (!empty($value)) { $value = EZ::getCatName($value); } $dataSource = 'data-source="' . EZ::mkCatSource() . '"'; break; case "select": $dataSource = 'data-source="' . EZ::mkSelectSource($this->source) . '"'; break; case "checklist": if (empty($value)) { $state = 'danger'; $dataValue = "data-value=''"; } else { $state = 'success'; } $xedit = "xedit-checkbox btn-sm btn-$state"; break; default: } if (empty($this->heading)) { $title = ucwords(str_replace("_", " ", $this->dbCol)); } else { $title = $this->heading; } if (empty($this->noEdit)) { return "$value"; } else { return "$value"; } } }