1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class TemplateTask extends Shell {
21:
22: 23: 24: 25: 26:
27: var $templateVars = array();
28:
29: 30: 31: 32: 33: 34:
35: var $templatePaths = array();
36:
37: 38: 39: 40: 41: 42:
43: function initialize() {
44: $this->templatePaths = $this->_findThemes();
45: }
46:
47: 48: 49: 50: 51: 52: 53:
54: function _findThemes() {
55: $paths = App::path('shells');
56: $core = array_pop($paths);
57: $separator = DS === '/' ? '/' : '\\\\';
58: $core = preg_replace('#libs' . $separator . '$#', '', $core);
59:
60: $Folder =& new Folder($core . 'templates' . DS . 'default');
61: $contents = $Folder->read();
62: $themeFolders = $contents[0];
63:
64: $plugins = App::objects('plugin');
65: foreach ($plugins as $plugin) {
66: $paths[] = $this->_pluginPath($plugin) . 'vendors' . DS . 'shells' . DS;
67: }
68: $paths[] = $core;
69:
70:
71: foreach ($paths as $i => $path) {
72: $paths[$i] = rtrim($path, DS) . DS;
73: }
74:
75: $themes = array();
76: foreach ($paths as $path) {
77: $Folder =& new Folder($path . 'templates', false);
78: $contents = $Folder->read();
79: $subDirs = $contents[0];
80: foreach ($subDirs as $dir) {
81: if (empty($dir) || preg_match('@^skel$|_skel$@', $dir)) {
82: continue;
83: }
84: $Folder =& new Folder($path . 'templates' . DS . $dir);
85: $contents = $Folder->read();
86: $subDirs = $contents[0];
87: if (array_intersect($contents[0], $themeFolders)) {
88: $templateDir = $path . 'templates' . DS . $dir . DS;
89: $themes[$dir] = $templateDir;
90: }
91: }
92: }
93: return $themes;
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103:
104: function set($one, $two = null) {
105: $data = null;
106: if (is_array($one)) {
107: if (is_array($two)) {
108: $data = array_combine($one, $two);
109: } else {
110: $data = $one;
111: }
112: } else {
113: $data = array($one => $two);
114: }
115:
116: if ($data == null) {
117: return false;
118: }
119: $this->templateVars = $data + $this->templateVars;
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129: 130:
131: function generate($directory, $filename, $vars = null) {
132: if ($vars !== null) {
133: $this->set($vars);
134: }
135: if (empty($this->templatePaths)) {
136: $this->initialize();
137: }
138: $themePath = $this->getThemePath();
139: $templateFile = $this->_findTemplate($themePath, $directory, $filename);
140: if ($templateFile) {
141: extract($this->templateVars);
142: ob_start();
143: ob_implicit_flush(0);
144: include($templateFile);
145: $content = ob_get_clean();
146: return $content;
147: }
148: return '';
149: }
150:
151: 152: 153: 154: 155: 156: 157: 158:
159: function getThemePath() {
160: if (count($this->templatePaths) == 1) {
161: $paths = array_values($this->templatePaths);
162: return $paths[0];
163: }
164: if (!empty($this->params['theme']) && isset($this->templatePaths[$this->params['theme']])) {
165: return $this->templatePaths[$this->params['theme']];
166: }
167:
168: $this->hr();
169: $this->out(__('You have more than one set of templates installed.', true));
170: $this->out(__('Please choose the template set you wish to use:', true));
171: $this->hr();
172:
173: $i = 1;
174: $indexedPaths = array();
175: foreach ($this->templatePaths as $key => $path) {
176: $this->out($i . '. ' . $key);
177: $indexedPaths[$i] = $path;
178: $i++;
179: }
180: $index = $this->in(__('Which bake theme would you like to use?', true), range(1, $i - 1), 1);
181: $themeNames = array_keys($this->templatePaths);
182: $this->Dispatch->params['theme'] = $themeNames[$index - 1];
183: return $indexedPaths[$index];
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195:
196: function _findTemplate($path, $directory, $filename) {
197: $themeFile = $path . $directory . DS . $filename . '.ctp';
198: if (file_exists($themeFile)) {
199: return $themeFile;
200: }
201: foreach ($this->templatePaths as $path) {
202: $templatePath = $path . $directory . DS . $filename . '.ctp';
203: if (file_exists($templatePath)) {
204: return $templatePath;
205: }
206: }
207: $this->err(sprintf(__('Could not find template for %s', true), $filename));
208: return false;
209: }
210: }
211: