forked from djoudi/underQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
underQL.mini.php
1775 lines (1319 loc) · 57.1 KB
/
underQL.mini.php
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
/****************************************************************************************
* Copyright (c) 2012, Abdullah E. Almehmadi - www.abdullaheid.net *
* All rights reserved. *
****************************************************************************************
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
Neither the name of the underQL nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************************/
define ( 'UQL_VERSION', '1.0.0' );
define ( 'UQL_VERSION_ID', 20120512 );
//define('UQL_VERSION_CODE_NAME','Eid');
define ( 'UQL_DIR_FILTER', 'filters/' );
define ( 'UQL_DIR_FILTER_API', 'filters_api/' );
define ( 'UQL_DIR_RULE', 'rules/' );
define ( 'UQL_DIR_RULE_API', 'rules_api/' );
define ( 'UQL_DIR_MODULE', 'modules/' );
//%s represents the table name
//define ('UQL_ABSTRACT_E_OBJECT_SYNTAX','the_%s_abstract');
define ( 'UQL_FILTER_IN', 0xA );
define ( 'UQL_FILTER_OUT', 0xC );
//%s represents table name
define ( 'UQL_FILTER_OBJECT_SYNTAX', '%s_filter' );
define ( 'UQL_FILTER_FUNCTION_NAME', 'ufilter_%s' );
//define ( 'UQL_FILTER_FILE_NAME', 'ufilter_%s' );
//%s represents table name
define ( 'UQL_RULE_OBJECT_SYNTAX', '%s_rule' );
define ( 'UQL_RULE_FUNCTION_NAME', 'urule_%s' );
//define ( 'UQL_RULE_FILE_NAME', 'urule_%s' );
define ( 'UQL_RULE_SUCCESS', 0x0D );
define ( 'UQL_ENTITY_OBJECT_SYNTAX', '%s' );
// %s represents module name
define ( 'UQL_MODULE_OBJECT_SYNTAX', '%s_module' );
define ( 'UQL_MODULE_CLASS_NAME', 'umodule_%s' );
/* Database connection information */
define ( 'UQL_DB_HOST', 'localhost' );
define ( 'UQL_DB_USER', 'root' );
define ( 'UQL_DB_PASSWORD', 'root' );
define ( 'UQL_DB_NAME', 'my' );
define ( 'UQL_DB_CHARSET', 'utf8' );
define ( 'UQL_CONFIG_USE_INVOKE_CALL', true );
// to use __invoke magic method
interface IUQLModule {
public function init();
public function in(&$values,$is_insert = true);
public function out(&$path);
public function shutdown();
}
class UQLBase {
public static function underql_error($message) {
die ( '<h3><code><b style = "color:#FF0000">UnderQL Error: </b>' . $message . '</h3> <br />' );
}
public static function underql_warning($message) {
echo '<h3><code><b style = "color:#0000FF">UnderQL Warning: </b>' . $message . '</h3> <br />';
}
public function _() {
$params_count = func_num_args ();
if ($params_count < 1)
UQLBase::underql_error ( '_ method accepts one parameter at least' );
$params = func_get_args ();
$func_name = 'underql_' . $params [0];
if (! method_exists ( $this, $func_name ))
UQLBase::underql_error ( $params [0] . ' is not a valid method' );
$params = array_slice ( $params, 1 );
return call_user_func_array ( array ($this, $func_name ), $params );
}
}
class UQLModuleEngine extends UQLBase {
public static function underql_module_run_input(&$values,$is_insert = true) {
/* run modules */
if(!$values || !is_array($values) || @count($values) == 0)
return;
if(isset($GLOBALS['uql_global_loaded_modules']) &&
@count($GLOBALS['uql_global_loaded_modules']) != 0) {
foreach($GLOBALS['uql_global_loaded_modules'] as $key => $module_name) {
if(isset($GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)])
&& $GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)]->isActive()
&& $GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)]->isInput())
$GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)]->in($values,$is_insert);
//$this->um_values_map->underql_set_map($current_vals);
}
}
}
public static function underql_module_run_output(&$path) {
/* run modules */
if(!$path || ($path instanceof UQLQueryPath && $path->_('count') == 0))
return;
if(isset($GLOBALS['uql_global_loaded_modules']) &&
@count($GLOBALS['uql_global_loaded_modules']) != 0) {
foreach($GLOBALS['uql_global_loaded_modules'] as $key => $module_name) {
if(isset($GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)])
&& $GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)]->isActive()
&& $GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)]->isOutput()) {
$GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)]->out($path);
$path->_('reset');
}
}
}
}
public static function underql_module_shutdown() {
if(isset($GLOBALS['uql_global_loaded_modules']) &&
@count($GLOBALS['uql_global_loaded_modules']) != 0) {
foreach($GLOBALS['uql_global_loaded_modules'] as $key => $module_name) {
if(isset($GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)]))
$GLOBALS[sprintf(UQL_MODULE_OBJECT_SYNTAX,$module_name)]->shutdown();
}
}
}
}
class UQLModule extends UQLBase {
protected $um_module_name;
protected $um_is_active;
protected $um_is_input;
protected $um_is_output;
public function __construct($module_name,$is_active) {
$this->um_module_name = $module_name;
$this->um_is_active = $is_active;
$this->um_is_input = true; // run (in) method if true
$this->um_is_output = true; // run (out) method if true
}
public function stopModule() {
$this->um_is_active = false;
}
public function restartModule() {
$this->um_is_active = true;
}
public function isActive() {
return $this->um_is_active;
}
public function useInput($use) {
$this->um_is_input = $use;
}
public function useOutput($use) {
$this->um_is_output = $use;
}
public function isInput() {
return $this->um_is_input;
}
public function isOutput() {
return $this->um_is_output;
}
public function __destruct() {
$this->um_module_name = null;
$this->um_is_active = false;
$this->um_is_input = false;
$this->um_is_output = false;
}
}
class UQLConnection extends UQLBase {
private $um_connection_handle;
private $um_database_host;
private $um_database_user_name;
private $um_database_password;
private $um_database_name;
private $um_operations_charset;
public function __construct($host, $database_name, $user = 'root', $password = '', $charset = 'utf8') {
$this->um_database_host = $host;
$this->um_database_name = $database_name;
$this->um_database_user_name = $user;
$this->um_database_password = $password;
$this->um_operations_charset = $charset;
$this->um_connection_handle = null;
$this->underql_start_connection ();
}
public function underql_start_connection() {
$this->um_connection_handle = mysql_connect ( $this->um_database_host, $this->um_database_user_name, $this->um_database_password );
if (! $this->um_connection_handle) {
UQLBase::underql_error ( 'Unable to connect' );
return false;
}
$this->underql_set_database_name ( $this->um_database_name );
$charset_query = sprintf ( "SET NAMES '%s'", $this->um_operations_charset );
mysql_query ( $charset_query );
return $this->um_connection_handle;
}
public function underql_restart_connection() {
$this->underql_close_connection ();
$this->underql_start_connection ();
}
public function underql_get_connection_handle() {
return $this->um_connection_handle;
}
public function underql_set_database_host($host) {
$this->um_database_host = $host;
}
public function underql_get_database_host() {
return $this->um_database_host;
}
public function underql_set_database_name($db_name) {
$this->um_database_name = $db_name;
$result = mysql_select_db ( $this->um_database_name );
if (! $result) {
$this->underql_close_connection ();
$this->underql_error ( 'Unable to select database' );
return false;
}
return true;
}
public function underql_get_database_name() {
return $this->um_database_name;
}
public function underql_set_database_user_name($user) {
$this->um_database_user_name = $user;
}
public function underql_get_database_user_name() {
return $this->um_database_user_name;
}
public function underql_set_database_password($password) {
$this->um_database_password = $password;
}
public function underql_get_database_password() {
return $this->um_database_password;
}
public function underql_set_database_charset($charset, $without_restart = false) {
/* $without_restart : if true, run a query to change charset without need to restarting the connection*/
$this->um_operations_charset = $charset;
if ($without_restart) {
$charset_query = sprintf ( "SET NAMES '%s'", $this->um_operations_charset );
mysql_query ( $charset_query );
}
}
public function underql_get_database_charset() {
return $this->um_operations_charset;
}
public function underql_close_connection() {
if ($this->um_connection_handle)
mysql_close ( $this->um_connection_handle );
$this->um_connection_handle = false;
}
public function __destruct() {
$this->um_database_host = null;
$this->um_database_name = null;
$this->um_database_user_name = null;
$this->um_database_password = null;
$this->um_operations_charset = null;
$this->um_connection_handle = null;
}
}
class UQLMap extends UQLBase {
private $um_map_list;
private $um_elements_count;
public function __construct() {
$this->um_map_list = array ();
$this->um_elements_count = 0;
}
public function underql_add_element($key, $value) {
if ($this->underql_find_element ( $key ) == null)
$this->um_elements_count ++;
$this->um_map_list [$key] = $value;
}
public function underql_find_element($key) {
if ($this->underql_is_element_exist ( $key ))
return $this->um_map_list [$key];
return null;
}
public function underql_is_element_exist($key) {
if ($this->um_elements_count <= 0)
return false;
if (@array_key_exists ( $key, $this->um_map_list ))
return true;
return false;
}
public function underql_get_count() {
return count ( $this->um_map_list );
}
public function underql_remove_element($key) {
if ($this->underql_is_element_exist ( $key )) {
unset ( $this->um_map_list [$key] );
$this->um_elements_count --;
}
}
public function underql_is_empty() {
return $this->um_elements_count == 0;
}
public function underql_map_callback($callback) {
if (! $this->underql_is_empty ())
return array_map ( $callback, $this->map_list );
}
public function underql_get_map() {
return $this->um_map_list;
}
public function underql_set_map($the_map) {
if(is_array($the_map))
$this->um_map_list = $the_map;
}
public function __destruct() {
$this->um_map_list = null;
$this->um_elements_count = 0;
}
}
class UQLAbstractEntity extends UQLBase {
private $um_entity_name;
private $um_fields;
private $um_fields_count;
public function __construct($entity_name, &$database_handle) {
$this->um_entity_name = null;
$this->um_fields = null;
$this->um_fields_count = 0;
$this->underql_set_entity_name ( $entity_name, $database_handle );
}
public function underql_set_entity_name($entity_name, &$database_handle) {
if (($database_handle instanceof UQLConnection)) {
$this->um_entity_name = $entity_name;
$string_query = sprintf ( "SHOW COLUMNS FROM `%s`", $this->um_entity_name );
$query_result = mysql_query ( $string_query );
if ($query_result) {
$this->um_fields_count = mysql_num_rows ( $query_result );
@mysql_free_result ( $query_result );
$fields_list = mysql_list_fields ( $database_handle->underql_get_database_name (), $this->um_entity_name );
$this->um_fields = array ();
$i = 0;
while ( $i < $this->um_fields_count ) {
$field = mysql_fetch_field ( $fields_list );
$this->um_fields [$field->name] = $field;
$i++;
}
@mysql_free_result ( $fields_list );
} else {
UQLBase::underql_error( mysql_error() );
}
}
}
public function underql_get_entity_name() {
return $this->um_entity_name;
}
public function underql_is_field_exist($name) {
return (($this->um_fields != null) && (array_key_exists ( $name, $this->um_fields )));
}
public function underql_get_field_object($name) {
if ($this->underql_is_field_exist ( $name ))
return $this->um_fields [$name];
return null;
}
public function underql_get_all_fields() {
return $this->um_fields;
}
public function underql_get_fields_count() {
return $this->um_fields_count;
}
public function __destruct() {
$this->um_entity_name = null;
$this->um_fields = null;
$this->um_fields_count = 0;
}
}
class UQLFilter extends UQLBase {
private $um_entity_name;
private $um_filters_map;
public function __construct($entity_name) {
$this->um_entity_name = $entity_name;
$this->um_filters_map = new UQLMap ();
}
public function __call($function_name, $parameters) {
$local_params_count = count ( $parameters );
if ($local_params_count < 1 /*filter_name [AT LEAST]*/)
UQLBase::underql_error ( $function_name . ' filter accepts one parameter at lest' );
if ($local_params_count == 1)
$this->underql_add_filter ( $function_name, array ($parameters [0], 'inout' ) );
else
$this->underql_add_filter ( $function_name, $parameters );
return $this;
}
protected function underql_add_filter($field, $filter) {
if (! $this->um_filters_map->underql_is_element_exist ( $field ))
$this->um_filters_map->underql_add_element ( $field, new UQLMap () );
$local_filter = $this->um_filters_map->underql_find_element ( $field );
$local_filter->underql_add_element ( $local_filter->underql_get_count (), array ('filter' => $filter, 'is_active' => true ) );
$this->um_filters_map->underql_add_element ( $field, $local_filter );
}
protected function underql_set_filter_activation($field_name, $filter_name, $activation) {
$local_filter = $this->um_filters_map->underql_find_element ( $field_name );
if (! $local_filter)
UQLBase::underql_error ( 'You can not stop a filter for unknown field (' . $field_name . ')' );
for($i = 0; $i < $local_filter->underql_get_count (); $i ++) {
$target_filter = $local_filter->underql_find_element ( $i );
if (strcmp ( $target_filter ['filter'] [0], $filter_name ) == 0) {
$target_filter ['is_active'] = $activation;
$local_filter->underql_add_element ( $i, array ('filter' => $target_filter ['filter'], 'is_active' => $activation ) );
$this->um_filters_map->underql_add_element ( $field_name, $local_filter );
}
}
}
public function underql_start_filters(/*$field_name,$filter_name*/) {
$params_count = func_num_args ();
if ($params_count < 2)
UQLBase::underql_error ( 'start_filters accepts two parameters at least' );
$filters_counts = $params_count - 1; // remove field name
$parameters = func_get_args ();
if ($filters_counts == 1) {
$this->underql_set_filter_activation ( $parameters [0], $parameters [1], true );
return;
} else {
for($i = 0; $i < $filters_counts - 1; $i ++)
$this->underql_set_filter_activation ( $parameters [0], $parameters [$i + 1], true );
}
}
public function underql_stop_filters(/*$field_name,$filter_name*/) {
$params_count = func_num_args ();
if ($params_count < 2)
UQLBase::error ( 'stop_filters accepts two parameters at least' );
$filters_counts = $params_count - 1; // remove field name
$parameters = func_get_args ();
if ($filters_counts == 1) {
$this->underql_set_filter_activation ( $parameters [0], $parameters [1], false );
return;
} else {
for($i = 0; $i < $filters_counts - 1; $i ++)
$this->underql_set_filter_activation ( $parameters [0], $parameters [$i + 1], false );
}
}
public function underql_get_filters_by_field_name($field_name) {
return $this->um_filters_map->underql_find_element ( $field_name );
}
public function underql_get_filters() {
return $this->um_filters_map;
}
public function underql_get_entity_name() {
return $this->um_entity_name;
}
public static function underql_find_filter_object($entity) {
$filter_object_name = sprintf ( UQL_FILTER_OBJECT_SYNTAX, $entity );
if (isset ( $GLOBALS [$filter_object_name] ))
$filter_object = $GLOBALS [$filter_object_name];
else
$filter_object = null;
return $filter_object;
}
public function __destruct() {
$this->um_entity_name = null;
$this->um_filters_map = null;
}
}
class UQLFilterEngine extends UQLBase {
private $um_filter_object;
private $um_values_map;
//current inserted | updated $key => $value pairs
private $um_in_out_flag;
// specify if the engine for input or output
public function __construct(&$filter_object, $in_out_flag) {
$this->um_filter_object = $filter_object;
$this->um_values_map = null;
$this->um_in_out_flag = $in_out_flag;
}
public function underql_set_values_map(&$values_map) {
$this->um_values_map = $values_map;
}
public function underql_apply_filter($field_name, $value) {
if ($this->um_filter_object != null)
$filters = $this->um_filter_object->underql_get_filters_by_field_name ( $field_name );
else
return $value;
if ($filters == null)
return $value;
$tmp_value = $value;
foreach ( $filters->underql_get_map () as $filter_id => $filter_value ) {
$filter_name = $filter_value ['filter'] [0];
$filter_flag = $filter_value ['filter'] [1];
// echo $filter_flag;
if (strcmp ( strtolower ( $filter_flag ), 'in' ) == 0)
$filter_flag = UQL_FILTER_IN;
else if (strcmp ( strtolower ( $filter_flag ), 'out' ) == 0)
$filter_flag = UQL_FILTER_OUT;
else
$filter_flag = UQL_FILTER_IN | UQL_FILTER_OUT;
if ((! $filter_value ['is_active']) || (($filter_flag != $this->um_in_out_flag) &&($filter_flag != (UQL_FILTER_IN | UQL_FILTER_OUT))))
continue;
$include_filter_api = 'include_filters';
$include_filter_api ( $filter_name );
$filter_api_function = sprintf ( UQL_FILTER_FUNCTION_NAME, $filter_name );
if (! function_exists ( $filter_api_function ))
die ( $filter_name . ' is not a valid filter' );
if (@count ( $filter_value ['filter'] ) == 2) // the filter has no parameter(s)
$tmp_value = $filter_api_function ( $field_name, $tmp_value, $filter_flag );
else {
$params = array_slice ( $filter_value ['filter'], 2 );
$tmp_value = $filter_api_function ( $field_name, $tmp_value, $filter_flag, $params );
}
}
return $tmp_value;
}
public function underql_run_engine() {
if (! $this->um_values_map || $this->um_values_map->underql_get_count () == 0)
return null;
foreach ( $this->um_values_map->underql_get_map () as $name => $value ) {
$this->um_values_map->underql_add_element ( $name, $this->underql_apply_filter ( $name, $value ) );
}
return $this->um_values_map;
}
public function __destruct() {
$this->um_values_map = null;
$this->um_filter_object = null;
}
}
class UQLRule extends UQLBase {
private $um_entity_name;
private $um_alises_map;
private $um_rules_map;
public function __construct($entity_name) {
$this->um_entity_name = $entity_name;
$this->um_alises_map = new UQLMap ();
$this->um_rules_map = new UQLMap ();
}
public function __call($function_name, $parameters) {
$local_params_count = count ( $parameters );
if ($local_params_count == 0)
return;
$this->underql_add_rule ( $function_name, $parameters );
return $this;
}
protected function underql_add_rule($field, $rule) {
if (! $this->um_rules_map->underql_is_element_exist ( $field ))
$this->um_rules_map->underql_add_element ( $field, new UQLMap () );
$local_rule = $this->um_rules_map->underql_find_element ( $field );
$local_rule->underql_add_element ( $local_rule->underql_get_count (), array ('rule' => $rule, 'is_active' => true ) );
$this->um_rules_map->underql_add_element ( $field, $local_rule );
}
protected function underql_set_rule_activation($field_name, $rule_name, $activation) {
$local_rule = $this->um_rules_map->underql_find_element ( $field_name );
if (! $local_rule)
UQLBase::underql_error ( 'You can not stop a rule for unknown field (' . $field_name . ')' );
for($i = 0; $i < $local_rule->underql_get_count (); $i ++) {
$target_rule = $local_rule->underql_find_element ( $i );
if (strcmp ( $target_rule ['rule'] [0], $rule_name ) == 0) {
$target_rule ['is_active'] = $activation;
$local_rule->underql_add_element ( $i, array ('rule' => $target_rule ['rule'], 'is_active' => $activation ) );
$this->um_rules_map->underql_add_element ( $field_name, $local_rule );
}
}
}
public function underql_start_rules(/*$field_name,$rule_name*/) {
$params_count = func_num_args ();
if ($params_count < 2)
UQLBase::underql_error ( 'start_rules accepts two parameters at least' );
$rules_counts = $params_count - 1;
// remove field name
$parameters = func_get_args ();
if ($rules_counts == 1) {
$this->underql_set_rule_activation ( $parameters [0], $parameters [1], true );
return;
} else {
for($i = 0; $i < $rules_counts - 1; $i ++)
$this->underql_set_rule_activation ( $parameters [0], $parameters [$i + 1], true );
}
}
public function underql_stop_rules(/*$field_name,$rule_name*/) {
$params_count = func_num_args ();
if ($params_count < 2)
UQLBase::underql_error ( 'stop_rules accepts two parameters at least' );
$rules_counts = $params_count - 1;
$parameters = func_get_args ();
if ($rules_counts == 1) {
$this->underql_set_rule_activation ( $parameters [0], $parameters [1], false );
return;
} else {
for($i = 0; $i < $rules_counts - 1; $i ++)
$this->underql_set_rule_activation ( $parameters [0], $parameters [$i + 1], false );
}
}
public function underql_get_rules_by_field_name($field_name) {
return $this->um_rules_map->underql_find_element ( $field_name );
}
public function underql_add_alias($key, $value) {
$this->um_alises_map->underql_add_element ( $key, $value );
return $this;
}
public function underql_get_alias($key) {
return $this->um_alises_map->underql_find_element ( $key );
}
public function underql_get_rules() {
return $this->um_alises_map;
}
public function underql_get_entity_name() {
return $this->um_entity_name;
}
public function underql_get_aliases() {
return $this->um_alises_map;
}
public static function underql_find_rule_object($entity) {
$rule_object_name = sprintf ( UQL_RULE_OBJECT_SYNTAX, $entity );
if (isset ( $GLOBALS [$rule_object_name] ))
$rule_object = $GLOBALS [$rule_object_name];
else
$rule_object = null;
return $rule_object;
}
public function __destruct() {
$this->um_entity_name = null;
$this->um_rules_map = null;
$this->um_alises_map = null;
}
}
class UQLRuleMessagesHandler extends UQLBase {
private $um_messages;
public function __construct(&$the_msgs_list) {
if (! $the_msgs_list)
$this->um_messages = array ();
else
$this->um_messages = $the_msgs_list;
}
public function in($field_name) {
return isset ( $this->um_messages [$field_name] );
}
public function at($field_name, $rule_name) {
return isset ( $this->um_messages [$field_name] [$rule_name] ) ;
}
public function get($field_name, $rule_name) {
return $this->um_messages [$field_name] [$rule_name];
}
public function __destruct() {
$this->um_messages = null;
}
}
class UQLRuleEngine extends UQLBase {
private $um_rule_object;
private $um_values_map;
//current inserted | updated $key => $value pairs
private $um_false_rule_flag;
// true if there is at least one rule failed.
private $um_fail_rules_list;
// list of error messages about each field that fail in one or more rules
public function __construct(&$rule_object, &$values_map) {
$this->um_rule_object = $rule_object;
$this->um_values_map = $values_map;
$this->um_false_rule_flag = false;
$this->um_fail_rules_list = new UQLMap ();
}
protected function underql_apply_rule($field_name, $value) {
$rules = $this->um_rule_object->underql_get_rules_by_field_name ( $field_name );
$the_results = array ();
if ($rules == null)
{
$the_results[0] = true; /* There is no rules*/
return $the_results;
}
foreach ( $rules->underql_get_map () as $rule_id => $rule_value ) {
if (! $rule_value ['is_active'])
continue;
$rule_name = $rule_value ['rule'] [0];
$include_rule_api = 'include_rules';
$include_rule_api ( $rule_name );
$rule_api_function = sprintf ( UQL_RULE_FUNCTION_NAME, $rule_name );
if (! function_exists ( $rule_api_function ))
$this->underql_error ( $rule_name . ' is not a valid rule' );
$alias = $this->um_rule_object->underql_get_alias ( $field_name );
if (@count ( $rule_value ['rule'] ) == 1) // the rule has no parameter(s)
$result = $rule_api_function ( $field_name, $value, $alias );
else {
$params = array_alice ( $rule_value ['rule'] );
// remove rule name
$result = $rule_api_function ( $field_name, $value, $alias, $params );
}
if ($result != UQL_RULE_SUCCESS) {
$the_results [$rule_name] = $result;
// message
$this->um_false_rule_flag = true;
} else
$the_results [$rule_name] = true;//$result;
// OK
}
return $the_results;
}
public function underql_are_rules_passed() {
return $this->um_false_rule_flag == false;
}
public function underql_run_engine() {
if (! $this->um_values_map || $this->um_values_map->underql_get_count () == 0)
return true;
$result = true;
foreach ( $this->um_values_map->underql_get_map () as $name => $value ) {
$result = $this->underql_apply_rule ( $name, $value );
foreach($result as $key => $val){
//echo $key. '=>'. $val. '<br />';
if ($val == 1 /*!= UQL_RULE_SUCCESS*/)
continue;
$this->um_fail_rules_list->underql_add_element ( $name, $result );
}
}
if ($this->underql_are_rules_passed ())
return true;
$the_map = $this->um_fail_rules_list->underql_get_map ();
//echo '<pre>'.var_dump($the_map).'</pre>';
return new UQLRuleMessagesHandler ( $the_map );
}
public function __destruct() {
$this->um_values_map = null;
$this->um_rule_object = null;
}
}
class UQLQuery extends UQLBase {
private $um_database_handle;
private $um_query_result;
private $um_current_row_object;
private $um_current_query_fields;
public function __construct(&$database_handle) {
$this->um_database_handle = (($database_handle instanceof UQLConnection) ? $database_handle : null);
$this->um_query_result = null;
$this->um_current_row_object = null;
$this->um_current_query_fields = array ();
}
public function underql_set_database_handle($database_handle) {
$this->underql_database_handle ( ($database_handle instanceof UQLConnection) ? $database_handle : null );
}
public function underql_get_database_handle() {
return $this->um_database_handle;
}
public function underql_execute_query($query) {
if ($this->um_database_handle instanceof UQLConnection) {
$this->um_query_result = mysql_query ( $query /*,$this -> database_handle*/);
$this->underql_is_there_any_error ();
if (! $this->um_query_result)
return false;
return true;
}
return false;
}
public function underql_get_current_query_fields() {
if (! $this->um_query_result)
return null;
$local_fields_count = @mysql_num_fields ( $this->um_query_result );
if ($local_fields_count == 0)
return null;
for($local_i = 0; $local_i < $local_fields_count; $local_i ++)
$this->um_current_query_fields [$local_i] = mysql_field_name ( $this->um_query_result, $local_i );
return $this->um_current_query_fields;
}
public function underql_fetch_row() {
if ($this->um_query_result) {
$this->um_current_row_object = mysql_fetch_object ( $this->um_query_result );
return $this->um_current_row_object;
}
return false;
}