-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrocery_crud.php
3966 lines (3369 loc) · 113 KB
/
grocery_crud.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 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* PHP grocery CRUD
*
* A Codeigniter library that creates a CRUD automatically with just few lines of code.
*
* Copyright (C) 2010 - 2012 John Skoumbourdis.
*
* LICENSE
*
* Grocery CRUD is released with dual licensing, using the GPL v3 (license-gpl3.txt) and the MIT license (license-mit.txt).
* You don't have to do anything special to choose one license or the other and you don't have to notify anyone which license you are using.
* Please see the corresponding license file for details of these licenses.
* You are free to use, modify and distribute this software, but all copyright information must remain.
*
* @package grocery CRUD
* @copyright Copyright (c) 2010 through 2012, John Skoumbourdis
* @license https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
* @version 1.2
* @author John Skoumbourdis <scoumbourdisj@gmail.com>
*/
// ------------------------------------------------------------------------
/**
* grocery Field Types
*
* The types of the fields and the default reactions
*
* @package grocery CRUD
* @author John Skoumbourdis <scoumbourdisj@gmail.com>
* @license https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
* @link http://www.grocerycrud.com/documentation
*/
class grocery_CRUD_Field_Types
{
/**
* Gets the field types of the main table.
* @return array
*/
public function get_field_types()
{
if($this->field_types !== null)
return $this->field_types;
$types = array();
foreach($this->basic_model->get_field_types_basic_table() as $field_info)
{
$field_info->required = !empty($this->required_fields) && in_array($field_info->name,$this->required_fields) ? true : false;
$field_info->display_as =
isset($this->display_as[$field_info->name]) ?
$this->display_as[$field_info->name] :
ucfirst(str_replace("_"," ",$field_info->name));
if($this->change_field_type != null && isset($this->change_field_type[$field_info->name]))
{
$field_info->crud_type = $this->change_field_type[$field_info->name]->type;
$real_type = $field_info->crud_type;
}
elseif(isset($this->relation[$field_info->name]))
{
$real_type = 'relation';
$field_info->crud_type = 'relation';
}
elseif(isset($this->upload_fields[$field_info->name]))
{
$real_type = 'upload_file';
$field_info->crud_type = 'upload_file';
}
else
{
$real_type = $this->get_type($field_info);
$field_info->crud_type = $real_type;
}
switch ($real_type) {
case 'text':
if(!empty($this->unset_texteditor) && in_array($field_info->name,$this->unset_texteditor))
$field_info->extras = false;
else
$field_info->extras = 'text_editor';
break;
case 'relation':
$field_info->extras = $this->relation[$field_info->name];
break;
case 'upload_file':
$field_info->extras = $this->upload_fields[$field_info->name];
break;
case 'hidden':
if(isset($this->change_field_type[$field_info->name]->value))
$field_info->extras = $this->change_field_type[$field_info->name]->value;
else
$field_info->extras = false;
break;
default:
$field_info->extras = false;
break;
}
$types[$field_info->name] = $field_info;
}
if(!empty($this->relation_n_n))
{
foreach($this->relation_n_n as $field_name => $field_extras)
{
$field_info = (object)array();
$field_info->name = $field_name;
$field_info->crud_type = 'relation_n_n';
$field_info->extras = $field_extras;
$field_info->required = false; //Temporary false
$field_info->display_as =
isset($this->display_as[$field_name]) ?
$this->display_as[$field_name] :
ucfirst(str_replace("_"," ",$field_name));
$types[$field_name] = $field_info;
}
}
if(!empty($this->add_fields))
foreach($this->add_fields as $field_object)
{
$field_name = isset($field_object->field_name) ? $field_object->field_name : $field_object;
if(!isset($types[$field_name]))
{
$field_info = (object)array(
'name' => $field_name,
'crud_type' => 'string',
'display_as' => isset($this->display_as[$field_name]) ?
$this->display_as[$field_name] :
ucfirst(str_replace("_"," ",$field_name)),
'required' => in_array($field_name,$this->required_fields) ? true : false
);
$types[$field_name] = $field_info;
}
}
if(!empty($this->edit_fields))
foreach($this->edit_fields as $field_object)
{
$field_name = isset($field_object->field_name) ? $field_object->field_name : $field_object;
if(!isset($types[$field_name]))
{
$field_info = (object)array(
'name' => $field_name,
'crud_type' => 'string',
'display_as' => isset($this->display_as[$field_name]) ?
$this->display_as[$field_name] :
ucfirst(str_replace("_"," ",$field_name)),
'required' => in_array($field_name,$this->required_fields) ? true : false
);
$types[$field_name] = $field_info;
}
}
$this->field_types = $types;
return $this->field_types;
}
public function get_primary_key()
{
return $this->basic_model->get_primary_key();
}
protected function get_field_input($field_info, $value = null)
{
$real_type = $field_info->crud_type;
switch ($real_type) {
case 'integer':
$field_info->input = $this->get_integer_input($field_info,$value);
break;
case 'true_false':
$field_info->input = $this->get_true_false_input($field_info,$value);
break;
case 'string':
$field_info->input = $this->get_string_input($field_info,$value);
break;
case 'text':
$field_info->input = $this->get_text_input($field_info,$value);
break;
case 'date':
$field_info->input = $this->get_date_input($field_info,$value);
break;
case 'datetime':
$field_info->input = $this->get_datetime_input($field_info,$value);
break;
case 'enum':
$field_info->input = $this->get_enum_input($field_info,$value);
break;
case 'relation':
$field_info->input = $this->get_relation_input($field_info,$value);
break;
case 'relation_n_n':
$field_info->input = $this->get_relation_n_n_input($field_info,$value);
break;
case 'upload_file':
$field_info->input = $this->get_upload_file_input($field_info,$value);
break;
case 'hidden':
$field_info->input = $this->get_hidden_input($field_info,$value);
break;
case 'password':
$field_info->input = $this->get_password_input($field_info,$value);
break;
default:
$field_info->input = $this->get_string_input($field_info,$value);
break;
}
return $field_info;
}
protected function change_list_value($field_info, $value = null)
{
$real_type = $field_info->crud_type;
switch ($real_type) {
case 'hidden':
case 'invisible':
case 'integer':
break;
case 'true_false':
$value = $this->default_true_false_text[$value];
break;
case 'string':
$value = $this->character_limiter($value,30,"...");
break;
case 'text':
$value = $this->character_limiter(strip_tags($value),30,"...");
break;
case 'date':
if(!empty($value) && $value != '0000-00-00' && $value != '1970-01-01')
{
list($year,$month,$day) = explode("-",$value);
$value = date ("d M Y",mktime (0,0,0,(int)$month , (int)$day , (int)$year));
}
else
{
$value = '';
}
break;
case 'datetime':
if(!empty($value) && $value != '0000-00-00 00:00:00' && $value != '1970-01-01 00:00:00')
{
list($year,$month,$day) = explode("-",$value);
list($hours,$minutes) = explode(":",substr($value,11));
$value = date ("d M Y - H:i", mktime ( (int)$hours , (int)$minutes ,0, (int)$month , (int)$day ,(int)$year));
}
else
{
$value = '';
}
break;
case 'enum':
$value = $this->character_limiter($value,20,"...");
break;
case 'relation_n_n':
$value = implode(', ' ,$this->get_relation_n_n_selection_array( $value, $this->relation_n_n[$field_info->name] ));
$value = $this->character_limiter($value,30,"...");
break;
case 'password':
$value = '******';
break;
case 'upload_file':
$value = !empty($value) ?
"<a href='".base_url().$field_info->extras->upload_path."/$value' target='_blank'>".
$this->character_limiter($value,20,"...",true).
"</a>":
"";
break;
default:
$value = $this->character_limiter($value,30,"...");
break;
}
return $value;
}
/**
* Character Limiter of codeigniter (I just don't want to load the helper )
*
* Limits the string based on the character count. Preserves complete words
* so the character count may not be exactly as specified.
*
* @access public
* @param string
* @param integer
* @param string the end character. Usually an ellipsis
* @return string
*/
function character_limiter($str, $n = 500, $end_char = '…', $force = false)
{
if (strlen($str) < $n)
{
return $str;
}
if($force === true)
{
return substr($str,0,$n).$end_char;
}
$str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
if (strlen($str) <= $n)
{
return $str;
}
$out = "";
foreach (explode(' ', trim($str)) as $val)
{
$out .= $val.' ';
if (strlen($out) >= $n)
{
$out = trim($out);
return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
}
}
}
protected function get_type($db_type)
{
$type = false;
if(!empty($db_type->type))
{
switch ($db_type->type) {
case '1':
case '3':
case 'int':
case 'tinyint':
if( $db_type->db_type == 'tinyint' && $db_type->db_max_length == 1)
$type = 'true_false';
else
$type = 'integer';
break;
case '254':
case 'string':
case 'enum':
if($db_type->db_type != 'enum')
$type = 'string';
else
$type = 'enum';
break;
case '252':
case 'blob':
case 'text':
$type = 'text';
break;
case '10':
case 'date':
$type = 'date';
break;
case '12':
case 'datetime':
case 'timestamp':
$type = 'datetime';
break;
}
}
return $type;
}
}
// ------------------------------------------------------------------------
/**
* Grocery Model Driver
*
* Drives the model - I'ts so easy like you drive a bicycle :-)
*
* @package grocery CRUD
* @author John Skoumbourdis <scoumbourdisj@gmail.com>
* @version 1.2
* @link http://www.grocerycrud.com/documentation
*/
class grocery_CRUD_Model_Driver extends grocery_CRUD_Field_Types
{
/**
* @var grocery_CRUD_Model
*/
public $basic_model = null;
protected function set_default_Model()
{
$ci = &get_instance();
$ci->load->model('grocery_CRUD_Model');
$this->basic_model = $ci->grocery_CRUD_Model;
}
protected function get_total_results()
{
if(!empty($this->where))
foreach($this->where as $where)
$this->basic_model->where($where[0],$where[1],$where[2]);
if(!empty($this->or_where))
foreach($this->or_where as $or_where)
$this->basic_model->or_where($or_where[0],$or_where[1],$or_where[2]);
if(!empty($this->like))
foreach($this->like as $like)
$this->basic_model->like($like[0],$like[1],$like[2]);
if(!empty($this->or_like))
foreach($this->or_like as $or_like)
$this->basic_model->or_like($or_like[0],$or_like[1],$or_like[2]);
if(!empty($this->relation))
foreach($this->relation as $relation)
$this->basic_model->join_relation($relation[0],$relation[1],$relation[2]);
return $this->basic_model->get_total_results();
}
public function set_model($model_name)
{
$ci = &get_instance();
$ci->load->model('grocery_CRUD_Model');
$ci->load->model($model_name);
$temp = explode('/',$model_name);
krsort($temp);
foreach($temp as $t)
{
$real_model_name = $t;
break;
}
$this->basic_model = $ci->$real_model_name;
}
protected function set_ajax_list_queries($state_info = null)
{
if(!empty($state_info->per_page))
{
if(empty($state_info->page) || !is_numeric($state_info->page) )
$this->limit($state_info->per_page);
else
{
$limit_page = ( ($state_info->page-1) * $state_info->per_page );
$this->limit($state_info->per_page, $limit_page);
}
}
if(!empty($state_info->order_by))
{
$this->order_by($state_info->order_by[0],$state_info->order_by[1]);
}
if(!empty($state_info->search))
{
if(!empty($this->relation))
foreach($this->relation as $relation_name => $relation_values)
$temp_relation[$this->_unique_field_name($relation_name)] = $this->_get_field_names_to_search($relation_values);
if($state_info->search->field != null)
{
if(isset($temp_relation[$state_info->search->field]))
{
if(is_array($temp_relation[$state_info->search->field]))
foreach($temp_relation[$state_info->search->field] as $search_field)
$this->or_like($search_field , $state_info->search->text);
else
$this->like($temp_relation[$state_info->search->field] , $state_info->search->text);
}
else
{
$this->like($state_info->search->field , $state_info->search->text);
}
}
else
{
$columns = $this->get_columns();
#region temporary solution for the search with relation_n_n
if(!empty($this->relation_n_n))
foreach($columns as $num_row => $column)
if(isset($this->relation_n_n[$column->field_name]))
unset($columns[$num_row]);
#endregion
$search_text = $state_info->search->text;
foreach($columns as $column)
if(isset($temp_relation[$column->field_name]))
if(is_array($temp_relation[$column->field_name]))
foreach($temp_relation[$column->field_name] as $search_field)
$this->or_like($search_field, $search_text);
else
$this->or_like($temp_relation[$column->field_name], $search_text);
else
$this->or_like($column->field_name, $search_text);
}
}
}
protected function table_exists($table_name = null)
{
if($this->basic_model->db_table_exists($table_name))
return true;
return false;
}
protected function get_relation_array($relation_info, $primary_key_value = null, $limit = null)
{
list($field_name , $related_table , $related_field_title, $where_clause, $order_by) = $relation_info;
if($primary_key_value !== null)
{
$primary_key = $this->basic_model->get_primary_key($related_table);
//A where clause with the primary key is enough to take the selected key row
$where_clause = array($primary_key => $primary_key_value);
}
$relation_array = $this->basic_model->get_relation_array($field_name , $related_table , $related_field_title, $where_clause, $order_by, $limit);
return $relation_array;
}
protected function get_relation_total_rows($relation_info)
{
list($field_name , $related_table , $related_field_title, $where_clause) = $relation_info;
$relation_array = $this->basic_model->get_relation_total_rows($field_name , $related_table , $related_field_title, $where_clause);
return $relation_array;
}
protected function db_insert_validation()
{
$validation_result = (object)array('success'=>false);
$field_types = $this->get_field_types();
$required_fields = $this->required_fields;
$add_fields = $this->get_add_fields();
if(!empty($required_fields))
{
foreach($add_fields as $add_field)
{
$field_name = $add_field->field_name;
if(!isset($this->validation_rules[$field_name]) && in_array( $field_name, $required_fields) )
{
$this->set_rules( $field_name, $field_types[$field_name]->display_as, 'required');
}
}
}
if(!empty($this->validation_rules))
{
$form_validation = $this->form_validation();
$add_fields = $this->get_add_fields();
foreach($add_fields as $add_field)
{
$field_name = $add_field->field_name;
if(isset($this->validation_rules[$field_name]))
{
$rule = $this->validation_rules[$field_name];
$form_validation->set_rules($rule['field'],$rule['label'],$rule['rules']);
}
}
if($form_validation->run())
{
$validation_result->success = true;
}
else
{
$validation_result->error_message = $form_validation->error_string();
$validation_result->error_fields = $form_validation->_error_array;
}
}
else
{
$validation_result->success = true;
}
return $validation_result;
}
protected function form_validation()
{
if($this->form_validation === null)
{
$this->form_validation = new grocery_CRUD_Form_validation();
$ci = &get_instance();
$ci->load->library('form_validation');
$ci->form_validation = $this->form_validation;
}
return $this->form_validation;
}
protected function db_update_validation()
{
$validation_result = (object)array('success'=>false);
$field_types = $this->get_field_types();
$required_fields = $this->required_fields;
$edit_fields = $this->get_edit_fields();
if(!empty($required_fields))
{
foreach($edit_fields as $edit_field)
{
$field_name = $edit_field->field_name;
if(!isset($this->validation_rules[$field_name]) && in_array( $field_name, $required_fields) )
{
$this->set_rules( $field_name, $field_types[$field_name]->display_as, 'required');
}
}
}
if(!empty($this->validation_rules))
{
$form_validation = $this->form_validation();
$edit_fields = $this->get_edit_fields();
foreach($edit_fields as $edit_field)
{
$field_name = $edit_field->field_name;
if(isset($this->validation_rules[$field_name]))
{
$rule = $this->validation_rules[$field_name];
$form_validation->set_rules($rule['field'],$rule['label'],$rule['rules']);
}
}
if($form_validation->run())
{
$validation_result->success = true;
}
else
{
$validation_result->error_message = $form_validation->error_string();
$validation_result->error_fields = $form_validation->_error_array;
}
}
else
{
$validation_result->success = true;
}
return $validation_result;
}
protected function db_insert($state_info)
{
$validation_result = $this->db_insert_validation();
if($validation_result->success)
{
$post_data = $state_info->unwrapped_data;
$add_fields = $this->get_add_fields();
if($this->callback_insert == null)
{
if($this->callback_before_insert != null)
{
$callback_return = call_user_func($this->callback_before_insert, $post_data);
if(!empty($callback_return) && is_array($callback_return))
$post_data = $callback_return;
elseif($callback_return === false)
return false;
}
$insert_data = array();
$types = $this->get_field_types();
foreach($add_fields as $num_row => $field)
{
if(isset($post_data[$field->field_name]) && !isset($this->relation_n_n[$field->field_name]))
{
if(isset($types[$field->field_name]->db_null) && $types[$field->field_name]->db_null && $post_data[$field->field_name] === '')
{
$insert_data[$field->field_name] = null;
}
elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'date')
{
$insert_data[$field->field_name] = $this->_convert_date_to_sql_date($post_data[$field->field_name]);
}
elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'datetime'){
$insert_data[$field->field_name] = $this->_convert_date_to_sql_date(substr($post_data[$field->field_name],0,10)).
substr($post_data[$field->field_name],10);
}
else
{
$insert_data[$field->field_name] = $post_data[$field->field_name];
}
}
}
$insert_result = $this->basic_model->db_insert($insert_data);
if($insert_result !== false)
{
$insert_primary_key = $insert_result;
}
else
{
return false;
}
if(!empty($this->relation_n_n))
{
foreach($this->relation_n_n as $field_name => $field_info)
{
$relation_data = isset( $post_data[$field_name] ) ? $post_data[$field_name] : array() ;
$this->db_relation_n_n_update($field_info, $relation_data ,$insert_primary_key);
}
}
if($this->callback_after_insert != null)
{
$callback_return = call_user_func($this->callback_after_insert, $post_data, $insert_primary_key);
if($callback_return === false)
{
return false;
}
}
}else
{
$callback_return = call_user_func($this->callback_insert, $post_data);
if($callback_return === false)
{
return false;
}
}
if(isset($insert_primary_key))
return $insert_primary_key;
else
return true;
}
return false;
}
protected function db_update($state_info)
{
$validation_result = $this->db_update_validation();
$edit_fields = $this->get_edit_fields();
if($validation_result->success)
{
$post_data = $state_info->unwrapped_data;
$primary_key = $state_info->primary_key;
if($this->callback_update == null)
{
if($this->callback_before_update != null)
{
$callback_return = call_user_func($this->callback_before_update, $post_data, $primary_key);
if(!empty($callback_return) && is_array($callback_return))
{
$post_data = $callback_return;
}
elseif($callback_return === false)
{
return false;
}
}
$update_data = array();
$types = $this->get_field_types();
foreach($edit_fields as $num_row => $field)
{
if(isset($post_data[$field->field_name]) && !isset($this->relation_n_n[$field->field_name]))
{
if(isset($types[$field->field_name]->db_null) && $types[$field->field_name]->db_null && $post_data[$field->field_name] === '')
{
$update_data[$field->field_name] = null;
}
elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'date')
{
$update_data[$field->field_name] = $this->_convert_date_to_sql_date($post_data[$field->field_name]);
}
elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'datetime'){
$update_data[$field->field_name] = $this->_convert_date_to_sql_date(substr($post_data[$field->field_name],0,10)).
substr($post_data[$field->field_name],10);
}
else
{
$update_data[$field->field_name] = $post_data[$field->field_name];
}
}
}
$this->basic_model->db_update($update_data, $primary_key);
if(!empty($this->relation_n_n))
{
foreach($this->relation_n_n as $field_name => $field_info)
{
$relation_data = isset( $post_data[$field_name] ) ? $post_data[$field_name] : array() ;
$this->db_relation_n_n_update($field_info, $relation_data ,$primary_key);
}
}
if($this->callback_after_update != null)
{
$callback_return = call_user_func($this->callback_after_update, $post_data, $primary_key);
if($callback_return === false)
{
return false;
}
}
}
else
{
$callback_return = call_user_func($this->callback_update, $post_data, $primary_key);
if($callback_return === false)
{
return false;
}
}
return true;
}
else
{
return false;
}
}
protected function _convert_date_to_sql_date($date)
{
$date = substr($date,0,10);
if(preg_match('/\d{4}-\d{2}-\d{2}/',$date))
{
//If it's already a sql-date don't convert it!
return $date;
}elseif(empty($date))
{
return '';
}
$date_array = preg_split( '/[-\.\/ ]/', $date);
if($this->php_date_format == 'd/m/Y')
{
$sql_date = date('Y-m-d',mktime(0,0,0,$date_array[1],$date_array[0],$date_array[2]));
}
elseif($this->php_date_format == 'm/d/Y')
{
$sql_date = date('Y-m-d',mktime(0,0,0,$date_array[0],$date_array[1],$date_array[2]));
}
else
{
$sql_date = $date;
}
return $sql_date;
}
protected function _get_field_names_to_search(array $relation_values)
{
if(!strstr($relation_values[2],'{'))
return $this->_unique_join_name($relation_values[0]).'.'.$relation_values[2];
else
{
$relation_values[2] = ' '.$relation_values[2].' ';
$temp1 = explode('{',$relation_values[2]);
unset($temp1[0]);
$field_names_array = array();
foreach($temp1 as $field)
list($field_names_array[]) = explode('}',$field);
return $field_names_array;
}
}
protected function _unique_join_name($field_name)
{
return 'j'.substr(md5($field_name),0,8); //This j is because is better for a string to begin with a letter and not a number
}
protected function _unique_field_name($field_name)
{
return 's'.substr(md5($field_name),0,8); //This s is because is better for a string to begin with a letter and not a number
}
protected function db_delete($state_info)
{
$primary_key = $state_info->primary_key;
if($this->callback_delete == null)
{
if($this->callback_before_delete != null)
{
$callback_return = call_user_func($this->callback_before_delete, $primary_key);
if($callback_return === false)
{
return false;
}
}
if(!empty($this->relation_n_n))
{
foreach($this->relation_n_n as $field_name => $field_info)
{
$this->db_relation_n_n_delete( $field_info, $primary_key );
}
}
$delete_result = $this->basic_model->db_delete($primary_key);
if($delete_result === false)
{
return false;
}
if($this->callback_after_delete != null)
{
$callback_return = call_user_func($this->callback_after_delete, $primary_key);
if($callback_return === false)
{
return false;
}
}
}
else
{
$callback_return = call_user_func($this->callback_delete, $primary_key);
if($callback_return === false)
{
return false;
}
}
return true;
}
protected function db_relation_n_n_update($field_info, $post_data , $primary_key_value)
{
$this->basic_model->db_relation_n_n_update($field_info, $post_data , $primary_key_value);
}
protected function db_relation_n_n_delete($field_info, $primary_key_value)
{
$this->basic_model->db_relation_n_n_delete($field_info, $primary_key_value);
}
protected function get_list()
{
if(!empty($this->order_by))
$this->basic_model->order_by($this->order_by[0],$this->order_by[1]);
if(!empty($this->where))
foreach($this->where as $where)
$this->basic_model->where($where[0],$where[1],$where[2]);
if(!empty($this->or_where))
foreach($this->or_where as $or_where)
$this->basic_model->or_where($or_where[0],$or_where[1],$or_where[2]);
if(!empty($this->like))
foreach($this->like as $like)
$this->basic_model->like($like[0],$like[1],$like[2]);
if(!empty($this->or_like))
foreach($this->or_like as $or_like)