diff options
Diffstat (limited to 'h-source/Library/Model/Base.php')
-rwxr-xr-x | h-source/Library/Model/Base.php | 520 |
1 files changed, 465 insertions, 55 deletions
diff --git a/h-source/Library/Model/Base.php b/h-source/Library/Model/Base.php index cdd1843..67bd91c 100755 --- a/h-source/Library/Model/Base.php +++ b/h-source/Library/Model/Base.php @@ -2,7 +2,7 @@ // EasyGiant is a PHP framework for creating and managing dynamic content // -// Copyright (C) 2009 - 2011 Antonio Gallo +// Copyright (C) 2009 - 2014 Antonio Gallo (info@laboratoriolibero.com) // See COPYRIGHT.txt and LICENSE.txt. // // This file is part of EasyGiant @@ -25,10 +25,12 @@ if (!defined('EG')) die('Direct access not allowed!'); abstract class Model_Base { + public $foreignKeys = array(); //list of foreign keys + public $fields = ''; //the fields that have to be manipulated by the update and insert query public $values = array(); //the values that corresponding to the $this->fields fields public $form = null; //reference to a Form_Form object - public $formStruct = null; //the form structure + public $formStruct = array("entries" => array()); //the form structure public $submitName = null; //the current submitName (from the form) public $identifierName = 'identifier'; @@ -37,15 +39,18 @@ abstract class Model_Base public $result = true; //the result of validate conditions, database conditions and query. It can be 'true' or 'false' public $queryResult = false; //the result of the query - //conditions that have to be satisfied before applying the query - //Ex: 'update'=>'checkEmpty:titolo,autore','submitName'=>'conditions' + //conditions that the $_POST array has to satisfy (strong) public $strongConditions = array(); - //conditions that have to be satisfied before applying the query - //Ex: 'update'=>'checkEmpty:titolo,autore','submitName'=>'conditions' - //do not return error if a value is equal to '' or null + //conditions that the $_POST array has to satisfy (soft) public $softConditions = array(); + //conditions that $this->values has to satisfy (strong) + public $valuesConditions = array(); + + //array where the conditions are temporary saved when the saveConditions is called + public $backupConditions = array(); + //conditions that have to be satisfied before applying the query //check that the new values inserted satisfy some conditions //Ex: 'update'=>'checkUniqueCompl:titolo,autore;checkUnique:titolo','insert'=>'checkUnique:titolo' @@ -57,13 +62,17 @@ abstract class Model_Base public $supplUpdateValues = array(); //associative array defining supplementary values to be inserted on each update query. It has to have the following form: array(field1 => value1,field2 => value2, ...) + public $backupSelect = array(); //where the status of the where clause is stored when the save() method is called public $select = null; //fields that have to be selected in select queries + public $sWhere = null; //string: free where clause public $where = array(); //associative array containing all the where clauses ($field => $value) //group by, order by and limit clauses public $groupBy = null; public $orderBy = null; public $limit = null; + public $convert = false; //It can be tru or false. If true the extracted values are converted from MySQL format to $_lang format + public $from = null; //from clause of the select queries public $on = array(); //on array public $using = array(); //using array @@ -104,6 +113,9 @@ abstract class Model_Base protected $_resultString; //reference to the class containing all the result strings of the db queries protected $_dbCondString; //reference to the class containing all the result strings of the database conditions + protected $_conversionToDbObject = null; //reference to the class to convert the values from current lang formats to MySQL formats + protected $_conversionFromDbObject = null; //reference to the class to convert the values from MySQL formats to current lang formats + protected $_backupFields = ''; //field saved after the delFields method has been applied protected $_backupValues = array(); //values saved after the delFields method has been applied protected $_allowedDbMethods = array('update','insert','del','moveup','movedown'); //methods that can be called by the updateTable method @@ -121,6 +133,7 @@ abstract class Model_Base protected $_arrayStrongCheck; //Array_Validate_Strong object protected $_arraySoftCheck; //Array_Validate_Soft object + protected $_arrayValuesCheck; //Array_Validate_Values object public $db; //reference to the database layer class protected $_lang = null; //language of notices @@ -138,6 +151,7 @@ abstract class Model_Base //initialize the validate objects $this->_arrayStrongCheck = new Array_Validate_Strong($this->_lang); $this->_arraySoftCheck = new Array_Validate_Soft($this->_lang); + $this->_arrayValuesCheck = new Array_Validate_Values($this->_lang); $this->identifierName = $this->_idFieldsArray[0]; @@ -157,6 +171,24 @@ abstract class Model_Base } $this->_dbCondString = new $dbCondStringClass(); + //create the references of the classes to convert to e from MySQL formats + if (DATABASE_TYPE === "Mysqli" or DATABASE_TYPE === "Mysql") + { + $_conversionToDbObject = 'Lang_'.$this->_lang.'_Formats_To_Mysql'; + if (!class_exists($_conversionToDbObject)) + { + $_conversionToDbObject = 'Lang_En_Formats_To_Mysql'; + } + $this->_conversionToDbObject = new $_conversionToDbObject(); + + $_conversionFromDbObject = 'Lang_'.$this->_lang.'_Formats_From_Mysql'; + if (!class_exists($_conversionFromDbObject)) + { + $_conversionFromDbObject = 'Lang_En_Formats_From_Mysql'; + } + $this->_conversionFromDbObject = new $_conversionFromDbObject(); + } + //instantiate the database class $this->db = Factory_Db::getInstance(DATABASE_TYPE); @@ -173,12 +205,22 @@ abstract class Model_Base } //sanitize all the $values property - public function sanitize() + public function sanitize($function = "sanitizeDb") { + if (!function_exists($function)) { + throw new Exception('Error in <b>'.__METHOD__.'</b>: function <b>'.$function.'</b> does not exists.'); + } + $keys = implode(',',array_keys($this->values)); - $this->values = $this->arrayExt->subset($this->values,$keys,'sanitizeDb'); + $this->values = $this->arrayExt->subset($this->values,$keys,$function); } + //return the name of the primary key + public function getPrimaryKey() + { + return $this->_idFields; + } + //change a resulting string from a db query public function setString($key,$value) { @@ -326,7 +368,129 @@ abstract class Model_Base } - + //converts values from MySQl to $_lang format when filling the form with values coming from the DB + public function convertFromMysql($values) + { + if (Params::$automaticConversionFromDbFormat) + { + if (isset($this->_conversionFromDbObject)) + { + //get all types as associative array + $types = $this->db->getTypes($this->_tables, "*", false, true); + + if ($types) + { + $values = $this->convertFromMysqlT($types, $values, $this->db->getEnumTypes()); + } + } + } + + return $values; + } + + //convert an array associaive from MySQL format to $_lang format + //$values: array associative to convert + //$types: types of the elements of the associative array + //$excludeTypes: array of type whose conversion has to be avoided + public function convertFromMysqlT($types, $values, $excludeTypes = array()) + { + foreach ($values as $field => $value) + { + if (array_key_exists($field, $types)) + { + if (!in_array(strtolower($types[$field]),$excludeTypes)) + { + if (method_exists($this->_conversionFromDbObject,strtolower($types[$field]))) + { + $values[$field] = call_user_func(array($this->_conversionFromDbObject, strtolower($types[$field])), $values[$field]); + } + } + } + } + return $values; + } + + //set the default values taking it from DB or from type definition + public function setDefaultFormValues($fields) + { + $returnDefaultValues = array(); + + if (Params::$automaticallySetFormDefaultValues) + { + if (isset($this->_conversionFromDbObject)) + { + //get all types as associative array + $types = $this->db->getTypes($this->_tables, "*", true, true); + + //get all default values as associative array + $defaultValues = $this->db->getDefaultValues($this->_tables, "*", false, true); + + $fieldsArray = explode(",",$fields); + + foreach ($fieldsArray as $field) + { + if (array_key_exists($field,$defaultValues)) + { + if (preg_match('/^('.implode("|",$this->db->getCharTypes()).')/i',$types[$field],$matches) or preg_match('/^('.implode("|",$this->db->getTextTypes()).')/i',$types[$field],$matches)) + { + if (strcmp($defaultValues[$field],"") !== 0) + { + $returnDefaultValues[$field] = $defaultValues[$field]; + } + } + else if (preg_match('/^('.implode("|",$this->db->getIntegerTypes()).')/i',$types[$field],$matches) or preg_match('/^('.implode("|",$this->db->getFloatTypes()).')$/i',$types[$field],$matches) or preg_match('/^('.implode("|",$this->db->getDecimalTypes()).')/i',$types[$field],$matches)) + { + if (strcmp($defaultValues[$field],"") !== 0) + { + $returnDefaultValues[$field] = method_exists($this->_conversionFromDbObject,strtolower($matches[1])) ? call_user_func(array($this->_conversionFromDbObject, strtolower($matches[1])), $defaultValues[$field]) : $defaultValues[$field]; + } + else + { + $returnDefaultValues[$field] = 0; + } + } + else if (preg_match('/^('.implode("|",$this->db->getDateTypes()).')$/i',$types[$field],$matches)) + { + $defDate = Params::$useCurrentDateAsDefaultDate ? date("Y-m-d") : ""; + if (strcmp($defaultValues[$field],"") !== 0) + { + $defDate = $defaultValues[$field]; + } + + if (method_exists($this->_conversionFromDbObject,strtolower($types[$field]))) + { + $returnDefaultValues[$field] = call_user_func(array($this->_conversionFromDbObject, strtolower($types[$field])), $defDate); + } + else + { + $returnDefaultValues[$field] = $defDate; + } + } + else if (preg_match('/^('.implode("|",$this->db->getEnumTypes()).')\((.*?)\)$/i',$types[$field],$matches)) + { + if (strcmp($defaultValues[$field],"") !== 0) + { + $returnDefaultValues[$field] = $defaultValues[$field]; + } + else + { + $temp = array(); + $strings = explode(",",$matches[2]); + for ($i=0;$i<count($strings);$i++) + { + $returnDefaultValues[$field] = trim($strings[$i],"'\""); + break; + } + } + } + } + } + } + } + + return $returnDefaultValues; + } + //return the values, taken from the $_POST array, to be inserted inside the forms //$queryType: insert or update //$func: sanitize function to apply upon each value @@ -369,14 +533,11 @@ abstract class Model_Base { $recordArray = $this->selectId($ident); + $recordArray = $this->convertFromMysql($recordArray); + $fieldsArray = explode(',',$this->fields); - - $values = $this->arrayExt->subset($recordArray,$this->fields,$funcDb); -// foreach ($fieldsArray as $field) -// { -// $values[$field] = array_key_exists($field,$recordArray) ? $recordArray[$field] : ''; -// } + $values = $this->arrayExt->subset($recordArray,$this->fields,$funcDb); $values[$idName] = $ident; @@ -408,8 +569,16 @@ abstract class Model_Base } else if ($queryType === 'insert') { + //set the default values taking it from DB or from type definition + $tempArray = $this->setDefaultFormValues($this->fields); - $tempArray = is_array($defaultValues) ? $defaultValues : array(); + if (is_array($defaultValues)) + { + foreach ($defaultValues as $field => $value) + { + $tempArray[$field] = $value; + } + } $values = $this->arrayExt->subset($tempArray,$this->fields,$funcPost); @@ -528,6 +697,8 @@ abstract class Model_Base //if $whereClause is set then use $whereClause as where clause of the update query public function update($id = null, $whereClause = null) { + $this->notice = null; + if (!is_array($this->supplUpdateValues)) { throw new Exception('error in <b>' . __METHOD__ . '</b>: the <b>supplUpdateValues</b> property has to be an array.'); @@ -535,50 +706,70 @@ abstract class Model_Base $el = $this->setSupplValues('update'); $this->queryResult = false; - if (isset($whereClause)) - { - $result = $this->db->update($this->_tablesArray[0],$el[0],$el[1],$whereClause); - $this->setNotice($result); - return $result; - } - else + if (count($this->values) > 0) { - if (isset($id)) + if (isset($whereClause)) { - $where = $this->_idFieldsArray[0].'='.(int)($id); - $result = $this->db->update($this->_tablesArray[0],$el[0],$el[1],$where); + $result = $this->db->update($this->_tablesArray[0],$el[0],$el[1],$whereClause); $this->setNotice($result); return $result; } else { - $this->notice = $this->_resultString->getString('no-id'); - $this->result = false; - $this->identifierValue = null; - return false; + if (isset($id)) + { + $where = $this->_idFieldsArray[0].'='.(int)($id); + $result = $this->db->update($this->_tablesArray[0],$el[0],$el[1],$where); + $this->setNotice($result); + return $result; + } + else + { + $this->notice .= $this->_resultString->getString('no-id'); + $this->result = false; + $this->identifierValue = null; + return false; + } } } + else + { + $this->notice .= $this->_resultString->getString('no-fields'); + $this->result = true; + $this->queryResult = true; + return false; + } } //method to call the insert query (overriding of the base_db del method) public function insert() { - + $this->notice = null; $this->queryResult = false; if (!is_array($this->supplInsertValues)) { throw new Exception('error in <b>' . __METHOD__ . '</b>: the <b>supplInsertValues</b> property has to be an array.'); } - if (isset($this->_idOrder)) + if (count($this->values) > 0) { - $maxValue = $this->db->getMax($this->_tablesArray[0],$this->_idOrder); - $this->supplInsertValues[$this->_idOrder] = (int)$maxValue + 1; - } - - $el = $this->setSupplValues('insert'); + if (isset($this->_idOrder)) + { + $maxValue = $this->db->getMax($this->_tablesArray[0],$this->_idOrder); + $this->supplInsertValues[$this->_idOrder] = (int)$maxValue + 1; + } + + $el = $this->setSupplValues('insert'); - $result = $this->db->insert($this->_tablesArray[0],$el[0],$el[1]); - $this->setNotice($result); - return $result; + $result = $this->db->insert($this->_tablesArray[0],$el[0],$el[1]); + $this->setNotice($result); + return $result; + } + else + { + $this->notice .= $this->_resultString->getString('no-fields'); + $this->result = true; + $this->queryResult = true; + return false; + } } //method to call the delete query (overriding of the base_db del method) @@ -600,7 +791,7 @@ abstract class Model_Base $this->setNotice($result); return $result; } else { - $this->notice = $this->_resultString->getString('no-id'); + $this->notice .= $this->_resultString->getString('no-id'); $this->result = false; $this->identifierValue = null; return false; @@ -627,6 +818,8 @@ abstract class Model_Base //where clause public function move($id,$par = 'up') { + $this->notice = null; + $this->queryResult = false; if (isset($id)) { @@ -662,7 +855,7 @@ abstract class Model_Base } else { - $this->notice = $this->_resultString->getString('no-id'); + $this->notice .= $this->_resultString->getString('no-id'); $this->result = false; $this->identifierValue = null; return false; @@ -672,11 +865,11 @@ abstract class Model_Base public function setNotice($result) { if ($result) { - $this->notice = $this->_resultString->getString('executed'); + $this->notice .= $this->_resultString->getString('executed'); $this->result = true; $this->queryResult = true; } else { - $this->notice = $this->_resultString->getString('error'); + $this->notice .= $this->_resultString->getString('error'); $this->result = false; $this->queryResult = false; } @@ -798,39 +991,107 @@ abstract class Model_Base } //add a condition to the strongCondition array + public function addDatabaseCondition($queryType,$condition,$field) + { + if ($queryType === "both") + { + $this->addChooseCondition($this->databaseConditions,"insert",$condition,$field); + $this->addChooseCondition($this->databaseConditions,"update",$condition."Compl",$field); + } + else + { + $this->addChooseCondition($this->databaseConditions,$queryType,$condition,$field); + } + } + + //add a condition to the strongCondition array public function addStrongCondition($queryType,$condition,$field) { $this->addChooseCondition($this->strongConditions,$queryType,$condition,$field); } - //add a condition to the strongCondition array + //add a condition to the softCondition array public function addSoftCondition($queryType,$condition,$field) { $this->addChooseCondition($this->softConditions,$queryType,$condition,$field); } + //add a condition to the valuesCondition array + public function addValuesCondition($queryType,$condition,$field) + { + $this->addChooseCondition($this->valuesConditions,$queryType,$condition,$field); + } + + //return the correct conditions array + //$strength: strong,soft,values + public function &getConditions($strength) + { + if ($strength === 'strong') + { + return $this->strongConditions; + } + else if ($strength === 'values') + { + return $this->valuesConditions; + } + else if ($strength === 'database') + { + return $this->databaseConditions; + } + + return $this->softConditions; + } + + //save the conditions + //$strength: strong,soft,values + public function saveConditions($strength) + { + $this->backupConditions[$strength] = $this->getConditions($strength); + } + + //restore the conditions taking them from $this->backupConditions + public function restoreConditions($strength) + { + $c = &$this->getConditions($strength); + + if (isset($this->backupConditions[$strength])) + { + $c = $this->backupConditions[$strength]; + } + } + + //clear the conditions + //$strength: strong,soft,values + public function clearConditions($strength) + { + $c = &$this->getConditions($strength); + $c = array(); + } + //method to apply the validate conditions listed in the $this->strongConditions associative array //$queryType: indicates what set of validate conditions has to be considered (it's the key of the associative array) - //$strength: 'strong' or 'soft' + //$strength: strong,soft,values public function applyValidateConditions($queryType,$strength = 'strong') { + $arrayToCheck = $_POST; if ($strength === 'strong') { $validateObj = $this->_arrayStrongCheck; $conditions = $this->strongConditions; $errString = 'strongConditions'; } + else if ($strength === 'values') + { + $validateObj = $this->_arrayValuesCheck; + $conditions = $this->valuesConditions; + $errString = 'valuesConditions'; + $arrayToCheck = $this->values; + } else { $validateObj = $this->_arraySoftCheck; $conditions = $this->softConditions; $errString = 'softConditions'; - -// if (Params::$nullQueryValue !== false) -// { -// $conditions['insert']['+++++checkIsNotStrings|'.Params::$nullQueryValue] = 'all'; -// $conditions['update']['+++++checkIsNotStrings|'.Params::$nullQueryValue] = 'all'; -// } } if (array_key_exists($queryType,$conditions)) @@ -856,7 +1117,7 @@ abstract class Model_Base $values = $temp[0]; } - $baseArgs = array($_POST,$values); + $baseArgs = array($arrayToCheck,$values); if (strstr($key,'|')) { @@ -889,7 +1150,6 @@ abstract class Model_Base } } - //apply, in sequence, the strong,soft and database conditions //$methodName: insert,update //$id: the id of the record. It is necessary for database conditions @@ -1046,6 +1306,13 @@ abstract class Model_Base return $this; } + //set the $convert property and return the current object + public function convert($convert = true) + { + $this->convert = $convert; + return $this; + } + //set the $from property and return the current object public function from($tables = null) { @@ -1104,6 +1371,12 @@ abstract class Model_Base return $this; } + public function sWhere($sWhere) + { + $this->sWhere = $sWhere; + return $this; + } + //set the $groupBy property and return the current object public function groupBy($groupBy = null) { @@ -1138,6 +1411,7 @@ abstract class Model_Base { $this->select = null; $this->where = array(); + $this->sWhere = null; $this->groupBy = null; $this->orderBy = null; $this->limit = null; @@ -1146,9 +1420,84 @@ abstract class Model_Base $this->using = array(); $this->join = array(); $this->toList = false; + $this->convert = false; return $this; } + //save all the clauses of the select query + public function save() + { + $this->backupSelect["select"] = $this->select; + $this->backupSelect["where"] = $this->where; + $this->backupSelect["sWhere"] = $this->sWhere; + $this->backupSelect["groupBy"] = $this->groupBy; + $this->backupSelect["orderBy"] = $this->orderBy; + $this->backupSelect["limit"] = $this->limit; + $this->backupSelect["from"] = $this->from; + $this->backupSelect["on"] = $this->on; + $this->backupSelect["using"] = $this->using; + $this->backupSelect["join"] = $this->join; + $this->backupSelect["toList"] = $this->toList; + $this->backupSelect["convert"] = $this->convert; + return $this; + } + + //restored all the saved clauses of the select query + public function restore() + { + if (count($this->backupSelect) > 0) + { + $this->select = $this->backupSelect["select"]; + $this->where = $this->backupSelect["where"]; + $this->sWhere = $this->backupSelect["sWhere"]; + $this->groupBy = $this->backupSelect["groupBy"]; + $this->orderBy = $this->backupSelect["orderBy"]; + $this->limit = $this->backupSelect["limit"]; + $this->from = $this->backupSelect["from"]; + $this->on = $this->backupSelect["on"]; + $this->using = $this->backupSelect["using"]; + $this->join = $this->backupSelect["join"]; + $this->toList = $this->backupSelect["toList"]; + $this->convert = $this->backupSelect["convert"]; + } + return $this; + } + + public function getSelectArrayFromEnumField($fieldName) + { + $types = $this->db->getTypes($this->_tables, $fieldName, true, true); + + if ($types) + { + if (preg_match('/^('.implode("|",$this->db->getEnumTypes()).')\((.*?)\)/i',$types[$fieldName],$matches)) + { + return $this->getSelectArrayFromEnumValues($matches[1], $matches[2]); + } + } + } + + public function getSelectArrayFromEnumValues($enumFunc, $enumValues) + { + $enumFunc = strtolower($enumFunc); + + $temp = array(); + $strings = explode(",",$enumValues); + for ($i=0;$i<count($strings);$i++) + { + $val = trim($strings[$i],"'\""); + + if (isset($this->_conversionFromDbObject) and method_exists($this->_conversionFromDbObject, $enumFunc)) + { + $temp[$val] = call_user_func(array($this->_conversionFromDbObject, $enumFunc), $val); + } + else + { + $temp[$val] = $val; + } + } + return $temp; + } + //initialize and populate the ::form property (reference to a Form_Form object) public function setForm($defAction = null, $defSubmit = array(), $defMethod = 'POST', $defEnctype = null) { @@ -1162,6 +1511,67 @@ abstract class Model_Base $this->form = new Form_Form($action,$submit,$method,$enctype); + //get the entries from DB definition + $types = $this->db->getTypes($this->_tables, "*", true, true); + + foreach ($types as $field => $type) + { + $entryType = "InputText"; + $classType = "varchar_input"; + $options = null; + + if (strcmp($field, $this->_idFieldsArray[0]) === 0) + { + $entryType = "Hidden"; + } + else if (preg_match('/^('.implode("|",$this->db->getTextTypes()).')/i',$type,$matches)) + { + $entryType = "Textarea"; + $classType = "text_input"; + } + else if (preg_match('/^('.implode("|",$this->db->getDateTypes()).')/i',$type,$matches)) + { + $classType = "date_input"; + } + else if (preg_match('/^('.implode("|",$this->db->getEnumTypes()).')\((.*?)\)/i',$type,$matches)) + { + $entryType = "Select"; + $classType = "select_input"; + $options = $this->getSelectArrayFromEnumValues($matches[1], $matches[2]); + } + + if (array_key_exists($field,$entries)) + { + if (!array_key_exists("type",$entries[$field])) + { + $entries[$field]["type"] = $entryType; + } + + if ($entryType === "Select" and !array_key_exists("options",$entries[$field])) + { + $entries[$field]["options"] = $options; + $entries[$field]["reverse"] = "yes"; + } + + if (!array_key_exists("className",$entries[$field])) + { + $entries[$field]["className"] = $classType." ".Form_Form::$defaultEntryAttributes['className']; + } + } + else + { + $entries[$field]["type"] = $entryType; + + if ($entryType === "Select") + { + $entries[$field]["options"] = $options; + $entries[$field]["reverse"] = "yes"; + } + + $entries[$field]["className"] = $classType." ".Form_Form::$defaultEntryAttributes['className']; + } + } + if (isset($entries)) { $this->form->setEntries($entries); |