/** * Ecriture automatique du fichier Metier representant * la structure d'une table de base de donnee. * @param string $tableName Nom de la table * @param array() $columns Liste des champs de la table * @param bool $verbose affiche l'action en cours * @return int Code d'erreur */ public static function write($tableName, $columns, $verbose = false) { if ($verbose) { echo helper::info("Génération du fichier business de {$tableName}\r\n"); } include __DIR__ . DIRECTORY_SEPARATOR . 'datadict.inc.php'; $folder = dirname(dirname(dirname(dirname(dirname(__DIR__))))) . DIRECTORY_SEPARATOR . 'ressources' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'business' . DIRECTORY_SEPARATOR; $field = ""; $construct = " public function __construct()\n {\n"; $getter = ""; $setter = ""; $primary = ""; $class_name = String_::camelize($tableName); $date = date('d/m/Y'); // Entete $txt = <<<EOF <?php /** * Classe d'accès aux données generée automatiquement par daoGenerator. * ATTENTION : NE PAS LA MODIFIER ! * * @name {$class_name} * @copyright PIXXID SARL - {$date} * @licence /LICENCE.txt * @since 1.0 * @author D.M <*****@*****.**> */ namespace xEngine\\Models\\Business; require_once(XENGINE_DIR . '/database/types/fieldString.class.php'); require_once(XENGINE_DIR . '/database/types/fieldInt.class.php'); require_once(XENGINE_DIR . '/database/types/fieldFloat.class.php'); require_once(XENGINE_DIR . '/database/types/fieldDate.class.php'); require_once(XENGINE_DIR . '/database/types/fieldDecimal.class.php'); use xEngine\\Database\\fieldString; use xEngine\\Database\\fieldInt; use xEngine\\Database\\fieldFloat; use xEngine\\Database\\fieldDate; use xEngine\\Database\\fieldDecimal; class {$class_name} { EOF; // Liste des champs de la table if (is_array($columns) && !empty($columns)) { foreach ($columns as $record) { // Recherche de la correspondance du type de la base $key = array_search($record->getType(), $databaseType); $type = $businessType[$key]; // Champ not null if ($record->getNotnull() == true) { $notNull = "true"; } else { $notNull = "false"; } // Champ Valuer par defaut if ($record->getDefaut() == null) { $default = "null"; } else { $default = "\"" . $record->getDefaut() . "\""; } // On efface la valeur par défaut pour certains types if (strtolower($record->getType()) == "set" || strtolower($record->getType()) == "enum") { $record->setLength(0); } // Liste des champs $field .= " private \$" . strtolower($record->getName()) . ";\n"; if ($type != "decimal") { $construct .= " \$this->" . strtolower($record->getName()) . " = new field" . ucfirst($type) . "(\"" . strtolower($record->getName()) . "\", \"" . $record->getType() . "\", " . $record->getLength() . ", " . $notNull . ", " . $default . ");\n"; } else { $construct .= " \$this->" . strtolower($record->getName()) . " = new field" . ucfirst($type) . "(\"" . strtolower($record->getName()) . "\", \"" . $record->getType() . "\", " . $record->getLength() . ", " . $record->getScale() . "," . $notNull . ", " . $default . ");\n"; } // Primary key if ($record->getConstraint() != null && $record->getConstraintType() == "PRI") { $primary .= " \$this->" . strtolower($record->getName()) . "->setConstraintValues(\"PRI\", \"" . $tableName . "\", \"" . strtolower($record->getName()) . "\");\n"; } // Getter $methode_name = String_::camelize($record->getName()); $record_name = strtolower($record->getName()); $getter .= <<<EOF public function get{$methode_name}() { return \$this->{$record_name}; } public function get{$methode_name}Value() { return \$this->{$record_name}->readValue(); } EOF; $ucType = ucfirst($type); $setter .= <<<EOF public function set{$methode_name}(field{$ucType} \$value) { \$this->{$record_name} = \$value; } public function set{$methode_name}Value(\$value) { \$this->{$record_name}->writeValue(\$value); } EOF; } } $txt .= <<<EOF {$field} {$construct} // Ajout des clés primaires {$primary} } // Getter et Setter {$getter} {$setter} } EOF; // Ecriture du fichier return writeFile::write_r($folder, $class_name . ".class.php", $txt); }
/** * Ecriture automatique du fichier Dao personalise representant * une requette qui sera utilisee pour la personalisation. * generer le script (ex : mysql) * @param string $tableName Nom de la table * @param array() $columns Liste des champs de la table * @param bool $overWrite écrase le fichier si déjà existant * @param bool $verbose affiche l'action en cours * @return int Code d'erreur */ public static function write($tableName, $columns, $overWrite = false, $verbose = false) { include __DIR__ . DIRECTORY_SEPARATOR . 'datadict.inc.php'; $class_name = String_::camelize($tableName); $folder = dirname(dirname(dirname(dirname(dirname(__DIR__))))) . DIRECTORY_SEPARATOR . 'ressources' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'daoCust' . DIRECTORY_SEPARATOR; // Avant de générer le daoCust, on vérifie que le fichier n'existe pas déjà if (file_exists($folder . $class_name . "DaoCust.class.php") && !$overWrite) { if ($verbose) { echo helper::warning("Le fichier daoCust de {$tableName} existe déjà.\r\n"); } return 1; } if ($verbose) { echo helper::info("Génération du fichier daoCust de {$tableName}\r\n"); } $businessClass = '/../business/' . $class_name . ".class.php"; $sql_read = ""; $i = 0; $date = date('d/m/Y'); // Entete $txt = <<<EOF <?php /** * Classe d'accès aux données generée automatiquement * par daoGenerator. * Cette classe a pour but d'être personnalisée, une fois * la personalisation effectuée, elle ne doit plus être regenerée. * * @name {$class_name}DaoCust * @copyright PIXXID SARL - {$date} * @licence /LICENCE.txt * @since 1.0 * @author D.M <*****@*****.**> */ namespace xEngine\\Models\\DaoCust; require_once(XENGINE_DIR . '/exception/Exception_.class.php'); require_once(__DIR__ . '{$businessClass}'); use \\xEngine\\Exception\\Exception_; use \\xEngine\\Models\\Business\\{$class_name}; class {$class_name}DaoCust extends {$class_name} { /** * Objet de connexion à la base de données * @access private * @var \\PDO */ private \$conn; private \$message; /** * Constructeur * * @name {$class_name}DaoCust::__construct() * @access public * @param PDO \$conn * @return void */ public function __construct(\\PDO \$conn) { parent::__construct(); \$this->conn = \$conn; } public function getConn() { return \$this->conn; } public function setConn(\\PDO \$conn) { \$this->conn = \$conn; } public function getMessage() { return \$this->message; } EOF; // Liste des champs de la table. if (!empty($columns) && is_array($columns)) { foreach ($columns as $record) { $sql_read .= $record->getName() . ", "; } } // Suppression de la dernière virgule $sql_read = substr($sql_read, 0, strlen($sql_read) - 2); // ================================================ // Ecriture de la fonction readAll // ================================================ $txt .= <<<EOF /** * Liste de tous les éléments de la table * * @name {$class_name}DaoCust::readAll() * @access public * @param int \$fetch_style \\PDO::FETCH_* * * @return mixed array | null */ public function readAll(\$fetch_style = \\PDO::FETCH_ASSOC) { try { \$sql = "SELECT {$sql_read} FROM {$tableName} "; \$stmt = \$this->conn->prepare(\$sql); if (\$stmt->execute() === false) { throw new Exception_(implode('-', \$stmt->errorInfo())); } return \$stmt->fetchAll(\$fetch_style); } catch (Exception_ \$e) { \$this->message = \$e->getMessage(); return null; } } } EOF; // Ecriture du fichier return writeFile::write_r($folder, $class_name . 'DaoCust.class.php', $txt); }
/** * Ecriture automatique du fichier Dao representant * les requettes de base d'acces a la tabele. * @param string $tableName Nom de la table * @param array() $columns Liste des champs de la table * @param bool $verbose affiche l'action en cours * @return int Code d'erreur */ public static function write($tableName, $columns, $verbose = false) { if ($verbose) { echo helper::info("Génération du fichier dao de {$tableName}\r\n"); } include __DIR__ . DIRECTORY_SEPARATOR . 'datadict.inc.php'; $class_name = String_::camelize($tableName); $folder = dirname(dirname(dirname(dirname(dirname(__DIR__))))) . DIRECTORY_SEPARATOR . 'ressources' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'dao' . DIRECTORY_SEPARATOR; $businessClass = '/../business/' . $class_name . ".class.php"; $primary = array(); $sql_read = ""; $sql_where = ""; $param_array = ""; $param_read = ""; $param_insert = ""; $array_insert = ""; $param_update = ""; $nb_cols = 0; $i = 0; $j = 0; $k = 0; $date = date('d/m/Y'); // Entete $txt = <<<EOF <?php /** * Classe d'accès aux données generée automatiquement * ATTENTION : NE PAS LA MODIFIER ! * * @name {$class_name}Dao * @copyright PIXXID SARL - {$date} * @licence /LICENCE.txt * @since 1.0 * @author D.M <*****@*****.**> */ namespace xEngine\\Models\\Dao; require_once(XENGINE_DIR . '/exception/Exception_.class.php'); require_once(__DIR__ . '{$businessClass}'); use \\xEngine\\Exception\\Exception_; use \\xEngine\\Database\\fieldString; use \\xEngine\\Database\\fieldInt; use \\xEngine\\Database\\fieldFloat; use \\xEngine\\Database\\fieldDate; use \\xEngine\\Database\\fieldDecimal; use \\xEngine\\Models\\Business\\{$class_name}; class {$class_name}Dao extends {$class_name} { /** * Objet de connexion à la base de données * @access private * @var \\PDO */ private \$conn; private \$message; /** * Constructeur * * @name {$class_name}Dao::__construct()\\ * @access public * @param \\PDO \$conn Connexion à la base de données * @return void */ public function __construct(\\PDO \$conn) { parent::__construct(); \$this->conn = \$conn; } public function getConn() { return \$this->conn; } public function setConn(\\PDO \$conn) { \$this->conn = \$conn; } public function getMessage() { return \$this->message; } EOF; // Liste des champs de la table. if (is_array($columns) && !empty($columns)) { $nb_cols = sizeof($columns); foreach ($columns as $record) { $sql_read .= $record->getName() . ", "; $param_read .= " \$this->set" . String_::camelize($record->getName()) . "Value(\$row[" . $i . "]);\n"; $param_insert .= "?, "; $array_insert .= "\$this->get" . String_::camelize($record->getName()) . "Value(), "; $param_update .= $record->getName() . " = ?, "; // Primary key if ($record->getConstraint() != null && $record->getConstraintType() == "PRI") { $primary[$k] = $record->getName(); if ($k > 0) { $sql_where .= " AND "; $param_array .= ", "; } $sql_where .= $record->getName() . " = ?"; $param_array .= "\$this->get" . String_::camelize($record->getName()) . "Value()"; $k++; } $i++; } } // On supprime la dernière virgule $sql_read = substr($sql_read, 0, strlen($sql_read) - 2); $param_insert = substr($param_insert, 0, strlen($param_insert) - 2); $array_insert = substr($array_insert, 0, strlen($array_insert) - 2); $param_update = substr($param_update, 0, strlen($param_update) - 2); // ================================================ // Ecriture de la fonction readAll // ================================================ $txt .= <<<EOF /** * Liste de tous les éléments de la table * * @name {$class_name}Dao::readAll() * @access public * @param int \$fetch_style \\PDO::FETCH_* * * @return mixed array | null */ public function readAll(\$fetch_style = \\PDO::FETCH_ASSOC) { try { \$sql = "SELECT {$sql_read} FROM {$tableName} "; \$stmt = \$this->conn->prepare(\$sql); if (\$stmt->execute() === false) { throw new Exception_(implode('-', \$stmt->errorInfo())); } return \$stmt->fetchAll(\$fetch_style); } catch (Exception_ \$e) { \$this->message = \$e->getMessage(); return null; } } EOF; // ================================================ // Ecriture de la fonction read // ================================================ $txt .= <<<EOF /** * Lecture d'un élement de la table * * @name {$class_name}Dao::read() * @access public * * @return boolean */ public function read() { try { \$sql = "SELECT {$sql_read} FROM {$tableName} WHERE {$sql_where}"; \$stmt = \$this->conn->prepare(\$sql); if (!\$stmt->execute(array({$param_array}))) { throw new Exception_(implode('-', \$stmt->errorInfo())); } \$row = \$stmt->fetch(\\PDO::FETCH_NUM); if (\$row === false) { return false; } {$param_read} return true; } catch (Exception_ \$e) { \$this->message = \$e->getMessage(); } return false; } EOF; // ================================================ // Ecriture de la fonction insert // ================================================ $txt .= <<<EOF /** * Insertion d'un élement dans la table * * @name {$class_name}Dao::insert() * @access public * * @return boolean */ public function insert() { try { \$sql = "INSERT INTO {$tableName} ({$sql_read}) VALUES ({$param_insert})"; \$stmt = \$this->conn->prepare(\$sql); if (\$stmt->execute(array({$array_insert})) === false) { throw new Exception_(implode('-', \$stmt->errorInfo())); } } catch (Exception_ \$e) { \$this->message = \$e->getMessage(); return false; } return true; } EOF; // ================================================ // Ecriture de la fonction update // ================================================ $txt .= <<<EOF /** * Mise à jour d'un élément dans la table * * @name {$class_name}Dao::update() * @access public * * @return boolean */ public function update() { try { \$sql = "UPDATE {$tableName} SET {$param_update} WHERE {$sql_where} "; \$stmt = \$this->conn->prepare(\$sql); if (\$stmt->execute(array({$array_insert}, {$param_array})) === false) { throw new Exception_(implode('-', \$stmt->errorInfo())); } } catch (Exception_ \$e) { \$this->message = \$e->getMessage(); return false; } return true; } EOF; // ================================================ // Ecriture de la fonction delete // ================================================ $txt .= <<<EOF /** * Suppression d'un élément dans la table * * @name {$class_name}Dao::delete() * @access public * @return boolean */ public function delete() { try { \$sql = "DELETE FROM {$tableName} WHERE {$sql_where}"; \$stmt = \$this->conn->prepare(\$sql); if (\$stmt->execute(array({$param_array})) === false) { throw new Exception_(implode('-', \$stmt->errorInfo())); } } catch (Exception_ \$e) { \$this->message = \$e->getMessage(); return false; } return true; } } EOF; // Ecriture du fichier return writeFile::write_r($folder, $class_name . "Dao.class.php", $txt); }
/** * Génère un token pour sécuriser les requête intersite (CSRF) * @name tokenInit * @access public * @param string token_id Permet de gérer plusieur Token simultanément * @param bool $force Force la regénération du token * * @return void */ public function tokenInit($token_id = null, $force = false) { if ($token_id != null && ($force === true || !isset($_SESSION[$token_id . '_token']))) { $_SESSION[$token_id . '_token'] = String_::getRandom(16, null, null, 2); } else { if ($force === true || !isset($_SESSION['_token'])) { $_SESSION['_token'] = String_::getRandom(16, null, null, 2); } } }