CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

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

Generated using CakePHP API Docs