PageRenderTime 168ms CodeModel.GetById 60ms app.highlight 75ms RepoModel.GetById 26ms app.codeStats 0ms

/application/admin/models/newform.php

http://f-engine.googlecode.com/
PHP | 690 lines | 453 code | 192 blank | 45 comment | 98 complexity | cd6432f78bbc3fd57132ba3d8b992a1c MD5 | raw file
  1<?php 
  2class newform extends Model {
  3
  4	private $post;
  5	private $fe;
  6
  7	private $apppath;
  8	private $public_data;
  9	private $foldername;
 10	private $filename;
 11
 12	private $assets;
 13	private $masterview;
 14
 15	public  $insert;
 16	private $insert_vars;
 17
 18	public  $update;
 19	private $update_vars;
 20
 21	public  $datagrid;
 22	private $datagrid_vars;
 23
 24	public  $delete;
 25	private $delete_vars;
 26	
 27	public  $model;
 28	private $model_vars;
 29
 30	private $template_folder;
 31	private $files;
 32
 33    function newform() {
 34
 35        parent::Model();
 36    }
 37
 38    function init($data) {
 39
 40    	$this->post = $data;
 41
 42    	$this->assets = array();
 43
 44    	$this->insert = array();
 45    	$this->insert_vars = array();
 46    	$this->update = array();
 47    	$this->update_vars = array();
 48    	$this->datagrid = array();
 49    	$this->datagrid_vars = array();
 50    	$this->delete = array();
 51    	$this->delete_vars = array();
 52 
 53    	$this->model = array();
 54    	$this->model_vars = array();
 55
 56    	if(isset($this->post["model"]) and $this->post["model"] == 1) {
 57			
 58			$this->template_folder = $this->post["template"]."/model";
 59		} else {
 60			
 61			$this->template_folder = $this->post["template"]."/controller";   
 62		}
 63
 64    	$this->files = array();
 65
 66    	//f-engine instance
 67    	$this->fe =& get_instance();
 68
 69    	/**
 70    	 * set apppath and public_data
 71    	 */
 72    	if( isset($_SESSION['project']) ) {
 73
 74			$apppath = explode("/",str_replace("\\","/", APPPATH));
 75			$apppath[count($apppath)-2] = $_SESSION['project'];
 76
 77			$this->apppath = implode("/",$apppath);
 78			$this->public_data = $this->apppath.'public_data/';
 79
 80		} else {
 81
 82			$this->apppath = APPPATH;
 83			$this->public_data = PUBLIC_DATA;
 84		}
 85
 86		/**
 87    	 * set foldername and filename
 88    	 */
 89		$path = explode('/',$this->post['url']) ? explode('/',$this->post['url']) : array($this->post['url']);
 90
 91		if( trim($path[count($path)-1]) == "")
 92			unset($path[count($path)-1]);
 93
 94		$vpath = $path[0];
 95		$this->filename = $path[count($path)-1];
 96		$this->foldername = $this->post['url'];
 97
 98		if($this->filename == '') 
 99			$this->filename = "unnamed";
100
101		if($this->foldername == '') 
102			$this->foldername = "unnamed";	
103
104		/*
105		 * parse input data and set al required var values
106		 */
107		$this->set_assets();
108		$this->set_insert_vars();
109    	$this->set_update_vars();
110		$this->set_datagrid_vars();
111		$this->set_delete_vars();
112
113		if(isset($this->post["model"]) and $this->post["model"] == 1) {
114			
115			$this->set_model_vars();
116		}
117
118		/*
119		 * Parse form templates
120		 */
121		$this->parse_templates();
122
123		$this->debug(false,false,false);
124    }
125
126    function create_folders() {
127
128    	if(!file_exists($this->apppath.'controllers/'.$this->foldername)) {
129			mkdir($this->apppath.'controllers/'.$this->foldername, 0777, true);
130			chmod($this->apppath.'controllers/'.$this->foldername, 0777);
131		}
132
133		if(!file_exists($this->apppath.'views/'.$this->foldername)) {
134			mkdir($this->apppath.'views/'.$this->foldername, 0777, true);
135			chmod($this->apppath.'views/'.$this->foldername, 0777);
136		}
137    }
138
139    function set_files() {
140
141    	$default_name = $this->filename;
142
143    	if(isset($this->datagrid["form"])) {
144
145    		$this->files["controllers"][$this->foldername.'/'.$default_name.EXT] = $this->datagrid["form"];
146    		$this->files["views"][$this->foldername.'/datagrid_view'.EXT] = $this->datagrid["view"];
147
148    		unset($default_name);
149    	}
150
151    	if( isset($this->insert_vars["form"]) ) {
152
153    		if(isset($default_name)) {
154
155	    		$filename = $default_name;
156	    		unset($default_name);
157
158    		} else {
159
160    			$filename = 'insert';
161    		}
162
163    		$this->files["controllers"][$this->foldername.'/'.$filename.EXT] = $this->insert["form"];
164    		$this->files["controllers"][$this->foldername.'/save'.EXT] = $this->insert["controller"];
165    		$this->files["views"][$this->foldername.'/insert_view'.EXT] = $this->insert["view"];
166    	}
167
168    	if( isset($this->update_vars["form"]) ) {
169
170    		if(isset($default_name)) {
171
172	    		$filename = $default_name;
173	    		unset($default_name);
174
175    		} else {
176
177    			$filename = 'edit';
178    		}
179
180    		$this->files["controllers"][$this->foldername.'/'.$filename.EXT] = $this->update["form"];
181    		$this->files["controllers"][$this->foldername.'/update'.EXT] = $this->update["controller"];
182    		$this->files["views"][$this->foldername.'/edit_view'.EXT] = $this->update["view"];
183    	}
184
185    	if( count($this->delete_vars) > 0 ) {
186
187    		$this->files["controllers"][$this->foldername.'/delete'.EXT] = $this->delete;
188    	}
189
190    	if( count($this->model_vars) > 0 and $this->model_vars["modelname"] != "" ) {
191
192    		$this->files["models"][$this->model_vars["modelname"].EXT] = $this->model;
193    	}
194    }
195
196    function get_files () {
197
198    	return $this->files;
199    }
200
201    function write_files() {
202
203		$this->set_files();
204
205    	foreach($this->files["controllers"] as $filename => $data) {
206
207	    	if (!$handler = fopen($this->apppath.'controllers/'.$filename, 'w+')) {
208		         show_error("No se pudo abrir el archivo ".$this->apppath.'controllers/'.$filename);
209		    }
210
211		    if (fwrite($handler, '<?'.$data.'?>') === FALSE) {
212		        show_error("No se pudo escribir al archivo ".$this->apppath.'controllers/'.$filename);
213		    }
214
215		    fclose($handler);
216			chmod($this->apppath.'controllers/'.$filename, 0777);
217    	}
218
219    	foreach($this->files["views"] as $filename => $data) {
220
221	    	if (!$handler = fopen($this->apppath.'views/'.$filename, 'w+')) {
222		         show_error("No se pudo abrir el archivo ".$this->apppath.'views/'.$filename);
223		    }
224
225		    if (fwrite($handler, $data) === FALSE) {
226		        show_error("No se pudo escribir al archivo ".$this->apppath.'views/'.$filename);
227		    }
228
229		    fclose($handler);
230			chmod($this->apppath.'views/'.$filename, 0777);
231    	}
232
233    	if(isset($this->files["models"])) {
234	        foreach($this->files["models"] as $filename => $data) {
235	
236		    	if (!$handler = fopen($this->apppath.'models/'.$filename, 'w+')) {
237			         show_error("No se pudo abrir el archivo ".$this->apppath.'models/'.$filename);
238			    }
239
240			    if (fwrite($handler, '<?'.$data) === FALSE) {
241			        show_error("No se pudo escribir al archivo ".$this->apppath.'models/'.$filename);
242			    }
243
244			    fclose($handler);
245				chmod($this->apppath.'models/'.$filename, 0777);
246	    	}
247    	}
248    }
249
250    function parse_templates() {
251
252    	/*
253    	 * Filename / classname integrity check
254    	 */ 
255    	$default_classname = $this->filename;
256    	if(isset($this->datagrid_vars["form"])) {
257
258			$this->datagrid_vars["form"]["classname"] = $default_classname;
259    		unset($default_classname);
260    	}
261
262    	if( isset($this->insert_vars["form"]) && isset($default_classname) ) {
263
264    		$this->insert_vars["form"]["classname"] = $default_classname;
265    		unset($default_classname);
266    	}
267
268    	if( isset($this->update_vars["form"]) && isset($default_classname) ) {
269
270			$this->update_vars["form"]["classname"] = $default_classname;
271    	}
272
273    	//datagrid
274    	if( isset($this->datagrid_vars["form"]) )
275			$this->datagrid["form"] = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/resumecontroller',$this->datagrid_vars["form"],true); 
276
277    	if( isset($this->datagrid_vars["view"]) )
278    		$this->datagrid["view"] = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/resume',$this->datagrid_vars["view"],true);
279
280    	//new record
281      	if( isset($this->insert_vars["view"]) )
282    		$this->insert["view"] = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/insert',$this->insert_vars["view"],true);
283
284        if( isset($this->insert_vars["form"]) )
285    		$this->insert["form"] = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/controller',$this->insert_vars["form"],true);
286
287        if( isset($this->insert_vars["controller"]) )
288    		$this->insert["controller"] = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/save',$this->insert_vars["controller"],true);
289
290    	// edit and update
291        if( isset($this->update_vars["view"]) )
292    		$this->update["view"] = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/form',$this->update_vars["view"],true);
293
294        if( isset($this->update_vars["form"]) )
295    		$this->update["form"] = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/savecontroller',$this->update_vars["form"],true);
296
297    	if( isset($this->update_vars["controller"]) )
298    		$this->update["controller"] = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/update',$this->update_vars["controller"],true);
299
300    	//delete
301		if( count($this->delete_vars) > 0 )
302    		$this->delete = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/delete',$this->delete_vars,true);
303
304    	//model
305    	if( count($this->model_vars) > 0 )
306    		$this->model = $this->fe->load->view('tools/newcontroller/templates/'.$this->template_folder.'/model',$this->model_vars,true);
307
308    }
309
310    function set_assets() {
311
312    	if( isset($this->post['js']) )
313    		$this->assets['js'] = "'".implode("','",$this->post['js'])."'";
314
315    	if( isset($this->post['css']) )
316    		$this->assets['css'] = "'".implode("','",$this->post['css'])."'";
317
318    	if( isset($this->post['header']) )
319    		$this->assets['header'] = str_replace("_view.php","","'header/".implode("','",$this->post['header'])."'");
320
321    	if( isset($this->post['footer']) )
322    		$this->assets['footer'] = str_replace("_view.php","","'footer/".implode("','",$this->post['footer'])."'");
323
324    	if( isset($this->post['masterview']) )
325    		$this->masterview = trim($this->post['masterview']);
326    }
327
328	function set_datagrid_vars() {
329
330		if(isset($this->post["resume_form_fields"]) && count($this->post["resume_form_fields"]) > 0) {
331			/**
332	    	 * set datagrid form controller vars 
333	    	 */
334			$databases = array();
335			foreach($this->post['resume_form_fields'] as $item) {
336				$databases[] = substr($item,0,strpos($item,'.'));
337			}
338			$databases = array_unique($databases);
339
340			if(count($databases) > 1 && isset($this->post['resume_rel_field1'])) {
341
342				$where = array();
343				for($i = 0; $i < count($this->post['resume_rel_field1']); $i++) {
344
345					$where[] = "'".$this->post['resume_rel_field1'][$i]." = ".$this->post['resume_rel_field2'][$i]."'";
346				}
347
348				$where = 'array('.implode($where,",").');';
349
350			} else {
351
352				$where = 'array();';	
353			}
354
355			$data = array('fields' => $this->post['resume_field_names'],'data' => array(),
356							'classname'=>$this->foldername,'vpath'=> $this->foldername, 
357							'dbs' => $databases, 'where' => $where);
358
359			if(isset($this->post['resume_form_fields'])) $data['data'] = array_unique(array_merge($data['data'], $this->post['resume_form_fields']));
360			if(isset($this->post['edit_id_fields']))	 $data['data'] = array_unique(array_merge($data['data'],$this->post['edit_id_fields']));
361
362			if(isset($this->post['insert_field_names']))	$data['edit'] = true;
363			if(isset($this->post['edit_field_names']))		$data['new'] = true;
364
365			$data["assets"] = $this->assets;
366			$data["masterview"] = $this->masterview;
367
368			$i=0;
369			foreach( $data["data"] as $key => $val) {
370
371				if( !isset($data["fields"][$i]) || $data["fields"][$i] != array_pop(explode(".", $data['data'][$key])) ) {
372
373					if(isset($data["fields"][$i]))
374						$data['data'][$key] = $data['data'][$key]." as ".$data['fields'][$i];
375					else
376						$data['data'][$key] = $data['data'][$key]." as ".array_pop(explode(".",$data['data'][$key]));
377				}
378				$i++;
379			}
380
381			$this->datagrid_vars["form"] = $data;
382			unset($data);
383
384			/**
385	    	 * set datagrid view controller vars 
386	    	 */
387			$data = array(
388							'fields' => isset($this->post['resume_field_names']) ? $this->post['resume_field_names'] : array(),
389							'data' => isset($this->post['resume_form_fields']) ? $this->post['resume_form_fields'] : array(),
390							'identifiers' => isset($this->post['edit_id_fields']) ? $this->post['edit_id_fields'] : array(),
391							'delete_ids' => isset($this->post['remove_id_fields']) ? $this->post['remove_id_fields'] : array(),
392							'uri' => $_POST['url'],
393							'delete' => isset($this->post['delete']) ? true : false,
394							'edit' => isset($this->post['edit_field_names']) ? true : false,
395							'new' =>  isset($this->post['insert_field_names']) ? true : false,
396							'path' => $this->foldername
397						);
398
399			$this->datagrid_vars["view"] = $data;
400		}
401    }
402
403    function set_insert_vars() {
404
405    	if(isset($this->post["insert_form_fields"]) && count($this->post["insert_form_fields"]) > 0) {
406			/**
407	    	 * set insert form controller vars 
408	    	 */
409			$this->insert_vars["form"] = array(
410
411				'classname' => 'insert',
412				'view' 	=> $this->foldername.'/insert',
413				'assets' => $this->assets
414			);
415
416			/**
417	    	 * set view vars 
418	    	 */
419			$view = array(
420				'target' => 'save',
421				'path' => $this->foldername,
422				'styles' => $this->post['insertform_style']
423			);
424
425			$view['field_names'] = array();
426			if(isset($this->post['insert_field_names'])) {
427
428				$view['field_names'] = $this->post['insert_field_names'];
429			}
430
431			$this->insert_vars["view"] = $view;
432			unset($view);
433
434	    	/**
435	    	 * set save controller vars 
436	    	 */
437	    	$databases = array();
438
439			if(isset($this->post['insert_form_fields'])) {
440
441				foreach($this->post['insert_form_fields'] as $item) {
442
443					$databases[] = substr($item,0,strpos($item,'.'));
444				}
445
446				$data = array(
447							'fields' => $this->post['insert_field_names'],
448							'data' => $this->post['insert_form_fields'],
449						  	'path'=> $this->foldername, 
450							'dbs' => array_unique($databases)
451						);
452
453				$i = 0;
454
455				$data['rules'] = array();
456				foreach($this->post['insert_form_fields'] as $item) {
457
458					$data['rules'][$this->post['insert_field_names'][$i]] = $this->post['insert_validation_rules'][$i++];
459				}
460				
461				$data['ignore'] = array();
462				if(isset($this->post['insert_ignoreInDB'])) {
463
464					$data['ignore'] = $this->post['insert_ignoreInDB'];
465
466				}
467
468				$data['styles'] = $this->post['insertform_style'];
469				$this->insert_vars["controller"] = $data;
470
471			} else {
472				
473				$this->insert_vars["controller"] = array();
474			} 
475    	}
476    }
477
478	function set_update_vars() {
479
480		if(isset($this->post["edit_form_fields"]) && count($this->post["edit_form_fields"]) > 0) {
481		/*
482		 * set edit form controller vars
483		 */ 	
484			$databases = array();
485			foreach($this->post['edit_form_fields'] as $item) {
486
487				$databases[] = substr($item,0,strpos($item,'.'));
488			}
489
490			$data = array(
491				'classname'=>'edit',
492				'path'=> site_url().$this->foldername, 
493				'dbs' => array_unique($databases),
494				'view' => $this->foldername.'/'.'edit',
495				'uripos' => 1,
496				'assets' => $this->assets
497			);
498
499			$data['indexes'] = array();
500			if(isset($this->post['edit_id_fields'])) {
501				$data['indexes'] = $this->post['edit_id_fields'];
502			}
503
504
505			if(isset($_POST['edit_field_names']))
506				$data = array_merge($data,array('field_names' => $this->post['edit_field_names']));
507			else
508				$data['field_names'] = array();
509
510			if(isset($_POST['edit_form_fields']))
511				$data = array_merge($data,array('form_names' => $this->post['edit_form_fields']));
512			else
513				$data['form_names'] = array();
514
515			array_merge($data, array('data' => $this->post['edit_id_fields']) );
516
517			$this->update_vars["form"] = $data;
518			unset($data);
519		/*
520		 * set edit form view vars
521		 */ 
522			$uripos = 1;
523			$target = '/';
524
525			if(isset($this->post['edit_id_fields']))
526				foreach($this->post['edit_id_fields'] as $item) {
527					 
528					$target .= '".$this->uri->param('.$uripos++.')."';
529				}
530
531			$data = array('target' => 'update'.$target, 'path' => $this->foldername);
532
533			if(isset($_POST['edit_field_names']))
534				$data = array_merge($data,array('field_names' => $this->post['edit_field_names']));
535			else
536				$data['field_names'] = array();	
537
538			if(isset($_POST['edit_form_fields']))
539				$data = array_merge($data,array('form_names' => $this->post['edit_form_fields']));
540			else
541				$data['form_names'] = array();	
542
543			$data['styles'] = $this->post['editform_style'];
544			$this->update_vars["view"] = $data;
545			unset($data);
546		/*
547		 * set update controller vars
548		 */ 
549			$uripos = 1;
550
551			$databases = array();
552			if(isset($this->post['edit_form_fields']))
553				foreach($this->post['edit_form_fields'] as $item) {
554		
555					$databases[] = substr($item,0,strpos($item,'.'));
556				}
557
558			$data = array(
559						  	'path'=> $this->foldername, 
560							'dbs' => array_unique($databases),
561							'uripos' => $uripos,
562							'view'  => $this->foldername.'/edit'
563						 );
564
565			if(isset($this->post['edit_form_fields']))
566				$data = array_merge($data,array('fields' => $this->post['edit_field_names'],'data' => $this->post['edit_form_fields']));
567			else {
568
569				$data['edit_field_names'] = array();
570				$data['edit_form_fields'] = array();
571			}
572
573
574			if(isset($this->post['edit_form_fields']))
575				$data = array_merge($data, array('indexes' => $this->post['edit_id_fields']));
576
577			$i = 0;
578
579			$data['rules'] = array();
580			if(isset($this->post['edit_form_fields'])) {
581				foreach($this->post['edit_form_fields'] as $item) {
582
583					$data['rules'][$this->post['edit_field_names'][$i]] = $this->post['edit_validation_rules'][$i++];
584				}
585			}
586
587			$data['ignore'] = array();
588			if(isset($this->post['edit_ignoreInDB'])) {
589
590				$data['ignore'] = $this->post['edit_ignoreInDB'];
591			}
592
593			if(isset($this->post['edit_id_fields']))
594				$data['indexes'] = $this->post['edit_id_fields'];
595			else
596				$data['indexes'] = array();
597
598			$data['styles'] = $this->post['editform_style'];
599			$this->update_vars["controller"] = $data;
600		}
601    }
602
603    function set_delete_vars() {
604
605    	if(isset($this->post["delete"]) && isset($this->post["remove_id_fields"]) && count($this->post["remove_id_fields"]) > 0) {
606	    	$this->delete_vars = array(
607		    	'path' => $this->foldername,
608		    	'table' => array_shift(explode(".",$this->post['remove_id_fields'][0])),
609		    	'field' => $this->post['remove_id_fields'][0]
610		    );
611    	}
612    }
613
614    function set_model_vars() {
615
616    	$this->model_vars["modelname"] = strtolower($this->post["modelname"]);
617
618    	if(isset($this->datagrid_vars["form"])) {
619
620    		$this->model_vars["datagrid"] = $this->datagrid_vars["form"];
621    		$this->datagrid_vars["form"]["modelname"] = $this->post["modelname"];
622    	}
623
624    	if(isset($this->insert_vars["form"])) {
625
626    		$this->model_vars["insert"] = $this->insert_vars["controller"];
627    		$this->insert_vars["form"]["modelname"] = $this->post["modelname"];
628    	}
629
630    	if(isset($this->update_vars["form"])) {
631
632    		$this->model_vars["update"] = $this->update_vars["controller"];
633    		$this->update_vars["form"]["modelname"] = $this->post["modelname"];
634    	}
635
636    	if(count($this->delete_vars) > 0) {
637
638    		$this->model_vars["delete"] = $this->delete_vars;
639    		$this->delete_vars["form"]["modelname"] = $this->post["modelname"];
640    	}
641
642    	if(false) {
643    		echo "<pre>";
644    		print_r($this->model_vars);
645    		die;
646    	}
647    }
648
649    function debug ($input = false, $output = false, $die = true) {
650
651    	if($input) {
652	        echo 'Apppath: '.$this->apppath.'<br />';
653			echo 'Public_data: '.$this->public_data.'<br />';
654			echo 'Foldername: '.$this->foldername.'<br />';
655
656	    	echo "<pre>";
657	    		print_r($this->post);
658	    		echo "@@@@@@@@@@@@@@@@@";
659
660	    		echo "<br />######### insert ###########<br />";
661	    		print_r($this->insert_vars);
662	    		echo "<br />######### update ###########<br />";
663	    		print_r($this->update_vars);
664	    		echo "<br />######### datagrid ###########<br />";
665	    		print_r($this->datagrid_vars);
666	    		echo "<br />######### delete ###########<br />";
667	    		print_r($this->delete_vars);
668	    	echo "</pre>";
669    	}
670
671    	if($output) {
672
673    		echo "<pre>";
674
675    			echo "########## datagrid ##########";
676    			print_r($this->datagrid);
677    			echo "########## insert ##########";
678    			print_r($this->insert);
679    			echo "########## update ##########";
680    			print_r($this->update);
681
682    		echo "</pre>";
683
684    	}
685
686    	if($die)
687    		die;
688    }
689}
690?>