Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 79 |
| iaParametros | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
812.00 | |
0.00% |
0 / 79 |
| colModelDef | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| keyword2colTemplate | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 21 |
|||
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| get_all | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_value | n/a |
0 / 0 |
2 | n/a |
0 / 0 |
|||||
| has_key | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_fromDb | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| set_all | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 9 |
|||
| set_value | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 9 |
|||
| get_colModel | |
0.00% |
0 / 1 |
72.00 | |
0.00% |
0 / 17 |
|||
| colModel2js | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
| colModel2js_inner | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 6 |
|||
| wordInKey | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 4 |
|||
| toLabel | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| <?php | |
| namespace ia\Lib; | |
| use ia\Util\Str; | |
| /** | |
| * iaParametros: key/value mysql persinstant generates colModel for jqgrid. | |
| * | |
| * Lee, guarda y hace JqGrid.colmodel para un tabla key/value: | |
| * Al extend colModelDef() permite crear el colModel manualmente columna por columna | |
| * Tabla: CREATE TABLE ia_parametros(llave VARCHAR(191) PRIMARY KEY, valor VARCHAR(191) NULL, globito VARCHAR(191) NOT NULL DEFAULT '', registrado_el DATETIME NOT NULL, ultimo_cambio DATETIME NOT NULL) ENGINE=Innodb DEFAULT CHARSET='utf8' COALLITION | |
| * | |
| * @example ver ia_example/iaParamtros.php y sus ajax | |
| * | |
| * class taParam extends iaParametros { | |
| * public function __construct($db) { parent::__construct($db); } | |
| * // ver keyword2colTemplate si existe keyword pone el template para la columna | |
| * // protected $table = 'ia_paremetros'; // si cambia tabla estandard | |
| * public function colModelDef() { // esta es la que implementan en su caso | |
| * // Nota: Poner string values entre ' o " dentro de las comillas de php ejempo ['name':"'nombre'"] | |
| * return [ | |
| * 'monto_compra_minima' =>[name:'"n"',label:'"l"','template'=>"'dinero'",'editoptions'=>['min'=>0,'max'=>999999]], | |
| * ]; | |
| * } | |
| * } | |
| * | |
| * colModel: [ | |
| * {name:'act', formatter:'actions'}, | |
| * {name:'col de la manga no en tabla',}, | |
| * {name:'colEnTablaYaPuesta''}, | |
| * < ?php $defaultColumns = new taParam($gSqlClas); | |
| * echo $defaultColumns->colModel2js( $defaultColumns->get_colModel(['colEnTablaYaPuesta']) ); ? > | |
| * ], | |
| * | |
| * pal ajax [ $gSqlClass->selectKeyValue("SELECT llave,valor FROM ia_parametros ORDER BY llave") ]; | |
| * | |
| * @version 1.0.0 | |
| */ | |
| abstract class iaParametros { | |
| protected $db; | |
| protected $parametros; | |
| protected $parametrosLastUpdated; | |
| protected $parametrosGlobito; | |
| // OVERRIDE START ________________________________________________________________________________________________________________________ | |
| protected $table = 'ia_parametros'; | |
| /** | |
| * Array con los colmodels manualmente agregado | |
| * Poner string values entre ' o " dentro de las comillas de php ejempo ['name':"'nombre'"] | |
| * @return array colModel de columnas cuyo colmodel es manual ['colName'=>[name:'colName', template=>'iaJqGrid.colTemplate.dinero']] | |
| */ | |
| public function colModelDef() { // esta es la que implementan en su caso | |
| // Nota: Poner string values entre ' o " dentro de las comillas de php ejempo ['name':"'nombre'"] | |
| return []; | |
| } | |
| /** | |
| * Determina automáticamente el column template acorde al nombre del campo | |
| * | |
| * @return string el template a usar para la columna en el colModel si tiene el keyword en su nombre (case sensitive) | |
| */ | |
| protected function keyword2colTemplate() { | |
| $var = 'iaJqGrid.colTemplate.'; | |
| return [ | |
| //'param word' => 'colModelTemplate', | |
| 'segundos' => $var.'entero', | |
| 'minutos' => $var.'entero', | |
| 'horas' => $var.'entero', | |
| 'dia' => $var.'entero', | |
| 'dias' => $var.'entero', | |
| 'diario' => $var.'entero', | |
| 'fecha' => $var.'fecha', | |
| 'percent' => $var.'porcentaje', | |
| 'porcentaje' => $var.'porcentaje', | |
| 'puntos' => $var.'entero', //@TODO es entre 100? | |
| 'monto' => $var.'dinero', | |
| 'dinero' => $var.'dinero', | |
| 'tipo_cambio' => $var.'tipoCambio', | |
| 'tipo_de_cambio' => $var.'tipoCambio', | |
| 'cantidad' => $var.'cantidad', | |
| 'email' => $var.'email', | |
| 'url' => $var.'url', | |
| 'SiNoSiRojo' => $var.'SiNoSiRojo', | |
| 'SiNoNoRojo' => $var.'SiNoNoRojo', | |
| 'SiNo' => $var.'SiNo', | |
| ]; | |
| } | |
| // OVERRIDE END | |
| // Constructor _______________________________________________________________________________________________________ | |
| /** | |
| * constructor | |
| * | |
| * @param object $db | |
| * @return void | |
| */ | |
| public function __construct($db) { | |
| $this->db = $db; | |
| $this->get_fromDb(); | |
| } | |
| // Get / exist values _____________________________________________________________________________________________________________ | |
| /** | |
| * Obten array con todas llave/valor | |
| * | |
| * @return array ['key1'=>'value1',...] ordenado por llave | |
| */ | |
| public function get_all() { return $this->parametros; } | |
| /** | |
| * iaParametros::get_value() | |
| * | |
| * @param string $key | |
| * @return mixed Valor de la llave $key o null de no existir la llave | |
| */ | |
| public function get_value($key) { return array_key_exists($key, $this->parametros) ? $this->parametros[$key] : null; } | |
| /** | |
| * Existe la llave? | |
| * | |
| * @param string $key llave a buscar | |
| * @return bool tre la llave existe, false no existe | |
| */ | |
| public function has_key($key) { return array_key_exists($key, $this->parametros); } | |
| // Persist: Read & save to db _____________________________________________________________________________________________________________ | |
| /** | |
| * Lee todos los parametros de la base de datos | |
| * | |
| * @return array ['key1'=>'value1',...] ordenado por llave | |
| */ | |
| public function get_fromDb() { | |
| $this->parametrosLastUpdated = $this->db->selectKeyValue("SELECT /*".__METHOD__."*/ llave, ultimo_cambio FROM ".Str::fieldit($this->table)." ORDER BY llave"); | |
| $this->parametrosGlobito = $this->db->selectKeyValue("SELECT /*".__METHOD__."*/ llave, globito FROM ".Str::fieldit($this->table)." ORDER BY llave"); | |
| return $this->parametros = $this->db->selectKeyValue("SELECT /*".__METHOD__."*/ llave, valor FROM ".Str::fieldit($this->table)." ORDER BY llave"); | |
| } | |
| /** | |
| * Guarda el array recibido en la base de datos | |
| * | |
| * @param array $parametros ['key'=>'value',...] | |
| * @return void | |
| */ | |
| public function set_all($parametros) { | |
| $saveAll = new \inc\sql\mysql\MultipleInsert( | |
| $this->db, | |
| "INSERT /*".__METHOD__."*/ INTO ".Str::fieldit($this->table)."(llave, valor, registrado_el)", | |
| " ON DUPLICATE KEY UPDATE valor=VALUES(valor), ultimo_cambio=NOW()" | |
| ); | |
| foreach($parametros as $key=>$value) { | |
| $saveAll->valuesArray(['key'=>$key, 'valor'=>$value, 'registrado_el'=>'NOW()', 'ultimo_cambio'=>'NOW()']); | |
| } | |
| $saveAll->insertNow(); | |
| $this->get_fromDb(); | |
| } | |
| /** | |
| * Guarda una $llave/$valor en la base de datos | |
| * | |
| * @param string $key | |
| * @param mixed $value | |
| * @return void | |
| */ | |
| public function set_value($key, $value) { | |
| $this->parametrosLastUpdated[$key] = Date('Y-m-d H:i:s'); | |
| $this->parametros[$key] = $value; | |
| $builder = new ia\Sql\Mysql\SqlBuilder(); | |
| $this->db->query($builder->insert( | |
| $this->table, | |
| ['key'=>$key, 'valor'=>$value, 'registrado_el'=>'NOW()', 'ultimo_cambio'=>'NOW()'], | |
| false, | |
| " ON DUPLICATE KEY UPDATE valor=VALUES(valor), ultimo_cambio=NOW()" | |
| )); | |
| } | |
| // Colmodel ______________________________________________________________________________________________________________________ | |
| /** | |
| * iaParametros::get_colModel() | |
| * | |
| * @return array [[colmodel],['name'=>'"nombre"','editoptions'=>[...]]] | |
| */ | |
| public function get_colModel($excludeColumns = []) { | |
| $preDefined = $this->colModelDef(); | |
| $keyword2colTemplate = $this->keyword2colTemplate(); | |
| $colModel = []; | |
| foreach($this->parametros as $key => $value) { | |
| $col = array_key_exists($key, $preDefined) ? $preDefined[$key] : []; | |
| if(!array_key_exists('name', $col)) { | |
| $col['name'] = "'$key'"; | |
| } | |
| if(!array_key_exists('label', $col)) { | |
| $col['label'] = "'" . $this->toLabel($key) . "'"; | |
| } | |
| if(!array_key_exists('headerTitle', $col)) { | |
| $col['headerTitle'] = $col['label'].' + "\\r\\nÚltimo cambio: '.Date('j/M/y H:i', strtotime($this->parametrosLastUpdated[$key]) ).'"'; | |
| } | |
| if(!array_key_exists('template', $col)) { | |
| $template = $this->wordInKey($key, $keyword2colTemplate); | |
| if(!empty($template)) { | |
| $col['template'] = $template; | |
| } | |
| } | |
| $colModel[] = $col; | |
| } | |
| return $colModel; | |
| } | |
| public function colModel2js( $colModel, $indent = 1 ) { | |
| $indentCode = "\r\n\r\n" . str_repeat("\t", $indent) ; | |
| $ret = ""; | |
| foreach ( $colModel as $item ) { | |
| $ret .= $indentCode . "{" . $this->colModel2js_inner( $item, $indent + 1) . $indentCode . "},"; | |
| } | |
| return $ret."\r\n"; | |
| } | |
| protected function colModel2js_inner( $array, $indent ) { | |
| //$indentCode = "\r\n\r\n" . str_repeat("\t", $indent) ; | |
| $ret = ""; | |
| foreach ( $array as $key => $item ) { | |
| if ( is_array($item) ) { | |
| $ret .= "\r\n". str_repeat("\t", $indent) ."'$key':{".$this->colModel2js_inner( $item, $indent + 1)."},"; | |
| } else { | |
| $ret .= "'$key':$item,"; | |
| } | |
| } | |
| return $ret; | |
| } | |
| /** | |
| * Deduce el template a usar en JqGrid.colModel de $key de existir en $keyword2colTemplate | |
| * | |
| * @param string $key | |
| * @param array $keyword2colTemplate ['keyword'=>'template_name',...] | |
| * @see iaParametros::keyword2colTemplate | |
| * @return string con el template name, correspondiente a los keywords existentes en key de $keyword2colTemplate, o '' de no tener keywords | |
| */ | |
| protected function wordInKey($key, $keyword2colTemplate) { | |
| foreach(explode('_', $key) as $word) { | |
| if(array_key_exists($word, $keyword2colTemplate)) { | |
| return $keyword2colTemplate[$word]; | |
| } | |
| } | |
| return ''; | |
| } | |
| /** | |
| * Convierte key name a label | |
| * | |
| * @param string $key | |
| * @return string $key como label | |
| */ | |
| protected function toLabel($key) { | |
| return Str::toLabel( str_replace(['SiNo'], '', str_replace(['SiNoSiRojo','SiNoNoRojo'], '', $key ) ) ); | |
| } | |
| } |