-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.php
More file actions
executable file
·1104 lines (752 loc) · 19.8 KB
/
Table.php
File metadata and controls
executable file
·1104 lines (752 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Zaape Extension Framework
*
* Class for Models
*
* @category Zaape
* @package Zaape_Table
* @copyright No Applied
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version 00.3
*/
/** @see Zend_Db_Table_Abstract */
class Zaape_Table extends Zend_Db_Table_Abstract {
protected $_prefix = null;
protected $_vars = array( );
protected $_sessionVars = array( );
protected $_formVars = array( );
protected $_formValues = array( );
public $_bindObjects = array( );
public $Errors = array();
public $Ecode;
protected $_conn = null;
protected $_rowSet = null;
public $_row = null;
public $_solution = null;
public $_log = null;
public $_selectQuery = null;
public $_prCache = null;
public $_pointer = 0;
public $_session = null;
protected $_Msg = null;
protected $_msgSuccess = null;
protected $initCountRow = null;
protected $paging = null;
protected $_meta = null;
/**
* init().
*
* Initialize functions for Model Metadata
*
* @see Zend_Db_Table_Abstrac::_SetupMetada()
* @see Zend_Db_Table_Abstrac::_setupPrimaryKey()
*
* @param void.
* @return void
*/
public function init() {
try {
$this->_setupMetadata();
} catch (Exception $e) {
//die(var_dump($this));
}
$this->_setupPrimaryKey();
$this->_setUpZaape();
$this->initZaape();
$this->setUpZaape();
//$this->getCache();
}
/**
* test().
*
* Print Metadata table and checkout all its ok
*/
public function test(){
echo "<pre>";
echo "<p>Hi World ;) </p>";
echo "";
echo "</p>Metadata Table: $this->_name </p>";
echo "</p>------------------------</p>";
print_r( $this->_metadata );
echo " </pre>";
}
/**
* _setUpZaape().
*
* Convert to Object to each column in the table
* this information comes from metadata information
*
*
* @param void
* @return void
*
*/
protected function _setUpZaape() {
$i = 0;
foreach ( $this->_metadata as $colum_name => $column_config ) {
$camell = $this->camelize ( $colum_name, $this->_prefix );
$this->$camell = $this->addVar ( $colum_name ) ;
$this->$camell->type ( $column_config ['DATA_TYPE'] );
$i++;
}
}
/**
* setUpZaape().
*
* This a auxiliar function to start the model
*
*
* @param void
* @return void
*
*/
public function setUpZaape(){
//Actions here....
}
/**
* initZaape().
*
* This a auxiliar function to start the model
*
*
* @param void
* @return void
*
*/
public function initZaape(){
//Actions here....
}
/**
* setName().
*
* Set the table name in the model
*
*
* @param String $name;
* @return void
*
*/
public function setName($name){
$this->_name = $name;
}
/**
* __get
*
* se utiliza para consultar datos a partir de propiedades inaccesibles. ...
* Se invoca a los metodos de sobrecarga cuando se interactua con propiedades
* o metodos que no se han declarado o que no son visibles en el ambito activo.
*
* @see http://php.net/manual/es/language.oop5.overloading.php
*
* @param String $name
*/
public function __get( $name )
{
$namelower = strtolower( $name );
if ($this->_rowSet instanceof Zend_Db_Table_Rowset_Abstract) {
if ($this->_rowSet->valid () && isset($this->_rowSet->$namelower)) {
$this->$name = $this->addVar($namelower);
$this->$name->setValue($this->_rowSet->$namelower);
return true;
}
return false;
}elseif ( isset ( $this->_rowSet [ 0 ][ $namelower ] ) ){
$this->$name = $this->addVar($namelower);
$this->$name->setValue( $this->_rowSet[0][$namelower] );
return $this->$name;
} else if ( isset ($this->_rowSet[0]) ){
$row = $this->_rowSet[0];
foreach($row as $column => $value){
$column_name = str_replace('_','',strtolower( $column ));
if ( $column_name == $namelower ){
$value = $this->getRowSetValue($column);
$this->$name = $this->addVar($column);
$this->$name->setValue( $value );
return $this->$name;
}
}
return false;
}
return false;
}
public function __call( $function_name , $vars){
exit( $function_name );
}
/**
*
* Enter description here ...
* @param stdClass $row
*/
public function loadFromStdClass(stdClass $row){
$row = ( $row === null ) ? $this->_row : $row ;
foreach($this->_vars as $column => $var)
if (isset($row->$column))
$var->setValue($row->$column);
}
/**
*
* camelize()
*
* Convert a string into camell format
*
* @param String $name
* @param String $prefix
* @return String $name_camelize
*/
public function camelize($name,$pre = '') {
if ($name != '')
$name = str_replace ( $pre, '', strtolower ( $name ) );
$name = '_' . str_replace ( '_', ' ', strtolower ( $name ) );
return ltrim ( str_replace ( ' ', '', ucwords ( $name ) ), '_' );
}
/**
* Se agrega una variable Zaape_Var a la lista de variables de Model_Object.
*
* @param String $name
* @return Zaape_var (Zaape Var)
*/
protected function addVar($name) {
$Object = new Zaape_Field();
$Object->set ( $name );
return $this->_vars [$name] = $Object;
}
/**
* Alimenta las variables del objeto apartir de un arreglo();
* extFunt: funciones a disparar antes de asignar el valor
* separadar por comas.
*
* @param array() $array
* @param String $extFunt
*/
public function loadFromArray($array,$extFunt = '') {
foreach ( $this->_vars as $var ) {
if (isset ( $array [ $var->Db ()] ) ) {
$var->setValue ( $array [$var->Db ()], $extFunt );
}
}
}
/*
* @name setSelectString
* @param Zend_Select $select
* Salva el query string creado por el Zend_Select
*/
public function setSelectString( $select ){
if( isset( $select ) ){
if( !is_string($select) ){
$this->_selectQuery = $select->__toString();
return;
}
$this->_selectQuery = $select;
}
}
/*
* @name getSelectString
* Salva el query string creado por el Zend_Select
*/
public function getSelectString( ){
return $this->_selectQuery;
}
/**
* Regresa el rowSet del objeto.
* Dependiendo si el rowSet es un objeto o un Array multidimencional.
* Regresa una copia o un puntero al rowSet.
*
* @return &$rowSet(Zen_Db_table_RowSet)| $rowSet(array())
*/
public function getRowSet() {
return $this->_rowSet;
}
/**
* Permite asignar un rowSet desde fuera de la clase.
* NOTA: Esto se hace cuando ahi querys muy elaborados.
* o se alimentan a varios objetos con el mismo resultado.
*
* @param unknown_type $rowSet
*/
public function setRowSet( $rowSet ) {
$this->_rowSet = $rowSet;
if ( $rowSet instanceof Zend_Db_Table_Rowset_Abstract) {
$this->_rowSet = $rowSet->toArray();
//print_r( $this->_rowSet );
}
}
/**
* Recorreo el puntero interno del rowSet a la siguiente posicion.
* y carga los valores a las variables del objeto.
* Si el rowSet llego a su final regresa false.
* en caso contrario regresara true.
*
* @return boolean
*/
public function next() {
$this->clean ();
if (isset ( $this->_rowSet [$this->_pointer] )) {
$this->loadFromArray ( $this->_rowSet [$this->_pointer] );
foreach ( $this->_bindObjects as $object ) {
$object->clean();
$object->loadFromArray ( $this->_rowSet [$this->_pointer] );
}
$this->_pointer ++;
return true;
} else {
return false;
}
return false;
}
/**
*
* Count rowset ...
*/
public function count(){
if (isset($this->_rowSet) )
return count($this->_rowSet);
else
return 0;
}
/**
*
* getTotals from query ...
*
*
*/
public function getTotals(){
return $this->_db->fetchOne('SELECT FOUND_ROWS()');
}
////////////////////////////////////////////////////////////////
/**
* Convierte todas las variables del objeto a un arreglo.
*
* array('nombre_variableA' => 'valor_variableA');
*
* Puedes pasar un arreglo con variables Zaape_vars y solo esas seran convertidas a un arreglo.
*
* @param array $vars
* @return array()
*/
public function toArray($vars = null) {
$array = array ( );
if ($vars === null)
$vars = $this->_vars;
if (is_array($vars)){
foreach ( $vars as $var ){
$array [$var->Db ()] =$var->getValue () ;
}
}
else {
$this->log[] = "Error: toArray() no valid array";
}
return $array;
}
////////////////////////////////////////////////////////////////
/**
* @param Zaape Var $vars
*/
public function insert($vars = null) {
if ($vars === null)
$vars = $this->_vars;
try {
$this->_db->insert ( $this->_name, $this->toArray ( $vars ) );
} catch ( Zend_Db_Statement_Mysqli_Exception $e ) {
echo "<br>" . $e . "<br>";
return false;
}
if (count ( $this->_primary ) == 1) {
$this->_vars [$this->_primary [1]]->setValue ( $this->_db->lastInsertId () );
}
return true;
}
/**
* Actualiza la base de datos con los valores actuales de las variables de la clase.
* Si no pasas un arreglod e variables a la funcion, tomara todas las variables de la clase
* para realizar el update.
*
* Si no pasas una llave primaria, tomara las valores actuales de la clase como llave primaria.
*
* @param Zaape_Var[] $vars
* @param array() $primary
* @return void
*/
public function update($vars = null,$primary = null) {
if ($vars === null){
$vars = $this->_vars;
}
try {
$update = $this->_db->update ( $this->_name, $this->toArray ( $vars ), $this->wherePrimaryKey ( $primary ) );
return $update;
} catch ( Zend_Db_Statement_Exception $e ) {
echo "<br>" . $e . "<br>";
return false;
}
}
/**
* Borra el registro de la base de datos.
* Basandoce en su llave primaria.
*
* @param array()|String $primary
* @return boolean
*/
public function delete($primary = null) {
return parent::delete ( $this->wherePrimaryKey ( $primary ) );
}
///////////////////////////////////////////////////////////
/**
* Obtiene el valor de la columna y el puntero indicado dentro del rowSet Actual.
*
* @param String $name_columns
* @param int $pointer
* @return unknown
*/
public function getRowSetValue($name_columns, $pointer = null) {
if ($pointer === null){//Si no se pasa un puntero toma el actual..
$this->_pointer = ($this->_pointer > 0)?$this->_pointer:1;
$pointer = $this->_pointer - 1;
}
if (isset ( $this->_rowSet [$pointer] [$name_columns] ))
return $this->_rowSet [$pointer] [$name_columns];
}
/**
* Establece el valor de todas las variables del objeto a
* value = '';
*
*/
public function clean() {
foreach ( $this->_vars as $var )
$var->setValue ( '' );
}
/**
* Carga los valores del RowClass a las variables del objeto.
*
* @param Zend_Db_Table_Row_Abstract $row
*/
public function loadFromRowClass(Zend_Db_Table_Row_Abstract $row = null) {
$row = ($row === null) ? $this->_row : $row;
foreach ( $this->_vars as $column => $var )
if (isset ( $row->$column ))
$var->setValue ( $row->$column );
}
/////////////////////////////////////////////////////////////
public function getCache($minutes = 120){
$config = Zend_Registry::get('config');
if( $config->cache ){
$frontendOptions = array(
'lifetime' => $minutes, // cache lifetime of 2 hours
'automatic_serialization' => true
);
$backendOptions = array(
'cache_dir' => realpath( $config->cache->path )
);
//getting a Zend_Cache_Core object
$this->_prCache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);
}
}
/////////////////////////////////////////////////////////////
/**
* Imprime una tabla con todas las variables del objeto y sus valores.
*
*/
public function printValues() {
echo "\n<table class='printValues'>";
echo "\n<tr class='top'><th><b>Nombre</b></th><th><b>Valor</b></th></tr>";
foreach ( $this->_vars as $var ) {
echo "\n<tr><td>" . $var->getName () . "</td><td>" . $var->getValue () . "</td></tr>";
}
echo "\n</table>";
}
/////////////////////////////////////////////////////////////
/**
* Regresa las columnas que componen la llave primaria
*
* @return unknown
*/
public function _primary() {
return $this->_primary;
}
/**
* Regresa las columnas que componen la llave primaria
* con sus valores actuales.
*
* @return unknown
*/
public function _primaryArray() {
$primary = array ( );
foreach ( $this->_primary as $column_name )
$primary [$column_name] = $this->_vars [$column_name]->getValue ();
return $primary;
}
/**
* Regresa el primary del objeto actual.
*
* @return primary
*/
public function getId() {
$id = array ( );
foreach ( $this->_primary as $column_name )
$id [] = $this->_vars [$column_name]->getValue ();
if (count ( $id ) == 1)
list ( $id ) = $id;
return $id;
}
/**
* Regresa un arreglo:
* Key: los nombres de las columnas que componen la llave primaria.
* Value: los valores que recive como parametro que forman la llave primaria que se esta buscando.
*
* @param unknown_type $array
* @return unknown
*/
public function wherePrimaryKey($array = null) {
$where = array ( );
if ($array !== null) {
if (! is_array ( $array ))
$array = array ($array );
foreach ( $this->_primary as $key => $column_name ) {
$where [] = '`'.$column_name.'`' . ' = ' . $this->_db->quote ( (isset ( $array [$key - 1] )) ? $array [$key - 1] : '' );
}
} else {
foreach ( $this->_primary as $key => $column_name ) {
$where [] = '`'.$column_name.'`' . ' = ' . $this->_db->quote ( $this->_vars [$column_name]->getValue () );
}
}
return $where;
}
/////////////////////////////////////////////////////////////
/**
* Carga un registro de la base de datos que concida con la llave primaria.
* Sinonimo de $this->loadByPrimary();
*
* @param array()|String|int $id
* @return boolean
*/
public function loadById($id = null) {
return $this->loadByPrimary ( $id );
}
public function loadByQuery( $query ) {
$this->_pointer = 0;
$this->_last_query = $query;
$this->setRowSet( $this->_db->query( $query )->fetchAll() );
}
/**
* Carga un registro de la base de datos que concida con la llave primaria.
*
* @param array()|String|int $id
* @return boolean
*/
public function loadByPrimary($array = null)
{
$this->_pointer = 0;
if ($array === null && trim($this->_vars [$this->_primary[1]]->getValue ()) == '')
return false;
$strWhere = implode ( ' AND ', $this->wherePrimaryKey ( $array ));
$this->_rowSet = $this->fetchAll ($strWhere );
$count = count ( $this->_rowSet );
if ($count == 1)
return $this->next ();
else
return ($count > 0) ? $count : false;
}
/**
* Cargar todos los registros de la tabla en el rowSet;
* Similar a un: select * from tabla;
*
*/
public function loadAllDb( $strWhere = '' ) {
$this->_pointer = 0;
$this->_rowSet = $this->_db->query('SELECT * FROM '. $this->_name .' '. $strWhere )->fetchAll();
}
/**
* Carga cache de un query via id
*
*/
public function loadByCache( $name, $select = null ) {
if( isset($this->_prCache ) ){
if( !( $result = $this->_prCache->load( $name ) ) ) {
$result = $this->fetchAll( $select );
$this->setSelectString( $select );
$this->_prCache->save( $result );
$this->setRowSet( $result );
}else{
$this->setRowSet( $result );
}
}
}
//////////////////////////////////////////////////////////////////////////////
/**
*
* Get a new mysql guid
*
*/
public function getUnquid( $prefix = null ){
//$prefix = $prefix ? $prefix : $this->_name;
return uniqid( $prefix );
}
/**
*
* Get a new mysql guid
*
*/
public function getGuid(){
$guid = $this->_db->fetchCol ( 'SELECT UUID()' );
return $guid[0];
}
/**
*
* Force a Database close ...
*/
public function DBClose(){
/// Close Database
$this->conn = $this->_db->getConnection();
$result = @$this->_db->query( 'SHOW FULL PROCESSLIST' );
while ( $row = $result->fetch() ) {
$process_id = $row["Id"];
if ($row["Time"] = 50 ) {
$sql = "KILL $process_id ";
$this->conn->query( $sql );
}
}
}
/**
*
* get a session space ...
* @param String $name
*/
public function getSpace($name){
if (!isset($this->_spaces[$name])){
$this->_spaces[$name] = new Zend_Session_Namespace($name);
}
return $this->_spaces[$name];
}
/*----------- MSGs ----------------------*/
/**
*
* Set the new message data for an action ...
* @param String $msg
*/
public function setSuccess( $msg ){
$this->_msgSuccess = $msg;
}
/**
*
* Verify if succes msg is define in the process ...
*/
public function isSuccess(){
if( $this->_msgSuccess ){
return true;
}
return false;
}
/**
*
* pending for now ...
*
*/
public function getSuccessMsg(){
return $this->_msgSuccess;
}
/**
*
* pending for now ...
*
*/
public function getSuccessMsgList(){
}
/**
* get a Form Error in Json format from a Zend_Form validation ...
*
*/
public function getJsonError(){
return $this->_Msg;
}
/**
*
* Return errors from Zend_Form validation ...
* @return String _Msg
*/
public function getMessages(){
return $this->_Msg;
}
public function getCodeResult(){
return $this->ECode;
}
/**
*
* create a list from a Error Msg from Zend_Form validation
*/
public function listErrors(){
if( $this->_Msg ) {
$str = '<ul>';
$strli = '';
foreach( $this->_Msg as $key => $errors ){
$strli .= '<li>';
$strli .= "<strong>$key</strong> <br>";