/cake/console/libs/tasks/view.php

https://github.com/hardsshah/bookmarks · PHP · 389 lines · 258 code · 16 blank · 115 comment · 33 complexity · 80e1d053f690d1ad644a60ff410df83e MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * The View Tasks handles creating and updating view files.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19. * @package cake
  20. * @subpackage cake.cake.console.libs.tasks
  21. * @since CakePHP(tm) v 1.2
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. App::import('Core', 'Controller');
  28. /**
  29. * Task class for creating and updating view files.
  30. *
  31. * @package cake
  32. * @subpackage cake.cake.console.libs.tasks
  33. */
  34. class ViewTask extends Shell {
  35. /**
  36. * Name of plugin
  37. *
  38. * @var string
  39. * @access public
  40. */
  41. var $plugin = null;
  42. /**
  43. * Tasks to be loaded by this Task
  44. *
  45. * @var array
  46. * @access public
  47. */
  48. var $tasks = array('Project', 'Controller');
  49. /**
  50. * path to VIEWS directory
  51. *
  52. * @var array
  53. * @access public
  54. */
  55. var $path = VIEWS;
  56. /**
  57. * Name of the controller being used
  58. *
  59. * @var string
  60. * @access public
  61. */
  62. var $controllerName = null;
  63. /**
  64. * Path to controller to put views
  65. *
  66. * @var string
  67. * @access public
  68. */
  69. var $controllerPath = null;
  70. /**
  71. * The template file to use
  72. *
  73. * @var string
  74. * @access public
  75. */
  76. var $template = null;
  77. /**
  78. * Actions to use for scaffolding
  79. *
  80. * @var array
  81. * @access public
  82. */
  83. var $scaffoldActions = array('index', 'view', 'add', 'edit');
  84. /**
  85. * Override initialize
  86. *
  87. * @access public
  88. */
  89. function initialize() {
  90. }
  91. /**
  92. * Execution method always used for tasks
  93. *
  94. * @access public
  95. */
  96. function execute() {
  97. if (empty($this->args)) {
  98. $this->__interactive();
  99. }
  100. if (isset($this->args[0])) {
  101. $controller = $action = $alias = null;
  102. $this->controllerName = Inflector::camelize($this->args[0]);
  103. $this->controllerPath = Inflector::underscore($this->controllerName);
  104. if (isset($this->args[1])) {
  105. $this->template = $this->args[1];
  106. }
  107. if (isset($this->args[2])) {
  108. $action = $this->args[2];
  109. }
  110. if (!$action) {
  111. $action = $this->template;
  112. }
  113. if (in_array($action, $this->scaffoldActions)) {
  114. $this->bake($action, true);
  115. } elseif ($action) {
  116. $this->bake($action, true);
  117. } else {
  118. $vars = $this->__loadController();
  119. if ($vars) {
  120. $methods = array_diff(
  121. array_map('strtolower', get_class_methods($this->controllerName . 'Controller')),
  122. array_map('strtolower', get_class_methods('appcontroller'))
  123. );
  124. if (empty($methods)) {
  125. $methods = $this->scaffoldActions;
  126. }
  127. $adminDelete = null;
  128. $adminRoute = Configure::read('Routing.admin');
  129. if (!empty($adminRoute)) {
  130. $adminDelete = $adminRoute.'_delete';
  131. }
  132. foreach ($methods as $method) {
  133. if ($method{0} != '_' && !in_array($method, array('delete', $adminDelete))) {
  134. $content = $this->getContent($method, $vars);
  135. $this->bake($method, $content);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Handles interactive baking
  144. *
  145. * @access private
  146. */
  147. function __interactive() {
  148. $this->hr();
  149. $this->out(sprintf("Bake View\nPath: %s", $this->path));
  150. $this->hr();
  151. $wannaDoAdmin = 'n';
  152. $wannaDoScaffold = 'y';
  153. $this->interactive = false;
  154. $this->controllerName = $this->Controller->getName();
  155. $this->controllerPath = low(Inflector::underscore($this->controllerName));
  156. $interactive = $this->in("Would you like bake to build your views interactively?\nWarning: Choosing no will overwrite {$this->controllerName} views if it exist.", array('y','n'), 'y');
  157. if (low($interactive) == 'y' || low($interactive) == 'yes') {
  158. $this->interactive = true;
  159. $wannaDoScaffold = $this->in("Would you like to create some scaffolded views (index, add, view, edit) for this controller?\nNOTE: Before doing so, you'll need to create your controller and model classes (including associated models).", array('y','n'), 'n');
  160. }
  161. if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') {
  162. $wannaDoAdmin = $this->in("Would you like to create the views for admin routing?", array('y','n'), 'y');
  163. }
  164. $admin = false;
  165. if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) {
  166. $admin = $this->getAdmin();
  167. }
  168. if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') {
  169. $actions = $this->scaffoldActions;
  170. if ($admin) {
  171. foreach ($actions as $action) {
  172. $actions[] = $admin . $action;
  173. }
  174. }
  175. $vars = $this->__loadController();
  176. if ($vars) {
  177. foreach ($actions as $action) {
  178. $content = $this->getContent($action, $vars);
  179. $this->bake($action, $content);
  180. }
  181. }
  182. $this->hr();
  183. $this->out('');
  184. $this->out('View Scaffolding Complete.'."\n");
  185. } else {
  186. $action = '';
  187. while ($action == '') {
  188. $action = $this->in('Action Name? (use camelCased function name)');
  189. if ($action == '') {
  190. $this->out('The action name you supplied was empty. Please try again.');
  191. }
  192. }
  193. $this->out('');
  194. $this->hr();
  195. $this->out('The following view will be created:');
  196. $this->hr();
  197. $this->out("Controller Name: {$this->controllerName}");
  198. $this->out("Action Name: {$action}");
  199. $this->out("Path: ".$this->params['app'] . DS . $this->controllerPath . DS . Inflector::underscore($action) . ".ctp");
  200. $this->hr();
  201. $looksGood = $this->in('Look okay?', array('y','n'), 'y');
  202. if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
  203. $this->bake($action);
  204. $this->_stop();
  205. } else {
  206. $this->out('Bake Aborted.');
  207. }
  208. }
  209. }
  210. /**
  211. * Loads Controller and sets variables for the template
  212. * Available template variables
  213. * 'modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
  214. * 'singularHumanName', 'pluralHumanName', 'fields', 'foreignKeys',
  215. * 'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'
  216. *
  217. * @return array Returns an variables to be made available to a view template
  218. * @access private
  219. */
  220. function __loadController() {
  221. if (!$this->controllerName) {
  222. $this->err(__('Controller not found', true));
  223. }
  224. $import = $this->controllerName;
  225. if ($this->plugin) {
  226. $import = $this->plugin . '.' . $this->controllerName;
  227. }
  228. if (!App::import('Controller', $import)) {
  229. $file = $this->controllerPath . '_controller.php';
  230. $this->err(sprintf(__("The file '%s' could not be found.\nIn order to bake a view, you'll need to first create the controller.", true), $file));
  231. $this->_stop();
  232. }
  233. $controllerClassName = $this->controllerName . 'Controller';
  234. $controllerObj = & new $controllerClassName();
  235. $controllerObj->constructClasses();
  236. $modelClass = $controllerObj->modelClass;
  237. $modelObj =& ClassRegistry::getObject($controllerObj->modelKey);
  238. if ($modelObj) {
  239. $primaryKey = $modelObj->primaryKey;
  240. $displayField = $modelObj->displayField;
  241. $singularVar = Inflector::variable($modelClass);
  242. $pluralVar = Inflector::variable($this->controllerName);
  243. $singularHumanName = Inflector::humanize($modelClass);
  244. $pluralHumanName = Inflector::humanize($this->controllerName);
  245. $schema = $modelObj->schema();
  246. $fields = array_keys($schema);
  247. $associations = $this->__associations($modelObj);
  248. } else {
  249. $primaryKey = null;
  250. $displayField = null;
  251. $singularVar = Inflector::variable(Inflector::singularize($this->controllerName));
  252. $pluralVar = Inflector::variable($this->controllerName);
  253. $singularHumanName = Inflector::humanize(Inflector::singularize($this->controllerName));
  254. $pluralHumanName = Inflector::humanize($this->controllerName);
  255. $fields = array();
  256. $schema = array();
  257. $associations = array();
  258. }
  259. return compact('modelClass', 'schema', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
  260. 'singularHumanName', 'pluralHumanName', 'fields','associations');
  261. }
  262. /**
  263. * Assembles and writes bakes the view file.
  264. *
  265. * @param string $action Action to bake
  266. * @param string $content Content to write
  267. * @return boolean Success
  268. * @access public
  269. */
  270. function bake($action, $content = '') {
  271. if ($content === true) {
  272. $content = $this->getContent();
  273. }
  274. $filename = $this->path . $this->controllerPath . DS . Inflector::underscore($action) . '.ctp';
  275. $Folder =& new Folder($this->path . $this->controllerPath, true);
  276. $errors = $Folder->errors();
  277. if (empty($errors)) {
  278. $path = $Folder->slashTerm($Folder->pwd());
  279. return $this->createFile($filename, $content);
  280. } else {
  281. foreach ($errors as $error) {
  282. $this->err($error);
  283. }
  284. }
  285. return false;
  286. }
  287. /**
  288. * Builds content from template and variables
  289. *
  290. * @param string $template file to use
  291. * @param array $vars passed for use in templates
  292. * @return string content from template
  293. * @access public
  294. */
  295. function getContent($template = null, $vars = null) {
  296. if (!$template) {
  297. $template = $this->template;
  298. }
  299. $action = $template;
  300. $adminRoute = Configure::read('Routing.admin');
  301. if (!empty($adminRoute) && strpos($template, $adminRoute) !== false) {
  302. $template = str_replace($adminRoute.'_', '', $template);
  303. }
  304. if (in_array($template, array('add', 'edit'))) {
  305. $action = $template;
  306. $template = 'form';
  307. }
  308. $loaded = false;
  309. foreach ($this->Dispatch->shellPaths as $path) {
  310. $templatePath = $path . 'templates' . DS . 'views' . DS .Inflector::underscore($template).'.ctp';
  311. if (file_exists($templatePath) && is_file($templatePath)) {
  312. $loaded = true;
  313. break;
  314. }
  315. }
  316. if (!$vars) {
  317. $vars = $this->__loadController();
  318. }
  319. if ($loaded) {
  320. extract($vars);
  321. ob_start();
  322. ob_implicit_flush(0);
  323. include($templatePath);
  324. $content = ob_get_clean();
  325. return $content;
  326. }
  327. $this->hr();
  328. $this->err(sprintf(__('Template for %s could not be found', true), $template));
  329. return false;
  330. }
  331. /**
  332. * Displays help contents
  333. *
  334. * @access public
  335. */
  336. function help() {
  337. $this->hr();
  338. $this->out("Usage: cake bake view <arg1> <arg2>...");
  339. $this->hr();
  340. $this->out('Commands:');
  341. $this->out("\n\tview <controller>\n\t\twill read the given controller for methods\n\t\tand bake corresponding views.\n\t\tIf var scaffold is found it will bake the scaffolded actions\n\t\t(index,view,add,edit)");
  342. $this->out("\n\tview <controller> <action>\n\t\twill bake a template. core templates: (index, add, edit, view)");
  343. $this->out("\n\tview <controller> <template> <alias>\n\t\twill use the template specified but name the file based on the alias");
  344. $this->out("");
  345. $this->_stop();
  346. }
  347. /**
  348. * Returns associations for controllers models.
  349. *
  350. * @return array $associations
  351. * @access private
  352. */
  353. function __associations($model) {
  354. $keys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
  355. $associations = array();
  356. foreach ($keys as $key => $type) {
  357. foreach ($model->{$type} as $assocKey => $assocData) {
  358. $associations[$type][$assocKey]['primaryKey'] = $model->{$assocKey}->primaryKey;
  359. $associations[$type][$assocKey]['displayField'] = $model->{$assocKey}->displayField;
  360. $associations[$type][$assocKey]['foreignKey'] = $assocData['foreignKey'];
  361. $associations[$type][$assocKey]['controller'] = Inflector::pluralize(Inflector::underscore($assocData['className']));
  362. $associations[$type][$assocKey]['fields'] = array_keys($model->{$assocKey}->schema());
  363. }
  364. }
  365. return $associations;
  366. }
  367. }
  368. ?>