senses-designertravelv3 /application/datamapper/array.php

Language PHP Lines 291
MD5 Hash 65b0799bafdf7a82473848a82251b10f Estimated Cost $4,100 (why?)
Repository https://bitbucket.org/matyhaty/senses-designertravelv3 View Raw File View Project SPDX
  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
<?php

/**
 * Array Extension for DataMapper classes.
 *
 * Quickly convert DataMapper models to-and-from PHP arrays.
 *
 * @license     MIT License
 * @package             DMZ-Included-Extensions
 * @category    DMZ
 * @author      Phil DeJarnett
 * @link        http://www.overzealous.com/dmz/pages/extensions/array.html
 * @version     1.0
 */

// --------------------------------------------------------------------------

/**
 * DMZ_Array Class
 *
 * @package             DMZ-Included-Extensions
 */
class DMZ_Array {

        /**
         * Convert a DataMapper model into an associative array.
         * If the specified fields includes a related object, the ids from the
         * objects are collected into an array and stored on that key.
         * This method does not recursively add objects.
         *
         * @param       DataMapper $object The DataMapper Object to convert
         * @param       array $fields Array of fields to include.  If empty, includes all database columns.
         * @return      array An associative array of the requested fields and related object ids.
         */
        function to_array($object, $fields = '')
        {
                // assume all database columns if $fields is not provided.
                if(empty($fields))
                {
                        $fields = $object->fields;
                }
                else
                {
                        $fields = (array) $fields;
                }

                $result = array();

                foreach($fields as $f)
                {
                        // handle related fields
                        if(array_key_exists($f, $object->has_one) || array_key_exists($f, $object->has_many))
                        {
                                // each related item is stored as an array of ids
                                // Note: this method will NOT get() the related object.
                                $rels = array();
                                foreach($object->{$f} as $item)
                                {
                                        $rels[] = $item->id;
                                }
                                $result[$f] = $rels;
                        }
                        else
                        {
                                // just the field.
                                $result[$f] = $object->{$f};
                        }
                }

                return $result;
        }

        /**
         * Convert the entire $object->all array result set into an array of
         * associative arrays.
         *
         * @see         to_array
         * @param       DataMapper $object The DataMapper Object to convert
         * @param       array $fields Array of fields to include.  If empty, includes all database columns.
         * @return      array An array of associative arrays.
         */
        function all_to_array($object, $fields = '')
        {
                // loop through each object in the $all array, convert them to
                // an array, and add them to a new array.
                $result = array();
                foreach($object as $o)
                {
                        $result[] = $o->to_array($fields);
                }
                return $result;
        }

        /**
         * Convert a single field from the entire $object->all array result set into an a single array
         * with the objects' id field as key
         *
         * @param       DataMapper $object The DataMapper Object to convert
         * @param       string $field to include
         * @return      array An array of associative arrays.
         */
        function all_to_single_array($object, $field = '')
        {
                // loop through each object in the $all array, convert them to
                // an array, and add them to a new array.
                $result = array();
                if ( ! empty($field) )
                {
                        foreach($object as $o)
                        {
                                isset($o->{$field}) and $result[$o->id] = $o->{$field};
                        }
                }
                return $result;
        }

        /**
         * Convert an associative array back into a DataMapper model.
         *
         * If $fields is provided, missing fields are assumed to be empty checkboxes.
         *
         * @param       DataMapper $object The DataMapper Object to save to.
         * @param       array $data A an associative array of fields to convert.
         * @param       array $fields Array of 'safe' fields.  If empty, only includes the database columns.
         * @param       bool $save If TRUE, then attempt to save the object automatically.
         * @return      array|bool A list of newly related objects, or the result of the save if $save is TRUE
         */
        function from_array($object, $data, $fields = '', $save = FALSE)
        {
                // clear the current object
                //$object->clear();

                // keep track of newly related objects
                $new_related_objects = array();

                // Assume all database columns.
                // In this case, simply store $fields that are in the $data array.
                if(empty($fields))
                {
                        $fields = $object->fields;
                        foreach($data as $k => $v) {
                                if(in_array($k, $fields))
                                {
                                        $object->{$k} = $v;
                                }
                        }
                }
                else
                {
                        // If $fields is provided, assume all $fields should exist.
                        foreach($fields as $f)
                        {
                                if(array_key_exists($f, $object->has_one))
                                {
                                        // Store $has_one relationships
                                        $c = get_class($object->{$f});
                                        $rel = new $c();
                                        $id = isset($data[$f]) ? $data[$f] : 0;
                                        $rel->get_by_id($id);
                                        if($rel->exists())
                                        {
                                                // The new relationship exists, save it.
                                                $new_related_objects[$f] = $rel;
                                        }
                                        else
                                        {
                                                // The new relationship does not exist, delete the old one.
                                                 $object->delete($object->{$f}->get());
                                        }
                                }
                                else if(array_key_exists($f, $object->has_many))
                                {
                                        // Store $has_many relationships
                                        $c = get_class($object->{$f});
                                        $rels = new $c();
                                        $ids = isset($data[$f]) ? $data[$f] : FALSE;
                                        if(empty($ids))
                                        {
                                                // if no IDs were provided, delete all old relationships.
                                                $object->delete($object->{$f}->select('id')->get()->all);
                                        }
                                        else
                                        {
                                                // Otherwise, get the new ones...
                                                $rels->where_in('id', $ids)->select('id')->get();
                                                // Store them...
                                                $new_related_objects[$f] = $rels->all;
                                                // And delete any old ones that do not exist.
                                                $old_rels = $object->{$f}->where_not_in('id', $ids)->select('id')->get();
                                                $object->delete($old_rels->all);
                                        }
                                }
                                else
                                {
                                        // Otherwise, if the $data was set, store it...
                                        if(isset($data[$f]))
                                        {
                                                $v = $data[$f];
                                        }
                                        else
                                        {
                                                // Or assume it was an unchecked checkbox, and clear it.
                                                $v = FALSE;
                                        }
                                        $object->{$f} = $v;
                                }
                        }
                }
                if($save)
                {
                        // Auto save
                        return $object->save($new_related_objects);
                }
                else
                {
                        // return new objects
                        return $new_related_objects;
                }
        }

        /**
		 * Convert an associative multi_level array back into a DataMapper model.
		 *
		 * If $fields is provided, missing fields are assumed to be empty checkboxes.
		 *
		 * @param	DataMapper $object The DataMapper Object to save to.
		 * @param	array $data A an associative array of fields to convert.
		 * @param	array $fields Array of 'safe' fields.  If empty, only includes the database columns.
		 * @return	array|bool A list of newly related objects, or the result of the save if $save is TRUE
		 */
		function all_from_array($object, $data, $fields = '')
		{
			//echo 'heelo';
			//echo 'All_from_array<hr><pre>'.print_r($data, true).'</pre><hr>';
			
			// clear the current object
			$object->clear();
	
			// get the objects class name, we need it to construct copies
			$class = get_class($object);
	
			$first = true;
	
			// loop over the data array
			foreach($data as $row)
			{
				// create an object for this row
				if ($first)
				{
					$object->from_array($row, $fields);
				}
				else
				{
					$new = new $class;
					$new->from_array($row, $fields);
				}
	
				// and store it in the object
				if ($object->all_array_uses_ids && isset($row['id']))
				{
					if ($first)
					{
						$object->all[$row['id']] =& $object;
					}
					else
					{
						$object->all[$row['id']] = $new;
					}
				}
				else
				{
					if ($first)
					{
						$object->all[] =& $object;
					}
					else
					{
						$object->all[] = $new;
					}
				}
	
				$first = FALSE;
			}
	
			return $object;
		}
}

/* End of file array.php */
/* Location: ./application/datamapper/array.php */
Back to Top