datamapper /application/datamapper/csv.php

Language PHP Lines 226
MD5 Hash 291f4cdd6a72a0c249f59190fd8d9160 Estimated Cost $3,377 (why?)
Repository https://bitbucket.org/ramo_carlo/datamapper 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
<?php

/**
 * CSV Extension for DataMapper classes.
 *
 * Quickly import and export a set of DataMapper models to-and-from CSV files.
 *
 * @license 	MIT License
 * @package		DMZ-Included-Extensions
 * @category	DMZ
 * @author  	Phil DeJarnett
 * @link    	http://www.overzealous.com/dmz/pages/extensions/csv.html
 * @version 	1.0
 */

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

/**
 * DMZ_CSV Class
 *
 * @package		DMZ-Included-Extensions
 */
class DMZ_CSV {
	
	/**
	 * Convert a DataMapper model into an associative array.
	 * 
	 * @param	DataMapper $object The DataMapper Object to export
	 * @param	mixed filename The filename to export to, or a file pointer. If this is a file pointer, it will not be closed.
	 * @param	array $fields Array of fields to include.  If empty, includes all database columns.
	 * @param	bool $include_header If FALSE the header is not exported with the CSV. Not recommended if planning to import this data.
	 * @return	bool TRUE on success, or FALSE on failure.
	 */
	function csv_export($object, $filename, $fields = '', $include_header = TRUE)
	{
		// determine the correct field set.
		if(empty($fields))
		{
			$fields = $object->fields;
		}
		
		$success = TRUE;
		
		// determine if we need to open the file or not.
		if(is_string($filename))
		{
			// open the file, if possible.
			$fp = fopen($filename, 'w');
			if($fp === FALSE)
			{
				log_message('error', 'CSV Extension: Unable to open file ' . $filename);
				return FALSE;
			}
		}
		else
		{
			// assume file pointer.
			$fp = $filename;
		}
		
		if($include_header)
		{
			// Print out header line
			$success = fputcsv($fp, $fields);
		}
		
		if($success)
		{
			foreach($object as $o)
			{
				// convert each object into an array
				$result = array();
				foreach($fields as $f)
				{
					$result[] = $o->{$f};
				}
				// output CSV-formatted line
				$success = fputcsv($fp, $result);
				if(!$success)
				{
					// stop on first failure.
					break;
				}
			}
		}
		
		if(is_string($filename))
		{
			fclose($fp);
		}
		
		return $success;
	}
	
	/**
	 * Import objects from a CSV file.
	 * 
	 * Completely empty rows are automatically skipped, as are rows that
	 * start with a # sign (assumed to be comments).
	 * 
	 * @param	DataMapper $object The type of DataMapper Object to import
	 * @param	mixed $filename Name of CSV file, or a file pointer.
	 * @param	array $fields If empty, the database fields are used.  Otherwise used to limit what fields are saved.
	 * @param	boolean $header_row If true, the first line is assumed to be a header row.  Defaults to true.
	 * @param	mixed $callback A callback method for each row.  Can return FALSE on failure to save, or 'stop' to stop the import.
	 * @return	array Array of imported objects, or FALSE if unable to import.
	 */
	function csv_import($object, $filename, $fields = '', $header_row = TRUE, $callback = NULL)
	{
		$class = get_class($object);
		
		if(empty($fields))
		{
			$fields = $object->fields;
		}
		
		// determine if we need to open the file or not.
		if(is_string($filename))
		{
			// open the file, if possible.
			$fp = fopen($filename, 'r');
			if($fp === FALSE)
			{
				log_message('error', 'CSV Extension: Unable to open file ' . $filename);
				return FALSE;
			}
		}
		else
		{
			// assume file pointer.
			$fp = $filename;
		}
		
		if(empty($callback))
		{
			$result = array();			
		}
		else
		{
			$result = 0;
		}
		$columns = NULL;
		
		while(($data = fgetcsv($fp)) !== FALSE)
		{
			// get column names
			if(is_null($columns))
			{
				if($header_row)
				{
					// store header row for column names
					$columns = $data;
					// only include columns in $fields
					foreach($columns as $index => $name)
					{
						if( ! in_array($name, $fields))
						{
							// mark column as false to skip
							$columns[$index] = FALSE;
						}
					}
					continue;
				}
				else
				{
					$columns = $fields;
				}
			}
			
			// skip on comments and empty rows
			if(empty($data) || $data[0][0] == '#' || implode('', $data) == '')
			{
				continue;
			}
			
			// create the object to save
			$o = new $class();
			foreach($columns as $index => $key)
			{
				if(count($data) <= $index)
				{
					// more header columns than data columns
					break;
				}
				
				// skip columns that were determined to not be needed above.
				if($key === FALSE)
				{
					continue;
				}
				
				// finally, it's OK to save the data column.
				$o->{$key} = $data[$index];
			}
			
			if( empty($callback))
			{
				$result[] = $o;
			}
			else
			{
				$test = call_user_func($callback, $o);
				if($test === 'stop')
				{
					break;
				}
				if($test !== FALSE)
				{
					$result++;
				}
			}
		}
		
		if(is_string($filename))
		{
			fclose($fp);
		}
		
		return $result;
	}
	
}

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