-
Notifications
You must be signed in to change notification settings - Fork 280
/
Facade.php
executable file
·1166 lines (1067 loc) · 34.8 KB
/
Facade.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
/**
* RedBean Facade
* @file RedBean/Facade.php
* @description Convenience class for RedBeanPHP.
* This class hides the object landscape of
* RedBeanPHP behind a single letter class providing
* almost all functionality with simple static calls.
* @author Gabor de Mooij and the RedBeanPHP Community
* @license BSD/GPLv2
*
* copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*
*/
class RedBean_Facade {
/**
* Collection of toolboxes
* @var array
*/
public static $toolboxes = array();
/**
*
* Constains an instance of the RedBean Toolbox
* @var RedBean_ToolBox
*
*/
public static $toolbox;
/**
* Constains an instance of RedBean OODB
* @var RedBean_OODB
*/
public static $redbean;
/**
* Contains an instance of the Query Writer
* @var RedBean_QueryWriter
*/
public static $writer;
/**
* Contains an instance of the Database
* Adapter.
* @var RedBean_DBAdapter
*/
public static $adapter;
/**
* Contains an instance of the Association Manager
* @var RedBean_AssociationManager
*/
public static $associationManager;
/**
* Contains an instance of the Extended Association Manager
* @var RedBean_ExtAssociationManager
*/
public static $extAssocManager;
/**
* Holds an instance of Bean Exporter
* @var RedBean_Plugin_BeanExport
*/
public static $exporter;
/**
* Holds the Key of the current database.
* @var string
*/
public static $currentDB = '';
/**
* Holds reference to SQL Helper
*/
public static $f;
/**
* Get version
* @return string
*/
public static function getVersion() {
return '3.2';
}
/**
* Kickstarts redbean for you. This method should be called before you start using
* RedBean. The Setup() method can be called without any arguments, in this case it will
* try to create a SQLite database in /tmp called red.db (this only works on UNIX-like systems).
*
* @param string $dsn Database connection string
* @param string $username Username for database
* @param string $password Password for database
*
* @return void
*/
public static function setup( $dsn=NULL, $username=NULL, $password=NULL ) {
if (function_exists('sys_get_temp_dir')) $tmp = sys_get_temp_dir(); else $tmp = 'tmp';
if (is_null($dsn)) $dsn = 'sqlite:/'.$tmp.'/red.db';
self::addDatabase('default',$dsn,$username,$password);
self::selectDatabase('default');
return self::$toolbox;
}
/**
* Adds a database to the facade, afterwards you can select the database using
* selectDatabase($key).
*
* @param string $key ID for the database
* @param string $dsn DSN for the database
* @param string $user User for connection
* @param null|string $pass Password for connection
* @param bool $frozen Whether this database is frozen or not
*
* @return void
*/
public static function addDatabase( $key, $dsn, $user=null, $pass=null, $frozen=false ) {
self::$toolboxes[$key] = RedBean_Setup::kickstart($dsn,$user,$pass,$frozen);
}
/**
* Selects a different database for the Facade to work with.
*
* @param string $key Key of the database to select
* @return int 1
*/
public static function selectDatabase($key) {
if (self::$currentDB===$key) return false;
self::configureFacadeWithToolbox(self::$toolboxes[$key]);
self::$currentDB = $key;
return true;
}
/**
* Toggles DEBUG mode.
* In Debug mode all SQL that happens under the hood will
* be printed to the screen or logged by provided logger.
*
* @param boolean $tf
* @param RedBean_ILogger $logger
*/
public static function debug( $tf = true, $logger = NULL ) {
if (!$logger) $logger = new RedBean_Logger;
self::$adapter->getDatabase()->setDebugMode( $tf, $logger );
}
/**
* Stores a RedBean OODB Bean and returns the ID.
*
* @param RedBean_OODBBean $bean bean
*
* @return integer $id id
*/
public static function store( RedBean_OODBBean $bean ) {
return self::$redbean->store( $bean );
}
/**
* Toggles fluid or frozen mode. In fluid mode the database
* structure is adjusted to accomodate your objects. In frozen mode
* this is not the case.
*
* You can also pass an array containing a selection of frozen types.
* Let's call this chilly mode, it's just like fluid mode except that
* certain types (i.e. tables) aren't touched.
*
* @param boolean|array $trueFalse
*/
public static function freeze( $tf = true ) {
self::$redbean->freeze( $tf );
}
/**
* Loads the bean with the given type and id and returns it.
*
* @param string $type type
* @param integer $id id of the bean you want to load
*
* @return RedBean_OODBBean $bean
*/
public static function load( $type, $id ) {
return self::$redbean->load( $type, $id );
}
/**
* Deletes the specified bean.
*
* @param RedBean_OODBBean $bean bean to be deleted
*
* @return mixed
*/
public static function trash( RedBean_OODBBean $bean ) {
return self::$redbean->trash( $bean );
}
/**
* Dispenses a new RedBean OODB Bean for use with
* the rest of the methods.
*
* @param string $type type
*
*
*/
public static function dispense( $type, $num = 1 ) {
if ($num==1) {
return self::$redbean->dispense( $type );
}
else {
$beans = array();
for($v=0; $v<$num; $v++) $beans[] = self::$redbean->dispense( $type );
return $beans;
}
}
/**
* Convience method. Tries to find beans of a certain type,
* if no beans are found, it dispenses a bean of that type.
*
* @param string $type type of bean you are looking for
* @param string $sql SQL code for finding the bean
* @param array $values parameters to bind to SQL
*
* @return array $beans Contains RedBean_OODBBean instances
*/
public static function findOrDispense( $type, $sql, $values ) {
$foundBeans = self::find($type,$sql,$values);
if (count($foundBeans)==0) return array(self::dispense($type)); else return $foundBeans;
}
/**
* Associates two Beans. This method will associate two beans with eachother.
* You can then get one of the beans by using the related() function and
* providing the other bean. You can also provide a base bean in the extra
* parameter. This base bean allows you to add extra information to the association
* record. Note that this is for advanced use only and the information will not
* be added to one of the beans, just to the association record.
* It's also possible to provide an array or JSON string as base bean. If you
* pass a scalar this function will interpret the base bean as having one
* property called 'extra' with the value of the scalar.
*
* @param RedBean_OODBBean $bean1 bean that will be part of the association
* @param RedBean_OODBBean $bean2 bean that will be part of the association
* @param mixed $extra bean, scalar, array or JSON providing extra data.
*
* @return mixed
*/
public static function associate( RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $extra = null ) {
//No extra? Just associate like always (default)
if (!$extra) {
return self::$associationManager->associate( $bean1, $bean2 );
}
else{
if (!is_array($extra)) {
$info = json_decode($extra,true);
if (!$info) $info = array('extra'=>$extra);
}
else {
$info = $extra;
}
$bean = RedBean_Facade::dispense('typeLess');
$bean->import($info);
return self::$extAssocManager->extAssociate($bean1, $bean2, $bean);
}
}
/**
* Breaks the association between two beans.
* This functions breaks the association between a pair of beans. After
* calling this functions the beans will no longer be associated with
* eachother. Calling related() with either one of the beans will no longer
* return the other bean.
*
* @param RedBean_OODBBean $bean1 bean
* @param RedBean_OODBBean $bean2 bean
*
* @return mixed
*/
public static function unassociate( RedBean_OODBBean $bean1, RedBean_OODBBean $bean2 , $fast=false) {
return self::$associationManager->unassociate( $bean1, $bean2, $fast );
}
/**
* Returns all the beans associated with $bean.
* This method will return an array containing all the beans that have
* been associated once with the associate() function and are still
* associated with the bean specified. The type parameter indicates the
* type of beans you are looking for. You can also pass some extra SQL and
* values for that SQL to filter your results after fetching the
* related beans.
*
* Dont try to make use of subqueries, a subquery using IN() seems to
* be slower than two queries!
*
* Since 3.2, you can now also pass an array of beans instead just one
* bean as the first parameter.
*
* @param RedBean_OODBBean|array $bean the bean you have
* @param string $type the type of beans you want
* @param string $sql SQL snippet for extra filtering
* @param array $val values to be inserted in SQL slots
*
* @return array $beans beans yielded by your query.
*/
public static function related( $bean, $type, $sql=null, $values=array()) {
$keys = self::$associationManager->related( $bean, $type );
if (count($keys)==0) return array();
if (!$sql) return self::batch($type, $keys);
$rows = self::$writer->selectRecord( $type, array('id'=>$keys),array($sql,$values),false );
return self::$redbean->convertToBeans($type,$rows);
}
/**
* Returns only single associated bean.
*
* @param RedBean_OODBBean $bean bean provided
* @param string $type type of bean you are searching for
* @param string $sql SQL for extra filtering
* @param array $values values to be inserted in SQL slots
*
*
* @return RedBean_OODBBean $bean
*/
public static function relatedOne( RedBean_OODBBean $bean, $type, $sql=null, $values=array() ) {
$beans = self::related($bean, $type, $sql, $values);
if (count($beans)==0) return null;
return reset( $beans );
}
/**
* Checks whether a pair of beans is related N-M. This function does not
* check whether the beans are related in N:1 way.
*
* @param RedBean_OODBBean $bean1 first bean
* @param RedBean_OODBBean $bean2 second bean
*
* @return bool $yesNo whether they are related
*/
public static function areRelated( RedBean_OODBBean $bean1, RedBean_OODBBean $bean2) {
return self::$associationManager->areRelated($bean1,$bean2);
}
/**
* The opposite of related(). Returns all the beans that are not
* associated with the bean provided.
*
* @param RedBean_OODBBean $bean bean provided
* @param string $type type of bean you are searching for
* @param string $sql SQL for extra filtering
* @param array $values values to be inserted in SQL slots
*
* @return array $beans beans
*/
public static function unrelated(RedBean_OODBBean $bean, $type, $sql=null, $values=array()) {
$keys = self::$associationManager->related( $bean, $type );
$rows = self::$writer->selectRecord( $type, array('id'=>$keys), array($sql,$values), false, true );
return self::$redbean->convertToBeans($type,$rows);
}
/**
* Clears all associated beans.
* Breaks all many-to-many associations of a bean and a specified type.
*
* @param RedBean_OODBBean $bean bean you wish to clear many-to-many relations for
* @param string $type type of bean you wish to break associatons with
*
* @return void
*/
public static function clearRelations( RedBean_OODBBean $bean, $type ) {
self::$associationManager->clearRelations( $bean, $type );
}
/**
* Finds a bean using a type and a where clause (SQL).
* As with most Query tools in RedBean you can provide values to
* be inserted in the SQL statement by populating the value
* array parameter; you can either use the question mark notation
* or the slot-notation (:keyname).
*
* @param string $type type the type of bean you are looking for
* @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause
* @param array $values values array of values to be bound to parameters in query
*
* @return array $beans beans
*/
public static function find( $type, $sql=null, $values=array() ) {
if ($sql instanceof RedBean_SQLHelper) list($sql,$values) = $sql->getQuery();
if (!is_array($values)) throw new InvalidArgumentException('Expected array, ' . gettype($values) . ' given.');
return self::$redbean->find($type,array(),array($sql,$values));
}
/**
* Finds a bean using a type and a where clause (SQL).
* As with most Query tools in RedBean you can provide values to
* be inserted in the SQL statement by populating the value
* array parameter; you can either use the question mark notation
* or the slot-notation (:keyname).
* The findAll() method differs from the find() method in that it does
* not assume a WHERE-clause, so this is valid:
*
* R::findAll('person',' ORDER BY name DESC ');
*
* Your SQL does not have to start with a valid WHERE-clause condition.
*
* @param string $type type the type of bean you are looking for
* @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause
* @param array $values values array of values to be bound to parameters in query
*
* @return array $beans beans
*/
public static function findAll( $type, $sql=null, $values=array() ) {
if (!is_array($values)) throw new InvalidArgumentException('Expected array, ' . gettype($values) . ' given.');
return self::$redbean->find($type,array(),array($sql,$values),true);
}
/**
* Finds a bean using a type and a where clause (SQL).
* As with most Query tools in RedBean you can provide values to
* be inserted in the SQL statement by populating the value
* array parameter; you can either use the question mark notation
* or the slot-notation (:keyname).
* The variation also exports the beans (i.e. it returns arrays).
*
* @param string $type type the type of bean you are looking for
* @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause
* @param array $values values array of values to be bound to parameters in query
*
* @return array $arrays arrays
*/
public static function findAndExport($type, $sql=null, $values=array()) {
$items = self::find( $type, $sql, $values );
$arr = array();
foreach($items as $key=>$item) {
$arr[$key]=$item->export();
}
return $arr;
}
/**
* Finds a bean using a type and a where clause (SQL).
* As with most Query tools in RedBean you can provide values to
* be inserted in the SQL statement by populating the value
* array parameter; you can either use the question mark notation
* or the slot-notation (:keyname).
* This variation returns the first bean only.
*
* @param string $type type the type of bean you are looking for
* @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause
* @param array $values values array of values to be bound to parameters in query
*
* @return RedBean_OODBBean $bean
*/
public static function findOne( $type, $sql=null, $values=array()) {
$items = self::find($type,$sql,$values);
$found = reset($items);
if (!$found) return null;
return $found;
}
/**
* Finds a bean using a type and a where clause (SQL).
* As with most Query tools in RedBean you can provide values to
* be inserted in the SQL statement by populating the value
* array parameter; you can either use the question mark notation
* or the slot-notation (:keyname).
* This variation returns the last bean only.
*
* @param string $type type the type of bean you are looking for
* @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause
* @param array $values values array of values to be bound to parameters in query
*
* @return RedBean_OODBBean $bean
*/
public static function findLast( $type, $sql=null, $values=array() ) {
$items = self::find( $type, $sql, $values );
$found = end( $items );
if (!$found) return null;
return $found;
}
/**
* Returns an array of beans. Pass a type and a series of ids and
* this method will bring you the correspondig beans.
*
* important note: Because this method loads beans using the load()
* function (but faster) it will return empty beans with ID 0 for
* every bean that could not be located. The resulting beans will have the
* passed IDs as their keys.
*
* @param string $type type of beans
* @param array $ids ids to load
*
* @return array $beans resulting beans (may include empty ones)
*/
public static function batch( $type, $ids ) {
return self::$redbean->batch($type, $ids);
}
/**
* Convenience function to execute Queries directly.
* Executes SQL.
*
* @param string $sql sql SQL query to execute
* @param array $values values a list of values to be bound to query parameters
*
* @return integer $affected number of affected rows
*/
public static function exec( $sql, $values=array() ) {
return self::query('exec',$sql,$values);
}
/**
* Convenience function to execute Queries directly.
* Executes SQL.
*
* @param string $sql sql SQL query to execute
* @param array $values values a list of values to be bound to query parameters
*
* @return array $results
*/
public static function getAll( $sql, $values=array() ) {
return self::query('get',$sql,$values);
}
/**
* Convenience function to execute Queries directly.
* Executes SQL.
*
* @param string $sql sql SQL query to execute
* @param array $values values a list of values to be bound to query parameters
*
* @return string $result scalar
*/
public static function getCell( $sql, $values=array() ) {
return self::query('getCell',$sql,$values);
}
/**
* Convenience function to execute Queries directly.
* Executes SQL.
*
* @param string $sql sql SQL query to execute
* @param array $values values a list of values to be bound to query parameters
*
* @return array $results
*/
public static function getRow( $sql, $values=array() ) {
return self::query('getRow',$sql,$values);
}
/**
* Convenience function to execute Queries directly.
* Executes SQL.
*
* @param string $sql sql SQL query to execute
* @param array $values values a list of values to be bound to query parameters
*
* @return array $results
*/
public static function getCol( $sql, $values=array() ) {
return self::query('getCol',$sql,$values);
}
/**
* Internal Query function, executes the desired query. Used by
* all facade query functions. This keeps things DRY.
*
* @throws RedBean_Exception_SQL
*
* @param string $method desired query method (i.e. 'cell','col','exec' etc..)
* @param string $sql the sql you want to execute
* @param array $values array of values to be bound to query statement
*
* @return array $results results of query
*/
private static function query($method,$sql,$values) {
if (!self::$redbean->isFrozen()) {
try {
$rs = RedBean_Facade::$adapter->$method( $sql, $values );
}catch(RedBean_Exception_SQL $e) {
if(self::$writer->sqlStateIn($e->getSQLState(),
array(
RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN,
RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE)
)) {
return array();
}
else {
throw $e;
}
}
return $rs;
}
else {
return RedBean_Facade::$adapter->$method( $sql, $values );
}
}
/**
* Convenience function to execute Queries directly.
* Executes SQL.
* Results will be returned as an associative array. The first
* column in the select clause will be used for the keys in this array and
* the second column will be used for the values. If only one column is
* selected in the query, both key and value of the array will have the
* value of this field for each row.
*
* @param string $sql sql SQL query to execute
* @param array $values values a list of values to be bound to query parameters
*
* @return array $results
*/
public static function getAssoc($sql,$values=array()) {
return self::query('getAssoc',$sql,$values);
}
/**
* Makes a copy of a bean. This method makes a deep copy
* of the bean.The copy will have the following features.
* - All beans in own-lists will be duplicated as well
* - All references to shared beans will be copied but not the shared beans themselves
* - All references to parent objects (_id fields) will be copied but not the parents themselves
* In most cases this is the desired scenario for copying beans.
* This function uses a trail-array to prevent infinite recursion, if a recursive bean is found
* (i.e. one that already has been processed) the ID of the bean will be returned.
* This should not happen though.
*
* Note:
* This function does a reflectional database query so it may be slow.
*
* @param RedBean_OODBBean $bean bean to be copied
* @param array $trail for internal usage, pass array()
* @param boolean $pid for internal usage
*
* @return array $copiedBean the duplicated bean
*/
public static function dup($bean,$trail=array(),$pid=false) {
$type = $bean->getMeta('type');
$key = $type.$bean->getID();
if (isset($trail[$key])) return $bean;
$trail[$key]=$bean;
$copy = RedBean_Facade::dispense($type);
$copy->import( $bean->getProperties() );
$copy->id = 0;
$tables = self::$writer->getTables();
foreach($tables as $table) {
if (strpos($table,'_')!==false || $table==$type) continue;
$owned = 'own'.ucfirst($table);
$shared = 'shared'.ucfirst($table);
if ($beans = $bean->$owned) {
$copy->$owned = array();
foreach($beans as $subBean) {
array_push($copy->$owned,self::dup($subBean,$trail,$pid));
}
}
$copy->setMeta('sys.shadow.'.$owned,null);
if ($beans = $bean->$shared) {
$copy->$shared = array();
foreach($beans as $subBean) {
array_push($copy->$shared,$subBean);
}
}
$copy->setMeta('sys.shadow.'.$shared,null);
}
if ($pid) $copy->id = $bean->id;
return $copy;
}
/**
* Exports a collection of beans. Handy for XML/JSON exports with a
* Javascript framework like Dojo or ExtJS.
* What will be exported:
* - contents of the bean
* - all own bean lists (recursively)
* - all shared beans (not THEIR own lists)
*
* @param array|RedBean_OODBBean $beans beans to be exported
*
* @return array $array exported structure
*/
public static function exportAll($beans) {
$array = array();
if (!is_array($beans)) $beans = array($beans);
foreach($beans as $bean) {
$f = self::dup($bean,array(),true);
$array[] = $f->export();
}
return $array;
}
/**
* Given an array of two beans and a property, this method
* swaps the value of the property.
* This is handy if you need to swap the priority or orderNo
* of an item (i.e. bug-tracking, page order).
*
* @param array $beans beans
* @param string $property property
*/
public static function swap( $beans, $property ) {
$bean1 = array_shift($beans);
$bean2 = array_shift($beans);
$tmp = $bean1->$property;
$bean1->$property = $bean2->$property;
$bean2->$property = $tmp;
RedBean_Facade::store($bean1);
RedBean_Facade::store($bean2);
}
/**
* Converts a series of rows to beans.
*
* @param string $type type
* @param array $rows must contain an array of arrays.
*
* @return array $beans
*/
public static function convertToBeans($type,$rows) {
return self::$redbean->convertToBeans($type,$rows);
}
/**
* Part of RedBeanPHP Tagging API.
* Tests whether a bean has been associated with one ore more
* of the listed tags. If the third parameter is TRUE this method
* will return TRUE only if all tags that have been specified are indeed
* associated with the given bean, otherwise FALSE.
* If the third parameter is FALSE this
* method will return TRUE if one of the tags matches, FALSE if none
* match.
*
* @param RedBean_OODBBean $bean bean to check for tags
* @param array $tags list of tags
* @param boolean $all whether they must all match or just some
*
* @return boolean $didMatch whether the bean has been assoc. with the tags
*/
public static function hasTag($bean, $tags, $all=false) {
$foundtags = RedBean_Facade::tag($bean);
if (is_string($foundtags)) $foundtags = explode(",",$tags);
$same = array_intersect($tags,$foundtags);
if ($all) {
return (implode(",",$same)===implode(",",$tags));
}
return (bool) (count($same)>0);
}
/**
* Part of RedBeanPHP Tagging API.
* Removes all sepcified tags from the bean. The tags specified in
* the second parameter will no longer be associated with the bean.
*
* @param RedBean_OODBBean $bean tagged bean
* @param array $tagList list of tags (names)
*
* @return void
*/
public static function untag($bean,$tagList) {
if ($tagList!==false && !is_array($tagList)) $tags = explode( ",", (string)$tagList); else $tags=$tagList;
foreach($tags as $tag) {
$t = RedBean_Facade::findOne('tag'," title = ? ",array($tag));
if ($t) {
RedBean_Facade::unassociate( $bean, $t );
}
}
}
/**
* Part of RedBeanPHP Tagging API.
* Tags a bean or returns tags associated with a bean.
* If $tagList is null or omitted this method will return a
* comma separated list of tags associated with the bean provided.
* If $tagList is a comma separated list (string) of tags all tags will
* be associated with the bean.
* You may also pass an array instead of a string.
*
* @param RedBean_OODBBean $bean bean
* @param mixed $tagList tags
*
* @return string $commaSepListTags
*/
public static function tag( RedBean_OODBBean $bean, $tagList = null ) {
if (is_null($tagList)) {
$tags = RedBean_Facade::related( $bean, 'tag');
$foundTags = array();
foreach($tags as $tag) {
$foundTags[] = $tag->title;
}
return $foundTags;
}
RedBean_Facade::clearRelations( $bean, 'tag' );
RedBean_Facade::addTags( $bean, $tagList );
}
/**
* Part of RedBeanPHP Tagging API.
* Adds tags to a bean.
* If $tagList is a comma separated list of tags all tags will
* be associated with the bean.
* You may also pass an array instead of a string.
*
* @param RedBean_OODBBean $bean bean
* @param array $tagList list of tags to add to bean
*
* @return void
*/
public static function addTags( RedBean_OODBBean $bean, $tagList ) {
if ($tagList!==false && !is_array($tagList)) $tags = explode( ",", (string)$tagList); else $tags=$tagList;
if ($tagList===false) return;
foreach($tags as $tag) {
$t = RedBean_Facade::findOne('tag',' title = ? ',array($tag));
if (!$t) {
$t = RedBean_Facade::dispense('tag');
$t->title = $tag;
RedBean_Facade::store($t);
}
RedBean_Facade::associate( $bean, $t );
}
}
/**
* Part of RedBeanPHP Tagging API.
* Returns all beans that have been tagged with one of the tags given.
*
* @param $beanType type of bean you are looking for
* @param $tagList list of tags to match
*
* @return array
*/
public static function tagged( $beanType, $tagList ) {
if ($tagList!==false && !is_array($tagList)) $tags = explode( ",", (string)$tagList); else $tags=$tagList;
$collection = array();
$tags = self::$redbean->find('tag',array('title'=>$tags));
if (count($tags)>0) $collection = self::related($tags,$beanType);
return $collection;
}
/**
* Part of RedBeanPHP Tagging API.
* Returns all beans that have been tagged with ALL of the tags given.
*
* @param $beanType type of bean you are looking for
* @param $tagList list of tags to match
*
* @return array
*/
public static function taggedAll( $beanType, $tagList ) {
if ($tagList!==false && !is_array($tagList)) $tags = explode( ",", (string)$tagList); else $tags=$tagList;
$beans = array();
foreach($tags as $tag) {
$beans = self::tagged($beanType,$tag);
if (isset($oldBeans)) $beans = array_intersect_assoc($beans,$oldBeans);
$oldBeans = $beans;
}
return $beans;
}
/**
* Wipes all beans of type $beanType.
*
* @param string $beanType type of bean you want to destroy entirely.
*/
public static function wipe( $beanType ) {
return RedBean_Facade::$redbean->wipe($beanType);
}
/**
* Counts beans
*
* @param string $beanType type of bean
*
* @return integer $numOfBeans
*/
public static function count( $beanType ) {
return RedBean_Facade::$redbean->count($beanType);
}
/**
* Configures the facade, want to have a new Writer? A new Object Database or a new
* Adapter and you want it on-the-fly? Use this method to hot-swap your facade with a new
* toolbox.
*
* @param RedBean_ToolBox $tb toolbox
*
* @return RedBean_ToolBox $tb old, rusty, previously used toolbox
*/
public static function configureFacadeWithToolbox( RedBean_ToolBox $tb ) {
$oldTools = self::$toolbox;
self::$toolbox = $tb;
self::$writer = self::$toolbox->getWriter();
self::$adapter = self::$toolbox->getDatabaseAdapter();
self::$redbean = self::$toolbox->getRedBean();
self::$associationManager = new RedBean_AssociationManager( self::$toolbox );
self::$redbean->setAssociationManager(self::$associationManager);
self::$extAssocManager = new RedBean_ExtAssociationManager( self::$toolbox );
$helper = new RedBean_ModelHelper();
self::$redbean->addEventListener('update', $helper );
self::$redbean->addEventListener('open', $helper );
self::$redbean->addEventListener('delete', $helper );
self::$associationManager->addEventListener('delete', $helper );
self::$redbean->addEventListener('after_delete', $helper );
self::$redbean->addEventListener('after_update', $helper );
self::$redbean->addEventListener('dispense', $helper );
self::$f = new RedBean_SQLHelper(self::$adapter);
return $oldTools;
}
/**
* facade method for Cooker Graph.
*
* @param array $array array containing POST/GET fields or other data
* @param boolean $filterEmptyBeans whether you want to exclude empty beans
*
* @return array $arrayOfBeans Beans
*/
public static function graph($array,$filterEmpty=false) {
$cooker = new RedBean_Cooker();
$cooker->setToolbox(self::$toolbox);
return $cooker->graph($array,$filterEmpty);
}
/**
* Facade Convience method for adapter transaction system.
* Begins a transaction.
*
* @return void
*/
public static function begin() {
self::$adapter->startTransaction();
}
/**
* Facade Convience method for adapter transaction system.
* Commits a transaction.
*
* @return void
*/
public static function commit() {
self::$adapter->commit();
}
/**
* Facade Convience method for adapter transaction system.
* Rolls back a transaction.
*
* @return void
*/
public static function rollback() {
self::$adapter->rollback();
}
/**
* Returns a list of columns. Format of this array:
* array( fieldname => type )
* Note that this method only works in fluid mode because it might be
* quite heavy on production servers!
*
* @param string $table name of the table (not type) you want to get columns of
*
* @return array $columns list of columns and their types
*/
public static function getColumns($table) {
return self::$writer->getColumns($table);
}
/**
* Generates question mark slots for an array of values.
*
* @param array $array
* @return string $slots
*/
public static function genSlots($array) {
if (count($array)>0) {