Commit 2d2fb9c2 authored by Cédric Houbart's avatar Cédric Houbart
Browse files

Revert all master crap

parent 52f80fab
......@@ -23,70 +23,11 @@ namespace Pasteque;
const ABSPATH = __DIR__; // Base path. Also to check if a call
// originates from api.php
// Load
require_once(ABSPATH . "/inc/load.php");
// Check user authentication
if (!is_user_logged_in()) {
$ret = array("error" => "Not logged");
return json_encode($ret);
} else {
require_once(ABSPATH . "/inc/load_logged.php");
$ret = NULL;
if (!isset($_GET[URL_ACTION_PARAM]) || !isset($_GET[URL_API_PARAM])) {
$ret = array("error" => "No such API");
echo json_encode($ret);
return;
} else {
$action = $_GET[URL_ACTION_PARAM];
$modelName = $_GET[URL_API_PARAM];
$def = ModelFactory::get($modelName);
if ($def == NULL) {
$ret = array("error" => "No such API");
echo json_encode($ret);
return;
}
switch ($action) {
case 'search':
$data = ModelService::search($modelName);
$ret = $data->fetchAll();
break;
case 'get':
if (!isset($_GET['id'])) {
$ret = FALSE;
break;
}
$data = ModelService::get($modelName, $_GET['id']);
if (is_array($_GET['id'])) {
$ret = $data->fetchAll();
} else {
$ret = $data;
}
break;
case 'create':
if (!$def->checkForm($_GET)) {
$ret = FALSE;
break;
}
$ret = ModelService::create($modelName, $_GET);
break;
case 'update':
if (!$def->checkForm($_GET) || !isset($_GET['id'])) {
$ret = FALSE;
break;
}
$ret = ModelService::update($modelName, $_GET);
break;
case 'delete':
if (!isset($_GET['id'])) {
$ret = FALSE;
break;
}
$ret = ModelService::delete($modelName, $_GET['id']);
break;
default:
$ret = array("error" => "No such API");
}
}
echo json_encode($ret);
}
// Check credentials
// Call API
......@@ -36,62 +36,23 @@ class MenuEntry {
public function getAction() { return $this->action; }
}
class MenuSection {
private $nameDomain;
private $name;
private $entries;
public function __construct($name, $domain = NULL) {
$this->name = $name;
$this->nameDomain = $domain;
$this->entries = array();
}
public function addEntry($menuEntry) {
$this->entries[] = $menuEntry;
}
public function getName() { return $this->name; }
public function getNameDomain() { return $this->nameDomain; }
public function getEntries() { return $this->entries; }
}
class Menu {
private $sections;
private $entries;
public function __construct() {
$this->sections = array();
$this->addSection("general", "General");
$entry = new MenuEntry("Home", "home");
$this->addEntry("general", $entry);
}
public function addSection($id, $name, $nameDomain = NULL) {
if (!isset($this->sections[$id])) {
$this->sections[$id] = new MenuSection($name, $nameDomain);
return TRUE;
}
return FALSE;
}
public function addEntry($sectionId, $entry) {
if (isset($this->sections[$sectionId])) {
$this->sections[$sectionId]->addEntry($entry);
return TRUE;
}
return FALSE;
$this->entries = array();
$this->entries[] = new MenuEntry("Home", "home");
}
public function registerModuleEntry($sectionId, $module_name, $name,
$action) {
$entry = new MenuEntry($name, get_module_action($module_name, $action),
public function register_module_entry($module_name, $name, $action) {
$this->entries[] = new MenuEntry($name,
get_module_action($module_name, $action),
$module_name);
return $this->addEntry($sectionId, $entry);
}
public function getSections() {
return $this->sections;
public function get_entries() {
return $this->entries;
}
}
......
......@@ -20,7 +20,6 @@
namespace Pasteque;
const URL_API_PARAM = "a";
const URL_ACTION_PARAM = "p";
?>
<?php
// Pastèque Web back office
//
// Copyright (C) 2013 Scil (http://scil.coop)
//
// This file is part of Pastèque.
//
// Pastèque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pastèque is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pastèque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque;
const ATTRDEF_STRING = "str";
const ATTRDEF_INT = "int";
const ATTRDEF_DOUBLE = "double";
const ATTRDEF_DATE = "date";
const ATTRDEF_SINGLEREL = "srel"; // one relationnal model
const ATTRDEF_MULTREL = "mrel"; // multiple relational model (array)
class AttributeDef {
protected $name;
protected $type;
protected $required;
protected $relModel;
public function __construct($name, $type, $args = array()) {
$this->name = $name;
$this->type = $type;
foreach($args as $key => $value) {
switch ($key) {
case 'required':
$this->required = $value;
break;
case 'model': // Model for rel field
$this->relModel = $value;
break;
}
}
}
public function getName() { return $this->name; }
public function getType() { return $this->type; }
public function isRequired() { return $this->required; }
public function getRelModel() { return $this->relModel; }
}
class ExtAttributeDef extends AttributeDef {
protected $table;
public function __construct($name, $type, $table, $args = array()) {
parent::__construct($name, $type, $args);
$this->table = $table;
}
public function getTable() { return $this->table; }
}
/** This is the base class for business models.
* Id and name are mandatory attributes.
*/
class ModelDef {
private $className;
private $attributes;
private $extAttr;
/** Multi rel attributes */
private $mrelAttr;
public function __construct($className) {
$this->className = $className;
$this->attributes = array();
$this->extAttr = array();
$this->mrelAttr = array();
}
public function addAttribute($name, $type, $args = array()) {
$attribute = new AttributeDef($name, $type, $args);
if ($attribute->getType() == ATTRDEF_MULTREL) {
$this->mrelAttr[] = $attribute;
} else {
$this->attributes[] = $attribute;
}
}
public function addExtAttr($name, $type, $table, $args = array()) {
$extAttribute = new ExtAttribute($name, $type, $table, $args);
if ($extAttribute->getType() == ATTRDEF_MULTREL) {
$this->mrelAttr[] = $extAttribute;
return;
}
$table = $extAttribute->getTable();
if (!isset($this->extAttrs[$table])) {
$this->extAttr[$table] = array();
}
$this->extAttr[$table][] = $extAttribute;
}
/** Check an array of data to be used as a model instance. */
public function checkForm(&$f) {
foreach ($this->attributes as $attribute) {
if ($attribute->isRequired() && !isset($f[$attribute->getName()])) {
return FALSE;
}
}
// Remove the dummy form entry for MULTREL attributes
// Must include it to send the value for an empty array
foreach ($this->mrelAttr as $attribute) {
if (isset($f[$attribute->getName()])) {
$idx = array_search("dummy", $f[$attribute->getName()]);
if ($idx !== FALSE) {
array_splice($f[$attribute->getName()], $idx, 1);
}
}
}
return TRUE;
}
public function getName() { return $this->className; }
public function getAttrs() { return $this->attributes; }
public function getExtTables() { return array_keys($this->extAttr); }
public function getExtAttrs($table) { return $this->extAttr[$table]; }
public function getMultirelAttrs() { return $this->mrelAttr; }
}
class MRel {
public static function table($modelDef, $attr) {
return $modelDef->getName() . "_" . $attr->getRelModel();
}
public static function srcField($modelDef) {
return $modelDef->getName() . "_id";
}
public static function destField($attr) {
return $attr->getRelModel() . "_id";
}
}
class ModelFactory {
private static $defs = array();
/** Register a new model definition.
* @param $modelName {string} The name of the model.
* @param $baseTable {string} The table in the database to hold data.
* @return NULL if already registered, the ModelDef if success.
*/
public static function register($modelName) {
if (!isset(ModelFactory::$defs[$modelName])) {
$def = new ModelDef($modelName);
ModelFactory::$defs[$modelName] = $def;
return $def;
}
return NULL;
}
public static function get($modelName) {
if (isset(ModelFactory::$defs[$modelName])) {
return ModelFactory::$defs[$modelName];
}
return NULL;
}
}
<?php
// Pastèque Web back office
//
// Copyright (C) 2013 Scil (http://scil.coop)
//
// This file is part of Pastèque.
//
// Pastèque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pastèque is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pastèque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque;
/** Results of a search to fetch. */
class ModelFetch {
private $modelName;
private $stmt;
private $fields;
public function __construct($modelName, $stmt, $fields) {
$this->modelName = $modelName;
$this->stmt = $stmt;
$this->fields = $fields;
}
public function fetch() {
$def = ModelFactory::get($this->modelName);
$pdo = PDOBuilder::getPDO();
$data = $this->stmt->fetch(\PDO::FETCH_ASSOC);
if ($data === FALSE) {
return NULL;
}
// Multirel fields
foreach ($def->getMultirelAttrs() as $attr) {
if ($this->fields !== NULL
&& array_search($attr->getName(), $this->fields) === FALSE) {
// Not requested
continue;
}
$table = MRel::table($def, $attr);
$rel = $attr->getRelModel();
$srcField = MRel::srcField($def);
$dstField = MRel::destField($attr);
$rsql = "SELECT " . $rel . ".id as id, " . $rel . ".name as name "
. "FROM " . $table . ", " . $rel . " WHERE "
. $table . "." . $srcField . " = " . intval($data['id'])
. " AND " . $table . "." . $dstField . " = " . $rel . ".id";
$relData = $pdo->query($rsql);
if ($relData !== FALSE) {
$data[$attr->getName()] = $relData->fetchAll(\PDO::FETCH_ASSOC);
}
}
// Singlerel fields
foreach ($def->getAttrs() as $attr) {
if ($attr->getType() == ATTRDEF_SINGLEREL) {
$name = $attr->getName();
$data[$name] = array('id' => $data[$name],
'name' => $data[$name . "_name"]);
unset($data[$name . "_name"]);
}
}
return $data;
}
public function fetchAll() {
$ret = array();
while ($data = $this->fetch()) {
$ret[] = $data;
}
return $ret;
}
}
class ModelService {
public static function search($name, $fields = NULL, $args = NULL,
$start = 0, $limit = NULL) {
$def = ModelFactory::get($name);
if ($def == NULL) {
return NULL;
}
$pdo = PDOBuilder::getPDO();
$sql = "SELECT " . ModelService::selectFields($def, $fields);
$sql .= " FROM " . ModelService::selectTables($def, $fields);
// Search
if ($args !== NULL) {
$sql .= " WHERE ";
for ($i = 0; $i < count($args); $i += 3) {
$sql .= $args[$i] . " " . $args[$i + 1] . " " . $args[$i + 2];
$sql .= " AND ";
}
$sql = substr($sql, 0, -5);
}
// Limit
if ($limit != NULL) {
$sql .= "LIMIT " . intval($start) . "," . intval($limit);
}
$data = $pdo->query($sql);
if ($data !== FALSE) {
return new ModelFetch($name, $data, $fields);
} else {
return NULL;
}
}
/** Get a single or multiple data.
* @param $name {string} Model name
* @param $id {mixed} int or array, ids to get
* @param $fields {array} Fields to get
*/
public static function get($name, $id, $fields = NULL) {
$def = ModelFactory::get($name);
if ($def == NULL) {
return NULL;
}
$pdo = PDOBuilder::getPDO();
$sql = "SELECT ";
// Fields
$sql .= ModelService::selectFields($def, $fields);
// Tables
$sql .= " FROM " . $def->getName();
foreach ($def->getExtTables() as $table) {
$sql .= "LEFT JOIN " . $table . " ON " . $table . ".rel_id = "
. $def->getName() . ".id ";
}
// Where
if (is_array($id)) {
$sql .= " WHERE " . $def->getName() . ".id IN (";
foreach ($id as $i) {
$sql .= $i . ", ";
}
$sql = substr($sql, 0, -2);
$sql .= ")";
} else {
$sql .= " WHERE " . $def->getName() . ".id = " . intval($id);
}
$fetch = $pdo->query($sql);
if ($fetch !== FALSE) {
$data = new ModelFetch($name, $fetch, $fields);
if (is_array($id)) {
return $data;
} else {
return $data->fetch();
}
} else {
return NULL;
}
}
public static function delete($name, $id) {
$def = ModelFactory::get($name);
if ($def == NULL) {
return NULL;
}
$pdo = PDOBuilder::getPDO();
$sql = "DELETE FROM " . $def->getName() . " WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":id", $id, \PDO::PARAM_INT);
return $stmt->execute();
}
public static function update($name, $values) {
$def = ModelFactory::get($name);
if ($def == NULL) {
return NULL;
}
$pdo = PDOBuilder::getPDO();
// Update main table
$attrs = array();
$sql = "UPDATE " . $def->getName() . " SET ";
foreach ($def->getAttrs() as $attr) {
// Check attribute in values to build sql
if (isset($values[$attr->getName()])) {
$sql .= $attr->getName() . " = :" . $attr->getName() . ", ";
$attrs[] = $attr;
}
}
$sql = substr($sql, 0, -2);
$sql .= " WHERE id = :id";
// Bind params
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":id", $values['id'], \PDO::PARAM_INT);
foreach ($attrs as $attr) {
$stmt->bindParam(':' . $attr->getName(), $values[$attr->getName()],
ModelService::PDOType($attr->getType()));
}
$stmt->execute();
// Update extension tables
foreach ($def->getExtTables() as $extTable) {
$extSql = "UPDATE " . $extTable . " SET ";
$extAttrs = array();
foreach ($def->getExtAttrs($extTable) as $extAttr) {
$extName = $extAttr->getName();
if (isset($values[$extName])) {
$extSql = $extName . " = :" . $extName . ", ";
$extAttrs[] = $extAttr;
}
}
$extSql = substr($extSql, 0, -2);
$extSql = " WHERE rel_id = :id";
$extStmt = $pdo->prepare($extSql);
$extStmt->bindParam(":id", $values['id'], \PDO::PARAM_INT);
foreach ($extAttrs as $extAttr) {
$extName = $extAttr->getName();
$extStmt->bindParam(':' . $extName, $values[$extName],
ModelService::PDOType($extAttr->getType()));
}
$extStmt->execute();
}
// Update multi rel tables
foreach ($def->getMultirelAttrs() as $attr) {
$relName = $attr->getName();
if (isset($values[$relName])) {
$table = MRel::table($def, $attr);
$srcField = MRel::srcField($def);
$dstField = MRel::destField($attr);
// Delete old values
$delete = "DELETE FROM " . $table . " WHERE " . $srcField
. " = :id";
$delStmt = $pdo->prepare($delete);
$delStmt->bindParam(":id", $values['id']);
$delStmt->execute();
// Insert new ones
if (count($values[$relName]) == 0) {
continue;
}
$insert = "INSERT INTO " . $table . " (" . $srcField . ", "
. $dstField . ") VALUES ";
for ($i = 0; $i < count($values[$relName]); $i++) {
$insert .= "(:src, :dst$i), ";
}
$insert = substr($insert, 0, -2);
$insStmt = $pdo->prepare($insert);
$insStmt->bindParam(":src", $values['id'], \PDO::PARAM_INT);
for ($i = 0; $i < count($values[$relName]); $i++) {
$insStmt->bindParam(":dst$i", $values[$relName][$i],
\PDO::PARAM_INT);
}
$insStmt->execute();
}
}
}
public static function create($name, $values) {
$def = ModelFactory::get($name);
if ($def == NULL) {
return NULL;
}
$pdo = PDOBuilder::getPDO();
// Insert main table
$attrs = array();
$sql = "INSERT INTO " . $def->getName() . " (name";
foreach ($def->getAttrs() as $attr) {
// Check attribute in values to build sql
if (isset($values[$attr->getName()])) {
$sql .= ", " . $attr->getName();
$attrs[] = $attr;
}
}
$sql .= ") VALUES (:name";
foreach ($attrs as $attr) {
$sql .= ", :" . $attr->getName();
}
$sql .= ")";
// Bind params
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":name", $values['name'], \PDO::PARAM_STR);
foreach ($attrs as $attr) {
$stmt->bindParam(':' . $attr->getName(), $values[$attr->getName()],