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