PageRenderTime 3ms CodeModel.GetById 167ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

/restrict/modules/ZendModels/ZendModels.php

http://webmind.googlecode.com/
PHP | 868 lines | 637 code | 67 blank | 164 comment | 41 complexity | 2d59a346837a30044f5847e3d0e14d21 MD5 | raw file
  1<?php
  2/**
  3* Class that generate a ZendModel project
  4*  @filesource
  5 * @author			Jaydson Gomes
  6 * @author			Felipe Nascimento
  7 * @copyright		Copyright <2009> TheWebMind.org
  8 * @package			ZendModels
  9 * @subpackage		restrict.modules
 10 * @version			0.1
 11*/
 12	class ZendModels extends Module implements module_interface
 13	{
 14		private $project;
 15		private $knowledge;
 16		private $structure;
 17				
 18		/**
 19		*Create the header of the file class
 20		*@param String 	$header - The content that the header must have
 21		*@param String  $extra - An opcional text
 22		*@return String
 23		*/
 24		public function headerFile($header,$extra=null){
 25			return $header = $extra != null ? $header." ".$extra."\n" : $header."\n";
 26		}
 27		
 28		/**
 29		* Get who references to the table
 30		*@param Object 	$tableObject - The Table
 31		*@return Array
 32		*/
 33		public function getReferences($tableObject){
 34			$Arrayreferenceds = $tableObject->refered;
 35			$referenceds = Array();						
 36			for($i=0;$i<sizeof($Arrayreferenceds);$i++){
 37				$referenceds[$i] = explode("|",$Arrayreferenceds[$i]);
 38				$referenceds[$i] = $referenceds[$i][1];
 39			}
 40			return $referenceds;
 41		}
 42		
 43		/**
 44		* Get foreign keys
 45		*@param Object 	$tableObject - The Table
 46		*@return Array
 47		*/
 48		public function getForeignKeys($tableObject){
 49			$foreignKeys = $tableObject->foreignKeys;
 50			$ret= Array();
 51			for($i=0;$i<sizeof($foreignKeys);$i++){
 52				$ret[$foreignKeys[$i][0]]= $foreignKeys[$i][1];
 53			}
 54			return $ret;
 55		}
 56		
 57		/**
 58		* Get primary keys
 59		*@param Object 	$tableObject - The Table
 60		*@return String
 61		*/
 62		public function getPrimaryKey($tableObject){
 63			$attr = $tableObject->attributes;
 64			reset($attr);
 65			return key($attr);
 66		}
 67		
 68		/**
 69		* Prepare the name of Class
 70		*@param String 	$name - The name of class
 71		*@return String
 72		*/
 73		public function prepareClassName($name){
 74			$tempName = str_replace('_',' ',$name);
 75			$tempName = ucwords($tempName);
 76			return str_replace(' ','',$tempName);
 77		}
 78		
 79		/**
 80		* Prepare the name of Label
 81		*@param String 	$name - The name of class
 82		*@return String
 83		*/
 84		public function prepareLabelName($name){
 85			return ucwords(str_replace("_"," ",$name));
 86		}
 87		
 88		/**
 89		* Prepare the folder name
 90		*@param String 	$name - The name of class
 91		*@return String
 92		*/
 93		public function prepareFolderName($name){
 94			//return strtolower(str_replace("_","-",$name));
 95			$tempName = str_replace('_',' ',$name);
 96			$tempName = ucwords($tempName);
 97			return str_replace(' ','',$tempName);
 98		}
 99		
100				
101		/**
102		* Prepare name to show on select options
103		*@param String 	$name - The name of table
104		*@return String
105		*/
106		public function getOptionLabel($table){	
107			$table = $this->knowledge->tables[$table];
108			reset($table->attributes);
109			$temp;
110			$counter=0;
111			while($cur = current($table->attributes)){
112				if($counter<2){
113					$temp = $cur->name;
114				}
115				switch($cur->type){
116					case 'string':
117					case 'char':
118					case 'varchar':
119					case 'text':
120						return $cur->name;
121					break;
122				} 
123				next($table->attributes);
124				$counter++;
125			}
126			return $temp;
127		}
128		
129		/**
130		*Create a Class comment
131		*@param Array 	$commentLines - An array with n lines that you want to comment
132		*@param int 		$tabs A number that define how much tabs have in the comment
133		*@return String
134		*/
135		public function createComment($commentLines,$tabs=null){
136			$tabs = $tabs != null ? $tabs : 0;
137			$tab=$this->setTabText($tabs);
138			$c = $tab."/**\n";
139			for($i=0;$i<sizeof($commentLines);$i++){
140				$c.= $i==sizeof($commentLines) ? $tab."* ".$commentLines[$i] : $tab."* ".$commentLines[$i]."\n";
141			}
142			$c.= $tab."*/\n";
143			return $c;
144		}
145		
146		/**
147		* Create Models
148		*@param Table 	$tableObject - Mind Table Object, that contais all knowledge
149		*@return String
150		*/
151		public function models(&$tableObject){
152			$name = $tableObject->name;
153			$attributes = $tableObject->attributes;
154			$referenceds = $this->getReferences($tableObject);
155			
156			//Header
157			$content = $this->headerFile("<?php");						
158			$content.= $this->createComment(array("Generate by TheWebMind 2.0 Software","Model Class for ".$this->prepareClassName($name),"@license	http://www.opensource.org/licenses/mit-license.php","@link http://thewebmind.org","@author".$this->setTabText(1)."Jaydson Gomes <jaydson@thewebmind.org>","@author".$this->setTabText(1)."Felipe N. Moura <felipe@thewebmind.org>", "@version  1.0"));
159			
160			//Class
161			$content.= "class ".$this->prepareClassName($name)." extends Zend_Db_Table {\n";
162			
163			//Protecteds
164			$content.= $this->setTabText(1)."protected \$_name = '".$name."';\n";
165			if(sizeof($referenceds)>0){
166				$content.= $this->setTabText(1)."protected \$_dependentTables = array(";
167				for($i=0;$i<sizeof($referenceds);$i++){
168					if($i>0){
169						$content.= ", ";
170						$content.= "\n".$this->setTabText(3)."                            ";
171					}
172					$content.="'".$this->prepareClassName($referenceds[$i])."'";
173				}
174				$content.= ");\n";
175			}
176			
177			//Reference Map
178			if(sizeof($tableObject->foreignKeys) > 0)
179			{
180				$content.="
181	protected \$_referenceMap = array(";
182				$fks= $this->getForeignKeys($tableObject);
183				reset($fks);
184				$i= 0;
185				while($cur= current($fks))
186				{
187					$tmpPk= $this->getPrimaryKey($this->knowledge->tables[$cur]);
188					if($i>0)
189					{
190						$content.=",
191									  '".$this->prepareClassName($cur)."'";
192					}else{
193							$content.= "'".$this->prepareClassName($cur)."'";
194						 }
195					$content.=" => Array(";
196					$content.="'columns'   => Array('".key($fks)."'),
197													    ";
198					$content.="'refTableClass'   => '".$this->prepareClassName($cur)."',
199													    ";
200					$content.="'refColumns'   => Array('".$tmpPk."')";
201					$content.="
202														)";
203					next($fks);
204					$i++;
205				}
206				$content.="
207										);\n";
208			}
209			// Save
210			$content.= $this->createComment(array("Save an object ".$this->prepareClassName($name)." into Database","@return void"),1);
211			$content.= $this->setTabText(1)."public function save(){\n";
212			$content.= $this->setTabText(2)."\$post = Zend_Registry::get('post');\n";
213			reset($attributes);
214			next($attributes);
215			while($cur = current($attributes)){
216				if($cur->required)
217				{
218					$content.= $this->setTabText(2)."if(!isset(\$post->".$cur->name.") || str_replace('/ /', '', \$post->".$cur->name.") == '')
219		{
220			die('Required field: ".(isset($cur->comment) ? $cur->comment : $this->prepareLabelName($cur->name))."');
221		}\n";
222				}
223				next($attributes);
224			}
225			reset($attributes);
226			$pk = key($attributes);
227			$content.= $this->setTabText(2)."if(isset(\$post->".$pk.")){\n";
228			$content.= $this->setTabText(3)."\$where = \$this->getAdapter()->quoteInto('".$pk." = ?', \$post->".$pk.");\n";
229			$content.= $this->setTabText(3)."\$data = array(";
230			$counter=0;
231			
232			//Ignoring primary key
233			next($attributes);
234			while($cur = current($attributes)){
235				if($counter>0){
236					$content.= ",\n".$this->setTabText(6)."  ";
237				}
238				$content.= "'".$cur->name."' => \$post->".$cur->name;
239				next($attributes);
240				$counter++;
241			}
242			
243			// Foreign Keys
244			$fks = $this->getForeignKeys($tableObject);
245			while($cur = current($fks)){
246				if($counter>0){
247					$content.= ",\n".$this->setTabText(6)."  ";
248				}
249				$pk = reset($this->knowledge->tables[$cur]->attributes);
250				$content.= "'".key($fks)."' => \$post->".$pk->name;
251				next($fks);
252				$counter++;
253			}
254			
255			$content.= ");\n";
256			$content.= $this->setTabText(3)."\$this->update(\$data,\$where);\n";
257			$content.= $this->setTabText(2)."}else{\n";
258			reset($attributes);
259			$content.= $this->setTabText(4)."\$data = array(";
260			$counter=0;
261			//Ignoring primary key
262			next($attributes);
263			while($cur = current($attributes)){
264				if($counter>0){
265					$content.= ",\n".$this->setTabText(7)."  ";
266				}
267				$content.= "'".$cur->name."' => \$post->".$cur->name;
268				next($attributes);
269				$counter++;
270			}
271			
272			// Foreign Keys
273			$fks = $this->getForeignKeys($tableObject);
274			while($cur = current($fks)){
275				if($counter>0){
276					$content.= ",\n".$this->setTabText(7)."  ";
277				}
278				$pk = reset($this->knowledge->tables[$cur]->attributes);
279				$content.= "'".key($fks)."' => \$post->".$pk->name;
280				next($fks);
281				$counter++;
282			}
283			$content.= ");\n";
284			$content.= $this->setTabText(4)."\$this->insert(\$data);\n";
285			$content.= $this->setTabText(3)."};\n";
286			$content.= $this->setTabText(1)."}\n\n";
287			
288			// getCollection
289			$content.= $this->createComment(array("List of objects ".$this->prepareClassName($name)." from Database","@return Collection of ".$this->prepareClassName($name)),1);
290			$content.= $this->setTabText(1)."public function getCollection(){\n";
291				$content.= $this->setTabText(2)."\$view = Zend_Registry::get('view');\n";
292				$content.= $this->setTabText(2)."\$collection = \$this->fetchAll();\n";
293				$content.= $this->setTabText(2)."\$view->assign('".$name."Collection',\$collection);\n";
294				$content.= $this->setTabText(2)."\$view->assign('header','pageHeader.phtml');\n";
295				$content.= $this->setTabText(2)."\$view->assign('body','".$this->prepareClassName($name)."/bodyList.phtml');\n";
296				$content.= $this->setTabText(2)."\$view->assign('footer','pageFooter.phtml');\n";
297				$content.= $this->setTabText(2)."if(sizeof(\$collection)==0){\n";
298				$content.= $this->setTabText(3)."\$view->assign('message','Empty');\n";
299				$content.= $this->setTabText(2)."}\n";
300			$content.= $this->setTabText(1)."}\n\n";
301			
302			//Edit
303			$content.= $this->createComment(array("Edit an object ".$this->prepareClassName($name),"@return void"), 1);
304			$content.= $this->setTabText(1)."public function edit(){\n";
305			$content.= $this->setTabText(2)."\$get = Zend_Registry::get('get');\n";
306			$content.= $this->setTabText(2)."\$view = Zend_Registry::get('view');\n";
307			$content.= $this->setTabText(2)."if (!isset(\$get->id))\n";
308			$content.= $this->setTabText(2)."{\n";
309				$content.= $this->setTabText(3)."\$this->_redirect('/".$this->prepareClassName($name)."/list');\n";
310				$content.= $this->setTabText(3)."exit;\n";
311			$content.= $this->setTabText(2)."}\n";
312			$content.= $this->setTabText(2)."\$id = (int)\$get->id;\n";
313			$content.= $this->setTabText(2)."\$".$name."Selected = \$this->find(\$id)->toArray();\n";
314			
315			$content.= $this->setTabText(2)."\$view->assign('".$name."',\$".$name."Selected[0]);\n";
316			$content.= $this->setTabText(2)."\$view->assign('header','pageHeader.phtml');\n";
317			$content.= $this->setTabText(2)."\$view->assign('body','".$this->prepareClassName($name)."/bodyEdit.phtml');\n";
318			$content.= $this->setTabText(2)."\$view->assign('footer','pageFooter.phtml');\n";
319			$content.= $this->setTabText(1)."}\n\n";
320			
321			//Delete
322			$pk = $this->getPrimaryKey($tableObject);
323			$content.= $this->createComment(array("Remove an object ".$this->prepareClassName($name),"@return void"), 1);
324			$content.= $this->setTabText(1)."public function remove(){\n";
325			$content.= $this->setTabText(2)."\$get = Zend_Registry::get('get');\n";
326			$content.= $this->setTabText(2)."if(!isset(\$get->id)){\n";
327			$content.= $this->setTabText(3)."\$this->_redirect('/".$this->prepareClassName($name)."/list');\n";
328			$content.= $this->setTabText(3)."exit;\n";
329			$content.= $this->setTabText(2)."}\n";
330			$content.= $this->setTabText(2)."\$".$pk." = (int)\$get->id;\n";
331			$content.= $this->setTabText(2)."\$where = \$this->getAdapter()->quoteInto('".$pk." = ?', \$".$pk.");\n";
332			$content.= $this->setTabText(2)."\$this->delete(\$where);\n";
333			$content.= $this->setTabText(1)."}\n\n";
334			
335			// End of Class
336			$content.= "\n}";
337			return $content;
338		}
339		
340		/* Create Controllers
341		*@param Table $tableObject - Mind Table Object, that contais all knowledge
342		*@return String
343		*/
344		public function controllers(&$tableObject){
345			$name = $tableObject->name;
346			
347			$referenceds = $this->getReferences($tableObject);
348			$fks = $this->getForeignKeys($tableObject);
349			// Header File
350			$content = $this->headerFile("<?php");			
351			$content.= $this->createComment(array("Generate by TheWebMind 2.0 Software","Controller Class for ".$name,"@license	http://www.opensource.org/licenses/mit-license.php","@link http://thewebmind.org","@author".$this->setTabText(1)."Jaydson Gomes <jaydson@thewebmind.org>","@author".$this->setTabText(1)."Felipe N. Moura <felipe@thewebmind.org>", "@version  1.0"));
352			
353			// Class
354			$content.= "class ".$this->prepareClassName($name)."Controller extends Zend_Controller_Action {\n";			
355			
356			// Init
357			$content.= $this->createComment(array('Overrides Zend_Controller_Action.init','Load classes needed'),1);
358			$content.= $this->setTabText(1)."public function init(){\n";
359			$content.= $this->setTabText(2)."Zend_Loader::loadClass('".$this->prepareClassName($name)."');";
360			reset($fks);
361			while($cur = current($fks)){
362				$content.= "\n".$this->setTabText(2)."Zend_Loader::loadClass('".$this->prepareClassName($cur)."');";
363				next($fks);
364			}
365			$content.= "\n".$this->setTabText(1)."}\n\n";
366			
367			// indexAction
368			$content.= $this->createComment(array('Redirect to View List'),1);
369			$content.= $this->setTabText(1)."public function indexAction(){\n";
370			$content.= $this->setTabText(2)."\$this->_redirect('".$this->prepareClassName($name)."/list');";
371			$content.= "\n".$this->setTabText(1)."}\n\n";
372			
373			//Add
374			$content.= $this->createComment(array("Action to add object ".$this->prepareClassName($name),"@return void"), 1);
375			$content.= $this->setTabText(1)."public function addAction(){\n";
376			reset($fks);
377			while($cur = current($fks)){
378				$content.= $this->setTabText(2)."\$".$cur." = new ".$this->prepareClassName($cur)."();\n";
379				next($fks);
380			}
381			$content.= $this->setTabText(2)."\$view = Zend_Registry::get('view');\n";
382			$content.= $this->setTabText(2)."\$view->assign('header','pageHeader.phtml');\n";
383			$content.= $this->setTabText(2)."\$view->assign('body','".$this->prepareClassName($name)."/bodyAdd.phtml');\n";
384			$content.= $this->setTabText(2)."\$view->assign('footer','pageFooter.phtml');\n";
385			reset($fks);
386			while($cur = current($fks)){
387				$content.= $this->setTabText(2)."\$view->assign('".$cur."Collection',\$".$cur."->fetchAll());\n";
388				next($fks);
389			}			
390			$content.= $this->setTabText(2)."\$this->_response->setBody(\$view->render('default.phtml'));\n";
391			$content.= $this->setTabText(1)."}\n\n";
392
393			//Save
394			$content.= $this->createComment(array("Action to save object ".$this->prepareClassName($name),"@return void"), 1);
395			$content.= $this->setTabText(1)."public function saveAction(){\n";
396			$content.= $this->setTabText(2)."\$table = new ".$this->prepareClassName($name)."();\n";
397			$content.= $this->setTabText(2)."\$table->save();\n";
398			$content.= $this->setTabText(2)."\$this->_redirect('".$this->prepareClassName($name)."/list');\n";
399			$content.= $this->setTabText(1)."}\n\n";
400			
401			//List
402			$content.= $this->createComment(array("Action to list object ".$this->prepareClassName($name),"@return void"), 1);
403			$content.= $this->setTabText(1)."public function listAction(){\n";
404			$content.= $this->setTabText(2)."\$view = Zend_Registry::get('view');\n";
405			$content.= $this->setTabText(2)."\$table = new ".$this->prepareClassName($name)."();\n";
406			$content.= $this->setTabText(2)."\$table->getCollection();\n";
407			$content.= $this->setTabText(2)."\$this->_response->setBody(\$view->render('default.phtml'));\n";
408			$content.= $this->setTabText(1)."}\n\n";
409			
410			//Edit
411			$content.= $this->createComment(array("Action to edit object ".$this->prepareClassName($name),"@return void"), 1);
412			$content.= $this->setTabText(1)."public function editAction(){\n";
413			$content.= $this->setTabText(2)."\$view = Zend_Registry::get('view');\n";
414			reset($fks);
415			while($cur = current($fks)){
416				$content.= $this->setTabText(2)."\$".$cur." = new ".$this->prepareClassName($cur)."();\n";
417				$content.= $this->setTabText(2)."\$view->assign('".$cur."Collection',\$".$cur."->fetchAll());\n";
418				next($fks);
419			}
420			$content.= $this->setTabText(2)."\$table = new ".$this->prepareClassName($name)."();\n";
421			$content.= $this->setTabText(2)."\$table->edit();\n";
422			$content.= $this->setTabText(2)."\$this->_response->setBody(\$view->render('default.phtml'));\n";
423			$content.= $this->setTabText(1)."}\n\n";
424			
425			//Remove
426			$content.= $this->createComment(array("Action to remove object ".$this->prepareClassName($name),"@return void"), 1);
427			$content.= $this->setTabText(1)."public function removeAction(){\n";
428			$content.= $this->setTabText(2)."\$table = new ".$this->prepareClassName($name)."();\n";
429			$content.= $this->setTabText(2)."\$table->remove();\n";
430			$content.= $this->setTabText(2)."\$this->_redirect('".$name."/list');\n\n";
431			$content.= $this->setTabText(1)."}\n\n";
432			$content.= "}";
433			
434			return $content;
435		}
436		
437		
438		/* Create Body Add Form
439		*@param Table $tableObject - Mind Table Object, that contais all knowledge
440		*@return String
441		*/
442		public function bodyAddForm(&$tableObject,$edit=false){
443			$name = $tableObject->name;
444			$attributes = $tableObject->attributes;
445			$content = "<h2 id='warning'><?php echo \$this->message?></h2>\n";
446			$content.= "<center>\n";
447			$content.= "<form action='<?php echo PATH_APPLICATION; ?>/".$this->prepareClassName($name)."/save' method='post'>\n";
448			$content.= "<table>\n";
449				reset($attributes);
450				if($edit){
451					$cur = current($attributes);
452					$label = isset($cur->comment) ? $cur->comment : $this->prepareLabelName($cur->name);
453					$content.= $this->setTabText(1)."<tr style='display:none'>\n";
454					
455						//Field
456						$content.= $this->setTabText(2)."<td colspan='2'>\n";
457							$content.= $this->setTabText(3)."<input name='".$cur->name."' value='<?php echo \$this->".$name."['".$cur->name."'];?>'";
458							if($cur->size > 0)
459							{
460								$content.= " maxlength='".$cur->size."'";
461							}
462							$content.= " id='".$cur->name."' type='hidden'>\n";
463						$content.= $this->setTabText(2)."</td>\n";
464					$content.= $this->setTabText(1)."</tr>\n";
465				}
466				$dateInputs= Array();
467				$maskedInputs= Array();
468				$multiPart= false;
469				$textarea = false;
470				next($attributes);
471				while($cur = current($attributes)){
472					$def= (substr($cur->defaultValue, 0, 5)=='Exec:')? '': preg_replace('/^\'|\'$/', '', $cur->defaultValue);
473					$value = $edit ? "<?php echo \$this->".$name."['".$cur->name."']; ?>" : $def;
474					$label = isset($cur->comment) ? $cur->comment : $this->prepareLabelName($cur->name);
475					$textarea = ($cur->type == 'text' || $cur->size > 200) ? true : false;
476					if($cur->type== 'time')
477					{
478						$dateInputs[]= $cur->name;
479					}elseif($cur->type== 'file')
480						 {
481							$multiPart= true;
482						 }
483					if($cur->mask)
484					{
485						$maskedInputs[]= Array($cur->name, $cur->mask);
486					}
487					$content.= $this->setTabText(1)."<tr>\n";
488						//Label
489						$content.= $this->setTabText(2)."<td>\n";
490							$content.= $this->setTabText(3).$label.":\n";
491						$content.= $this->setTabText(2)."</td>\n";
492						
493						//Field
494						$content.= $this->setTabText(2)."<td>\n";
495						if(!isset($cur->options))
496						{
497							if($textarea){
498								$content.= $this->setTabText(3)."<textarea name='".$cur->name."'";
499								$content.= " id='".$cur->name."' style='width:360px; height:210px;'>".$value."</textarea> \n";
500							}else{
501									$content.= $this->setTabText(3)."<input name='".$cur->name."' value='".$value."'";
502									if($cur->size > 0)
503									{
504										$content.= " maxlength='".$cur->size."'";
505									}
506									$content.= " id='".$cur->name."' type='".(($cur->type== 'file')? 'file': 'text')."'>\n";
507								}
508						}else{
509								$content.= $this->setTabText(3)."<select id='".$cur->name."' name='".$cur->name."'>";
510								if(!$cur->required)
511								{
512									$content.= $this->setTabText(4)."<option value=''></option>";
513								}
514								for($i=0, $j=sizeof($cur->options); $i<$j; $i++)
515								{
516									$content.= "\n".$this->setTabText(4)."<option value='".$cur->options[$i][0]."' ";
517									if($edit)
518									{
519										$content.= "<?php echo (\$this->".$name."['".$cur->name."'] == '".$cur->options[$i][0]."')? \" selected='selected'\" : ''; ?> ";
520									}
521									$content.= ">\n".$this->setTabText(5).$cur->options[$i][1]."\n".$this->setTabText(4)."</option>";
522								}
523								$content.= "\n".$this->setTabText(3)."</select>\n";
524								//print_r($cur->options);
525							 }
526						$content.= $this->setTabText(2)."</td>\n";
527					$content.= $this->setTabText(1)."</tr>\n";
528					//enctype="multipart/form-data"
529					next($attributes);
530				}
531				if($multiPart)
532				{
533					$content= preg_replace("/\<form /", "<form enctype='multipart/form-data' ", $content, 1);
534				}
535				
536				// Foreign Keys
537				$fks = $this->getForeignKeys($tableObject);
538				reset($fks);
539				while($cur = current($fks)){
540					$pk = reset($this->knowledge->tables[$cur]->attributes);
541					$content.= $this->setTabText(1)."<tr>\n";
542						//Label
543						$content.= $this->setTabText(2)."<td>\n";
544							$content.= $this->setTabText(3).$this->prepareLabelName($cur).":\n";
545						$content.= $this->setTabText(2)."</td>\n";
546						
547						//Field						
548						$content.= $this->setTabText(2)."<td>\n";
549							$content.= $this->setTabText(3)."<select name='".$pk->name."'>\n";
550								$content.= $this->setTabText(4)."<?php\n";
551									$content.= $this->setTabText(5)."foreach (\$this->".$cur."Collection as \$".$cur.")\n";
552									$content.=$this->setTabText(5)."{\n";
553										$content.=$this->setTabText(6)."?>\n";
554											$content.=$this->setTabText(7)."<option <?php echo \$this->".$name."['".$this->getPrefixNameFK().$cur."'] == \$".$cur."->".$this->getPrefixNamePK().$cur." ? 'selected' : ''; ?> value=\"<?php echo \$".$cur."->".$pk->name."; ?>\">\n";
555												$content.=$this->setTabText(8)."<?php echo \$".$cur."->".$this->getOptionLabel($cur)."; ?>\n";
556											$content.=$this->setTabText(7)."</option>\n";
557										$content.=$this->setTabText(6)."<?php\n";
558									$content.=$this->setTabText(5)."}\n";
559								$content.= $this->setTabText(3)."?>\n";
560							$content.= $this->setTabText(3)."</select>\n";
561						$content.= $this->setTabText(2)."</td>\n";
562					$content.= $this->setTabText(1)."</tr>\n";
563					next($fks);
564				}
565			$content.= $this->setTabText(1)."<tr>\n";
566				$content.= $this->setTabText(2)."<td>\n";
567					$content.= $this->setTabText(3)."<input type='submit'>\n";
568				$content.= $this->setTabText(2)."</td>\n";
569			$content.= $this->setTabText(1)."</tr>\n";
570			$content.= "</table>\n";
571			$content.= "</form>\n</center>";
572			$content.= "
573	<script type='text/javascript'>
574		$(function() {";
575			if(sizeof($maskedInputs)>0)
576			{
577				for($i=0, $j=sizeof($maskedInputs); $i<$j; $i++)
578				{
579					$content.= "
580			$('#".$maskedInputs[$i][0]."').mask('".$maskedInputs[$i][1]."');";
581				}
582			}
583			if(sizeof($dateInputs)>0)
584			{
585				for($i=0, $j=sizeof($dateInputs); $i<$j; $i++)
586				{
587					$content.= "
588			$('#".$dateInputs[$i]."').datepicker({
589				changeMonth: true,
590				changeYear: true
591			});";
592				}
593			}
594			$content.= "
595		});
596	</script>";
597			return $content;
598		}
599		
600		/* Create Body Edit Form
601		*@param Table $tableObject - Mind Table Object, that contais all knowledge
602		*@return String
603		*/
604		public function bodyEditForm(&$tableObject){
605			return $this->bodyAddForm($tableObject,true);
606		}
607		
608		/* Create Body List Form
609		*@param Table $tableObject - Mind Table Object, that contais all knowledge
610		*@return String
611		*/
612		public function bodyListForm(&$tableObject){
613			$name = $tableObject->name;
614			$attributes = $tableObject->attributes;
615			$referenceds = $this->getReferences($tableObject);
616			$pk = $this->getPrimaryKey($tableObject);
617			$content= "<h2 id='warning'><?php echo \$this->message?></h2>
618<div id='list'>
619	<?php 
620	foreach (\$this->".$name."Collection as $".$name.")
621	{
622		";
623		for($i=0;$i<sizeof($referenceds);$i++){
624			$content.= "\$".$referenceds[$i]."Collection = \$".$name."->findDependentRowset('".$this->prepareClassName($referenceds[$i])."');\n";
625		}
626		$content.= "
627		?>
628			<table border='0'>
629				<tr>
630					<td style='text-align: left'>
631							<a href='<?php echo PATH_APPLICATION; ?>/".$this->prepareClassName($name)."/edit?id=<?php echo (\$".$name."->".$pk."); ?>'>
632								<?php echo (\$".$name."->".$this->getOptionLabel($name).");?>
633							</a><br>
634							";
635		for($i=0;$i<sizeof($referenceds);$i++){
636			$content.= "<?php 
637								if(sizeof(\$".$referenceds[$i]."Collection".")>0){
638							?>
639							<i>".$this->prepareClassName($referenceds[$i]).":</i><br>
640								<div style='color:blue;font-size: 12px'>
641								<?php
642								foreach (\$".$referenceds[$i]."Collection"." as $".$referenceds[$i].")
643								{
644									echo \$".$referenceds[$i]."->".$this->getOptionLabel($referenceds[$i]).".'<br>';
645								}
646								?>
647								</div>
648								<?php }?>\n";
649		}
650		$content.="				</td>
651					<td style='vertical-align: top'>
652						<a href='<?php echo PATH_APPLICATION; ?>/".$this->prepareClassName($name)."/remove?id=<?php echo (\$".$name."->".$pk."); ?>'><button>Delete</button></a>	
653					</td>
654				</tr>
655			</table>
656		<?php		
657	}
658	?>
659</div>
660";
661			return $content;
662		}
663		
664		/* Create pageHeader
665		*@return String
666		*/
667		public function pageHeader(){
668			$tables = $this->knowledge->tables;
669			$content = "<?php
670\$session = Zend_Registry::get('session');
671?>
672<div id='header'>
673	Generate by TheWebMind beta 2.0 at ".date('m/d/Y')."
674</div>
675
676<div id='menu' class='menu'>
677	<ul id='browser' class='filetree'>";
678			reset($tables);
679			while($table = current($tables)){
680				$content.= "
681		<li>
682			<span class='folder'>".$this->prepareLabelName($table->name)."</span>
683			<ul>
684				<li><span class='file'><a href='<?php echo PATH_APPLICATION; ?>/".$this->prepareClassName($table->name)."/add'>Add</a></span></li>
685				<li><span class='file'><a href='<?php echo PATH_APPLICATION; ?>/".$this->prepareClassName($table->name)."/list'>List</a></span></li>
686			</ul>
687		</li>";
688			next($tables);
689			}
690			$content .= "	</ul>
691</div>";
692			return $content;
693		}
694		
695		/* Create Default.phtml		
696		*@return String
697		*/
698		public function defaultPhtml(){
699			$content = "<html xmlns='http://www.w3.org/1999/xhtml'>
700<head>
701<title>".$this->project->name."</title>
702<link rel='stylesheet' type='text/css' href='/".$this->project->name."/public/styles/zend.generic.css' />
703<link rel='stylesheet' href='/".$this->project->name."/public/styles/jquery.treeview.css' />
704<script src='/".$this->project->name."/public/scripts/jquery.js' type='text/javascript'></script>
705<script src='/".$this->project->name."/public/scripts/default.js' type='text/javascript'></script>
706<script src='/".$this->project->name."/public/scripts/jquery.treeview.js' type='text/javascript'></script>
707</head>
708<body>
709	<?php echo \$this->render(\$this->header)?>
710	<div id='container'>
711	<!-- Content -->
712	<?php echo \$this->render(\$this->body)?>
713	</div>
714</body>
715</html>";
716			return $content;
717		}
718		
719			
720		/* Create Views
721		*@param Table $tableObject - Mind Table Object, that contais all knowledge
722		*@return String
723		*/
724		public function views(&$tableObject){
725			$name = $tableObject->name;
726			$attributes = $tableObject->attributes;
727			$referenceds = $this->getReferences($tableObject);			
728			$folder = $this->prepareFolderName($name);
729			$this->fw->mkDir('project_name/application/views/scripts/'.$folder);
730			$this->fw->mkDir('project_name/application/views/scripts/'.strtolower($folder));
731			
732			//Add
733			$this->fw->mkFile('project_name/application/views/scripts/'.strtolower($folder).'/add.phtml','');
734			$this->fw->mkFile('project_name/application/views/scripts/'.$folder.'/add.phtml','');
735			
736			//Edit
737			$this->fw->mkFile('project_name/application/views/scripts/'.strtolower($folder).'/edit.phtml','');
738			$this->fw->mkFile('project_name/application/views/scripts/'.$folder.'/edit.phtml','');
739			
740			//List
741			$this->fw->mkFile('project_name/application/views/scripts/'.strtolower($folder).'/list.phtml','');
742			$this->fw->mkFile('project_name/application/views/scripts/'.$folder.'/list.phtml','');
743			
744			//bodyAdd.phtml
745			$this->fw->mkFile('project_name/application/views/scripts/'.$folder.'/bodyAdd.phtml',$this->bodyAddForm($tableObject));
746			
747			//bodyList.phtml
748			$this->fw->mkFile('project_name/application/views/scripts/'.$folder.'/bodyList.phtml',$this->bodyListForm($tableObject));
749			
750			//bodyEdit.phtml
751			$this->fw->mkFile('project_name/application/views/scripts/'.$folder.'/bodyEdit.phtml',$this->bodyEditForm($tableObject));			
752						
753		}
754		
755		/**
756		* Tells mind what the structure to copy
757		*@return String
758		*/
759		public function getStructure(){
760			return $this->structure;
761		}
762		
763		/**
764		*Method Called for each table
765		*@param Table 	$tableObject - Mind Table Object, that contais all knowledge
766		*@return void
767		*/
768		public function applyCRUD($tableObject){
769			//Models
770			$this->fw->mkFile('project_name/application/models/'
771							  .$this->prepareClassName($tableObject->name).'.php',
772							  $this->models($tableObject));
773			
774			//Controllers
775			$this->fw->mkFile('project_name/application/controllers/'
776							  .$this->prepareClassName($tableObject->name).'Controller.php',
777							  $this->controllers($tableObject));
778
779			//Views
780			$this->views($tableObject);
781		}
782		
783		/**
784		*Method that return the correct PDO Adapter name
785		*@param type A String with the type of DBMS
786		*@return String
787		*/
788		public function getPDOAdapter($type){
789			$pdoAdapter = '';
790			
791			switch(strtolower($type)){
792				case 'mysql' :
793					$pdoAdapter = 'Pdo_Mysql';
794				break;
795				case 'postgresql' :
796					$pdoAdapter = 'Pdo_Pgsql';
797				break;
798				case 'sqlite' :
799					$pdoAdapter = 'Pdo_Sqlite';
800				break;
801				case 'oracle':
802					$pdoAdapter = 'Pdo_Oci';
803				break;
804				case 'mssql':
805					$pdoAdapter = 'Pdo_Mssql';
806				break;
807				case 'db2':
808					$pdoAdapter = 'Pdo_Ibm';
809				break;
810				default : $pdoAdapter = 'Pdo_Mysql';
811			}
812			return $pdoAdapter;
813		}		
814		
815		/**
816		* Method executed when module start
817		*/
818		public function onStart(){
819			// Create database config file
820			$file = $this->fw->getContent("config.ini");
821			$file = str_replace("<dbType>",$this->getPDOAdapter($this->project->dbms),$file);
822			$file = str_replace("<host>",$this->project->environment["development"]["dbAddress"],$file);
823			$file = str_replace("<user>",$this->project->environment["development"]["user"],$file);
824			$file = str_replace("<pass>",$this->project->environment["development"]["userPwd"],$file);
825			$file = str_replace("<db>",$this->project->environment["development"]["dbName"],$file);
826			$file = str_replace("<port>",$this->project->environment["development"]["dbPort"],$file);
827			$this->fw->mkFile('project_name/application/config.ini',$file);
828		}
829		
830		/**
831		* Override Module.onFinish
832		* Method executed when module start
833		*/
834		public function onFinish(){
835			// Renames the root dir
836			$this->fw->rename('project_name',$this->project->name);
837		}
838		
839		/**
840		* Override Module.callExtra
841		* Call extra function
842		*/
843		public function callExtra(){
844			//Create pageHeader 
845			$this->fw->mkFile($this->project->name.'/application/views/scripts/pageHeader.phtml',$this->pageHeader());
846			
847			//Create default.phtml
848			$this->fw->mkFile($this->project->name.'/application/views/scripts/dafault.phtml',$this->defaultPhtml());
849			
850			// Replace project information
851			$this->fw->mkFile($this->project->name."/index.php",
852							  str_replace("<projectName>",
853										  $this->project->name,
854										  $this->fw->getContent("structure/project_name/index.php")));
855		}
856		
857		/**
858		* Construct
859		*/
860		public function __construct($project){
861			GLOBAL $_FW;
862			$this->fw= $_FW;
863			$this->structure= 'structure'; // tells Mind what is the structure directory to be based on
864			$this->project = $project;
865			$this->knowledge= $project->knowledge;
866		}
867	}
868?>