PluginProbe
ShiftController Employee Shift Scheduling / 2.1.0
ShiftController Employee Shift Scheduling v2.1.0
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / happ / application / datamapper / array.php

array.php in ShiftController Employee Shift Scheduling 2.1.0, at happ/application/datamapper/array.php

222 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Array Extension for DataMapper classes.
5 *
6 * Quickly convert DataMapper models to-and-from PHP arrays.
7 *
8 * @license MIT License
9 * @package DMZ-Included-Extensions
10 * @category DMZ
11 * @author Phil DeJarnett
12 * @link http://www.overzealous.com/dmz/pages/extensions/array.html
13 * @version 1.0
14 */
15
16 // --------------------------------------------------------------------------
17
18 /**
19 * DMZ_Array Class
20 *
21 * @package DMZ-Included-Extensions
22 */
23 class DMZ_Array {
24
25 /**
26 * Convert a DataMapper model into an associative array.
27 * If the specified fields includes a related object, the ids from the
28 * objects are collected into an array and stored on that key.
29 * This method does not recursively add objects.
30 *
31 * @param DataMapper $object The DataMapper Object to convert
32 * @param array $fields Array of fields to include. If empty, includes all database columns.
33 * @return array An associative array of the requested fields and related object ids.
34 */
35 function to_array($object, $fields = '')
36 {
37 // assume all database columns if $fields is not provided.
38 if(empty($fields))
39 {
40 $fields = $object->fields;
41 }
42 else
43 {
44 $fields = (array) $fields;
45 }
46
47 $result = array();
48
49 foreach($fields as $f)
50 {
51 // handle related fields
52 if(array_key_exists($f, $object->has_one) || array_key_exists($f, $object->has_many))
53 {
54 // each related item is stored as an array of ids
55 // Note: this method will NOT get() the related object.
56 $rels = array();
57 foreach($object->{$f} as $item)
58 {
59 $rels[] = $item->id;
60 }
61 $result[$f] = $rels;
62 }
63 else
64 {
65 // just the field.
66 $result[$f] = $object->{$f};
67 }
68 }
69
70 return $result;
71 }
72
73 /**
74 * Convert the entire $object->all array result set into an array of
75 * associative arrays.
76 *
77 * @see to_array
78 * @param DataMapper $object The DataMapper Object to convert
79 * @param array $fields Array of fields to include. If empty, includes all database columns.
80 * @return array An array of associative arrays.
81 */
82 function all_to_array($object, $fields = '')
83 {
84 // loop through each object in the $all array, convert them to
85 // an array, and add them to a new array.
86 $result = array();
87 foreach($object as $o)
88 {
89 $result[] = $o->to_array($fields);
90 }
91 return $result;
92 }
93
94 /**
95 * Convert a single field from the entire $object->all array result set into an a single array
96 * with the objects' id field as key
97 *
98 * @param DataMapper $object The DataMapper Object to convert
99 * @param string $field to include
100 * @return array An array of associative arrays.
101 */
102 function all_to_single_array($object, $field = '')
103 {
104 // loop through each object in the $all array, convert them to
105 // an array, and add them to a new array.
106 $result = array();
107 if ( ! empty($field) )
108 {
109 foreach($object as $o)
110 {
111 isset($o->{$field}) and $result[$o->id] = $o->{$field};
112 }
113 }
114 return $result;
115 }
116
117 /**
118 * Convert an associative array back into a DataMapper model.
119 *
120 * If $fields is provided, missing fields are assumed to be empty checkboxes.
121 *
122 * @param DataMapper $object The DataMapper Object to save to.
123 * @param array $data A an associative array of fields to convert.
124 * @param array $fields Array of 'safe' fields. If empty, only includes the database columns.
125 * @param bool $save If TRUE, then attempt to save the object automatically.
126 * @return array|bool A list of newly related objects, or the result of the save if $save is TRUE
127 */
128 function from_array($object, $data, $fields = '', $save = FALSE)
129 {
130 // keep track of newly related objects
131 $new_related_objects = array();
132
133 // Assume all database columns.
134 // In this case, simply store $fields that are in the $data array.
135 if(empty($fields))
136 {
137 $fields = $object->fields;
138 foreach($data as $k => $v) {
139 if(in_array($k, $fields))
140 {
141 $object->{$k} = $v;
142 }
143 }
144 }
145 else
146 {
147 // If $fields is provided, assume all $fields should exist.
148 foreach($fields as $f)
149 {
150 if(array_key_exists($f, $object->has_one))
151 {
152 // Store $has_one relationships
153 $c = get_class($object->{$f});
154 $rel = new $c();
155 $id = isset($data[$f]) ? $data[$f] : 0;
156 $rel->get_by_id($id);
157 if($rel->exists())
158 {
159 // The new relationship exists, save it.
160 $new_related_objects[$f] = $rel;
161 }
162 else
163 {
164 // The new relationship does not exist, delete the old one.
165 $object->delete($object->{$f}->get());
166 }
167 }
168 else if(array_key_exists($f, $object->has_many))
169 {
170 // Store $has_many relationships
171 $c = get_class($object->{$f});
172 $rels = new $c();
173 $ids = isset($data[$f]) ? $data[$f] : FALSE;
174 if(empty($ids))
175 {
176 // if no IDs were provided, delete all old relationships.
177 $object->delete($object->{$f}->select('id')->get()->all);
178 }
179 else
180 {
181 // Otherwise, get the new ones...
182 $rels->where_in('id', $ids)->select('id')->get();
183 // Store them...
184 $new_related_objects[$f] = $rels->all;
185 // And delete any old ones that do not exist.
186 $old_rels = $object->{$f}->where_not_in('id', $ids)->select('id')->get();
187 $object->delete($old_rels->all);
188 }
189 }
190 else
191 {
192 // Otherwise, if the $data was set, store it...
193 if(isset($data[$f]))
194 {
195 $v = $data[$f];
196 }
197 else
198 {
199 // Or assume it was an unchecked checkbox, and clear it.
200 $v = FALSE;
201 }
202 $object->{$f} = $v;
203 }
204 }
205 }
206 if($save)
207 {
208 // Auto save
209 return $object->save($new_related_objects);
210 }
211 else
212 {
213 // return new objects
214 return $new_related_objects;
215 }
216 }
217
218 }
219
220 /* End of file array.php */
221 /* Location: ./application/datamapper/array.php */
222