sfAdminGenerator.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Admin generator.
  11. *
  12. * This class generates an admin module.
  13. *
  14. * This class calls two ORM specific methods:
  15. * getAllColumns()
  16. * and
  17. * getAdminColumnForField($field, $flag = null)
  18. *
  19. * @package symfony
  20. * @subpackage generator
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. * @version SVN: $Id: sfAdminGenerator.class.php 13060 2008-11-17 11:52:11Z fabien $
  23. */
  24. abstract class sfAdminGenerator extends sfCrudGenerator
  25. {
  26. protected
  27. $formObject = null,
  28. $fields = array();
  29. /**
  30. * Returns HTML code for a help icon.
  31. *
  32. * @param string $column The column name
  33. * @param string $type The field type (list, edit)
  34. *
  35. * @return string HTML code
  36. */
  37. public function getHelpAsIcon($column, $type = '')
  38. {
  39. $help = $this->getParameterValue($type.'.fields.'.$column->getName().'.help');
  40. if ($help)
  41. {
  42. return "[?php echo image_tag(sfConfig::get('sf_admin_web_dir').'/images/help.png', array('align' => 'absmiddle', 'alt' => __('".$this->escapeString($help)."'), 'title' => __('".$this->escapeString($help)."'))) ?]";
  43. }
  44. return '';
  45. }
  46. /**
  47. * Returns HTML code for a help text.
  48. *
  49. * @param string $column The column name
  50. * @param string $type The field type (list, edit)
  51. *
  52. * @return string HTML code
  53. */
  54. public function getHelp($column, $type = '')
  55. {
  56. $help = $this->getParameterValue($type.'.fields.'.$column->getName().'.help');
  57. if ($help)
  58. {
  59. return "<div class=\"sf_admin_edit_help\">[?php echo __('".$this->escapeString($help)."') ?]</div>";
  60. }
  61. return '';
  62. }
  63. /**
  64. * Returns HTML code for an action button.
  65. *
  66. * @param string $actionName The action name
  67. * @param array $params The parameters
  68. * @param boolean $pk_link Whether to add a primary key link or not
  69. *
  70. * @return string HTML code
  71. */
  72. public function getButtonToAction($actionName, $params, $pk_link = false)
  73. {
  74. $params = (array) $params;
  75. $options = isset($params['params']) ? sfToolkit::stringToArray($params['params']) : array();
  76. $method = 'button_to';
  77. $li_class = '';
  78. $only_for = isset($params['only_for']) ? $params['only_for'] : null;
  79. // default values
  80. if ($actionName[0] == '_')
  81. {
  82. $actionName = substr($actionName, 1);
  83. $default_name = strtr($actionName, '_', ' ');
  84. $default_icon = sfConfig::get('sf_admin_web_dir').'/images/'.$actionName.'_icon.png';
  85. $default_action = $actionName;
  86. $default_class = 'sf_admin_action_'.$actionName;
  87. if ($actionName == 'save' || $actionName == 'save_and_add' || $actionName == 'save_and_list')
  88. {
  89. $method = 'submit_tag';
  90. $options['name'] = $actionName;
  91. }
  92. if ($actionName == 'delete')
  93. {
  94. $options['post'] = true;
  95. if (!isset($options['confirm']))
  96. {
  97. $options['confirm'] = 'Are you sure?';
  98. }
  99. $li_class = 'float-left';
  100. $only_for = 'edit';
  101. }
  102. }
  103. else
  104. {
  105. $default_name = strtr($actionName, '_', ' ');
  106. $default_icon = sfConfig::get('sf_admin_web_dir').'/images/default_icon.png';
  107. $default_action = 'List'.sfInflector::camelize($actionName);
  108. $default_class = '';
  109. }
  110. $name = isset($params['name']) ? $params['name'] : $default_name;
  111. $icon = isset($params['icon']) ? sfToolkit::replaceConstants($params['icon']) : $default_icon;
  112. $action = isset($params['action']) ? $params['action'] : $default_action;
  113. $url_params = $pk_link ? '?'.$this->getPrimaryKeyUrlParams() : '\'';
  114. if (!isset($options['class']))
  115. {
  116. if ($default_class)
  117. {
  118. $options['class'] = $default_class;
  119. }
  120. else
  121. {
  122. $options['style'] = 'background: #ffc url('.$icon.') no-repeat 3px 2px';
  123. }
  124. }
  125. $li_class = $li_class ? ' class="'.$li_class.'"' : '';
  126. $html = '<li'.$li_class.'>';
  127. if ($only_for == 'edit')
  128. {
  129. $html .= '[?php if ('.$this->getPrimaryKeyIsSet().'): ?]'."\n";
  130. }
  131. else if ($only_for == 'create')
  132. {
  133. $html .= '[?php if (!'.$this->getPrimaryKeyIsSet().'): ?]'."\n";
  134. }
  135. else if ($only_for !== null)
  136. {
  137. throw new sfConfigurationException(sprintf('The "only_for" parameter can only takes "create" or "edit" as argument ("%s").', $only_for));
  138. }
  139. if ($method == 'submit_tag')
  140. {
  141. $html .= '[?php echo submit_tag(__(\''.$name.'\'), '.var_export($options, true).') ?]';
  142. }
  143. else
  144. {
  145. $phpOptions = var_export($options, true);
  146. // little hack
  147. $phpOptions = preg_replace("/'confirm' => '(.+?)(?<!\\\)'/", '\'confirm\' => __(\'$1\')', $phpOptions);
  148. $html .= '[?php echo button_to(__(\''.$name.'\'), \''.$this->getModuleName().'/'.$action.$url_params.', '.$phpOptions.') ?]';
  149. }
  150. if ($only_for !== null)
  151. {
  152. $html .= '[?php endif; ?]'."\n";
  153. }
  154. $html .= '</li>'."\n";
  155. return $html;
  156. }
  157. /**
  158. * Returns HTML code for an action link.
  159. *
  160. * @param string $actionName The action name
  161. * @param array $params The parameters
  162. * @param boolean $pk_link Whether to add a primary key link or not
  163. *
  164. * @return string HTML code
  165. */
  166. public function getLinkToAction($actionName, $params, $pk_link = false)
  167. {
  168. $options = isset($params['params']) ? sfToolkit::stringToArray($params['params']) : array();
  169. // default values
  170. if ($actionName[0] == '_')
  171. {
  172. $actionName = substr($actionName, 1);
  173. $name = $actionName;
  174. $icon = sfConfig::get('sf_admin_web_dir').'/images/'.$actionName.'_icon.png';
  175. $action = $actionName;
  176. if ($actionName == 'delete')
  177. {
  178. $options['post'] = true;
  179. if (!isset($options['confirm']))
  180. {
  181. $options['confirm'] = 'Are you sure?';
  182. }
  183. }
  184. }
  185. else
  186. {
  187. $name = isset($params['name']) ? $params['name'] : $actionName;
  188. $icon = isset($params['icon']) ? sfToolkit::replaceConstants($params['icon']) : sfConfig::get('sf_admin_web_dir').'/images/default_icon.png';
  189. $action = isset($params['action']) ? $params['action'] : 'List'.sfInflector::camelize($actionName);
  190. }
  191. $url_params = $pk_link ? '?'.$this->getPrimaryKeyUrlParams() : '\'';
  192. $phpOptions = var_export($options, true);
  193. // little hack
  194. $phpOptions = preg_replace("/'confirm' => '(.+?)(?<!\\\)'/", '\'confirm\' => __(\'$1\')', $phpOptions);
  195. return '<li>[?php echo link_to(image_tag(\''.$icon.'\', array(\'alt\' => __(\''.$name.'\'), \'title\' => __(\''.$name.'\'))), \''.$this->getModuleName().'/'.$action.$url_params.($options ? ', '.$phpOptions : '').') ?]</li>'."\n";
  196. }
  197. /**
  198. * Returns HTML code for an action option in a select tag.
  199. *
  200. * @param string $actionName The action name
  201. * @param array $params The parameters
  202. *
  203. * @return string HTML code
  204. */
  205. public function getOptionToAction($actionName, $params)
  206. {
  207. $options = isset($params['params']) ? sfToolkit::stringToArray($params['params']) : array();
  208. // default values
  209. if ($actionName[0] == '_')
  210. {
  211. $actionName = substr($actionName, 1);
  212. if ($actionName == 'deleteSelected')
  213. {
  214. $params['name'] = 'Delete Selected';
  215. }
  216. }
  217. $name = isset($params['name']) ? $params['name'] : $actionName;
  218. $options['value'] = $actionName;
  219. $phpOptions = var_export($options, true);
  220. return '[?php echo content_tag(\'option\', __(\''.$name.'\')'.($options ? ', '.$phpOptions : '').') ?]';
  221. }
  222. /**
  223. * Returns HTML code for a column in edit mode.
  224. *
  225. * @param string $column The column name
  226. * @param array $params The parameters
  227. *
  228. * @return string HTML code
  229. */
  230. public function getColumnEditTag($column, $params = array())
  231. {
  232. // user defined parameters
  233. $user_params = $this->getParameterValue('edit.fields.'.$column->getName().'.params');
  234. $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
  235. $params = $user_params ? array_merge($params, $user_params) : $params;
  236. if ($column->isComponent())
  237. {
  238. return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
  239. }
  240. else if ($column->isPartial())
  241. {
  242. return "get_partial('".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
  243. }
  244. // default control name
  245. $params = array_merge(array('control_name' => $this->getSingularName().'['.$column->getName().']'), $params);
  246. // default parameter values
  247. $type = $column->getType();
  248. if ($type == PropelColumnTypes::DATE)
  249. {
  250. $params = array_merge(array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params);
  251. }
  252. else if ($type == PropelColumnTypes::TIMESTAMP)
  253. {
  254. $params = array_merge(array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params);
  255. }
  256. // user sets a specific tag to use
  257. if ($inputType = $this->getParameterValue('edit.fields.'.$column->getName().'.type'))
  258. {
  259. if ($inputType == 'plain')
  260. {
  261. return $this->getColumnListTag($column, $params);
  262. }
  263. else
  264. {
  265. return $this->getPHPObjectHelper($inputType, $column, $params);
  266. }
  267. }
  268. // guess the best tag to use with column type
  269. return parent::getCrudColumnEditTag($column, $params);
  270. }
  271. /**
  272. * Returns all column categories.
  273. *
  274. * @param string $paramName The parameter name
  275. *
  276. * @return array The column categories
  277. */
  278. public function getColumnCategories($paramName)
  279. {
  280. if (is_array($this->getParameterValue($paramName)))
  281. {
  282. $fields = $this->getParameterValue($paramName);
  283. // do we have categories?
  284. if (!isset($fields[0]))
  285. {
  286. return array_keys($fields);
  287. }
  288. }
  289. return array('NONE');
  290. }
  291. /**
  292. * Wraps content with a credential condition.
  293. *
  294. * @param string $content The content
  295. * @param array $params The parameters
  296. *
  297. * @return string HTML code
  298. */
  299. public function addCredentialCondition($content, $params = array())
  300. {
  301. if (isset($params['credentials']))
  302. {
  303. $credentials = str_replace("\n", ' ', var_export($params['credentials'], true));
  304. return <<<EOF
  305. [?php if (\$sf_user->hasCredential($credentials)): ?]
  306. $content
  307. [?php endif; ?]
  308. EOF;
  309. }
  310. else
  311. {
  312. return $content;
  313. }
  314. }
  315. /**
  316. * Gets sfAdminColumn objects for a given category.
  317. *
  318. * @param string $paramName The parameter name
  319. * @param string $category The category
  320. *
  321. * @return array sfAdminColumn array
  322. */
  323. public function getColumns($paramName, $category = 'NONE')
  324. {
  325. $phpNames = array();
  326. // user has set a personnalized list of fields?
  327. $fields = $this->getParameterValue($paramName);
  328. if (is_array($fields))
  329. {
  330. // categories?
  331. if (isset($fields[0]))
  332. {
  333. // simulate a default one
  334. $fields = array('NONE' => $fields);
  335. }
  336. if (!$fields)
  337. {
  338. return array();
  339. }
  340. foreach ($fields[$category] as $field)
  341. {
  342. list($field, $flags) = $this->splitFlag($field);
  343. $phpNames[] = $this->getAdminColumnForField($field, $flags);
  344. }
  345. }
  346. else
  347. {
  348. // no, just return the full list of columns in table
  349. return $this->getAllColumns();
  350. }
  351. return $phpNames;
  352. }
  353. /**
  354. * Gets modifier flags from a column name.
  355. *
  356. * @param string $text The column name
  357. *
  358. * @return array An array of detected flags
  359. */
  360. public function splitFlag($text)
  361. {
  362. $flags = array();
  363. while (in_array($text[0], array('=', '-', '+', '_', '~')))
  364. {
  365. $flags[] = $text[0];
  366. $text = substr($text, 1);
  367. }
  368. return array($text, $flags);
  369. }
  370. /**
  371. * Gets a parameter value.
  372. *
  373. * @param string $key The key name
  374. * @param mixed $default The default value
  375. *
  376. * @return mixed The parameter value
  377. */
  378. public function getParameterValue($key, $default = null)
  379. {
  380. if (preg_match('/^([^\.]+)\.fields\.(.+)$/', $key, $matches))
  381. {
  382. return $this->getFieldParameterValue($matches[2], $matches[1], $default);
  383. }
  384. else
  385. {
  386. return $this->getValueFromKey($key, $default);
  387. }
  388. }
  389. /**
  390. * Gets a field parameter value.
  391. *
  392. * @param string $key The key name
  393. * @param string $type The type (list, edit)
  394. * @param mixed $default The default value
  395. *
  396. * @return mixed The parameter value
  397. */
  398. protected function getFieldParameterValue($key, $type = '', $default = null)
  399. {
  400. $retval = $this->getValueFromKey($type.'.fields.'.$key, $default);
  401. if ($retval !== null)
  402. {
  403. return $retval;
  404. }
  405. $retval = $this->getValueFromKey('fields.'.$key, $default);
  406. if ($retval !== null)
  407. {
  408. return $retval;
  409. }
  410. if (preg_match('/\.name$/', $key))
  411. {
  412. // default field.name
  413. return sfInflector::humanize(($pos = strpos($key, '.')) ? substr($key, 0, $pos) : $key);
  414. }
  415. else
  416. {
  417. return null;
  418. }
  419. }
  420. /**
  421. * Gets the value for a given key.
  422. *
  423. * @param string $key The key name
  424. * @param mixed $default The default value
  425. *
  426. * @return mixed The key value
  427. */
  428. protected function getValueFromKey($key, $default = null)
  429. {
  430. $ref =& $this->params;
  431. $parts = explode('.', $key);
  432. $count = count($parts);
  433. for ($i = 0; $i < $count; $i++)
  434. {
  435. $partKey = $parts[$i];
  436. if (!isset($ref[$partKey]))
  437. {
  438. return $default;
  439. }
  440. if ($count == $i + 1)
  441. {
  442. return $ref[$partKey];
  443. }
  444. else
  445. {
  446. $ref =& $ref[$partKey];
  447. }
  448. }
  449. return $default;
  450. }
  451. /**
  452. * Wraps a content for I18N.
  453. *
  454. * @param string $key The key name
  455. * @param string $default The defaul value
  456. * @param bool $withEcho If true, string is wrapped in php echo
  457. *
  458. * @return string HTML code
  459. */
  460. public function getI18NString($key, $default = null, $withEcho = true)
  461. {
  462. $value = $this->escapeString($this->getParameterValue($key, $default));
  463. // find %%xx%% strings
  464. preg_match_all('/%%([^%]+)%%/', $value, $matches, PREG_PATTERN_ORDER);
  465. $this->params['tmp']['display'] = array();
  466. foreach ($matches[1] as $name)
  467. {
  468. $this->params['tmp']['display'][] = $name;
  469. }
  470. $vars = array();
  471. foreach ($this->getColumns('tmp.display') as $column)
  472. {
  473. if ($column->isLink())
  474. {
  475. $vars[] = '\'%%'.$column->getName().'%%\' => link_to('.$this->getColumnListTag($column).', \''.$this->getModuleName().'/edit?'.$this->getPrimaryKeyUrlParams().')';
  476. }
  477. elseif ($column->isPartial())
  478. {
  479. $vars[] = '\'%%_'.$column->getName().'%%\' => '.$this->getColumnListTag($column);
  480. }
  481. else if ($column->isComponent())
  482. {
  483. $vars[] = '\'%%~'.$column->getName().'%%\' => '.$this->getColumnListTag($column);
  484. }
  485. else
  486. {
  487. $vars[] = '\'%%'.$column->getName().'%%\' => '.$this->getColumnListTag($column);
  488. }
  489. }
  490. // strip all = signs
  491. $value = preg_replace('/%%=([^%]+)%%/', '%%$1%%', $value);
  492. $i18n = '__(\''.$value.'\', '."\n".'array('.implode(",\n", $vars).'))';
  493. return $withEcho ? '[?php echo '.$i18n.' ?]' : $i18n;
  494. }
  495. /**
  496. * Replaces constants in a string.
  497. *
  498. * @param string $value
  499. *
  500. * @return string
  501. */
  502. public function replaceConstants($value)
  503. {
  504. // find %%xx%% strings
  505. preg_match_all('/%%([^%]+)%%/', $value, $matches, PREG_PATTERN_ORDER);
  506. $this->params['tmp']['display'] = array();
  507. foreach ($matches[1] as $name)
  508. {
  509. $this->params['tmp']['display'][] = $name;
  510. }
  511. foreach ($this->getColumns('tmp.display') as $column)
  512. {
  513. $value = str_replace('%%'.$column->getName().'%%', '{'.$this->getColumnGetter($column, true, 'this->').'}', $value);
  514. }
  515. return $value;
  516. }
  517. /**
  518. * Returns HTML code for a column in list mode.
  519. *
  520. * @param string $column The column name
  521. * @param array $params The parameters
  522. *
  523. * @return string HTML code
  524. */
  525. public function getColumnListTag($column, $params = array())
  526. {
  527. $user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params');
  528. $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
  529. $params = $user_params ? array_merge($params, $user_params) : $params;
  530. $type = $column->getType();
  531. $columnGetter = $this->getColumnGetter($column, true);
  532. if ($column->isComponent())
  533. {
  534. return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
  535. }
  536. else if ($column->isPartial())
  537. {
  538. return "get_partial('".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
  539. }
  540. else if ($type == PropelColumnTypes::DATE || $type == PropelColumnTypes::TIMESTAMP)
  541. {
  542. $format = isset($params['date_format']) ? $params['date_format'] : ($type == PropelColumnTypes::DATE ? 'D' : 'f');
  543. return "($columnGetter !== null && $columnGetter !== '') ? format_date($columnGetter, \"$format\") : ''";
  544. }
  545. elseif ($type == PropelColumnTypes::BOOLEAN)
  546. {
  547. return "$columnGetter ? image_tag(sfConfig::get('sf_admin_web_dir').'/images/tick.png') : '&nbsp;'";
  548. }
  549. else
  550. {
  551. return "$columnGetter";
  552. }
  553. }
  554. /**
  555. * Returns HTML code for a column in filter mode.
  556. *
  557. * @param string $column The column name
  558. * @param array $params The parameters
  559. *
  560. * @return string HTML code
  561. */
  562. public function getColumnFilterTag($column, $params = array())
  563. {
  564. $user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params');
  565. $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
  566. $params = $user_params ? array_merge($params, $user_params) : $params;
  567. if ($column->isComponent())
  568. {
  569. return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'filter'))";
  570. }
  571. else if ($column->isPartial())
  572. {
  573. return "get_partial('".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))";
  574. }
  575. $type = $column->getType();
  576. $default_value = "isset(\$filters['".$column->getName()."']) ? \$filters['".$column->getName()."'] : null";
  577. $unquotedName = 'filters['.$column->getName().']';
  578. $name = "'$unquotedName'";
  579. if ($column->isForeignKey())
  580. {
  581. $params = $this->getObjectTagParams($params, array('include_blank' => true, 'related_class'=>$this->getRelatedClassName($column), 'text_method'=>'__toString', 'control_name'=>$unquotedName));
  582. return "object_select_tag($default_value, null, $params)";
  583. }
  584. else if ($type == PropelColumnTypes::DATE)
  585. {
  586. // rich=false not yet implemented
  587. $params = $this->getObjectTagParams($params, array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
  588. return "input_date_range_tag($name, $default_value, $params)";
  589. }
  590. else if ($type == PropelColumnTypes::TIMESTAMP)
  591. {
  592. // rich=false not yet implemented
  593. $params = $this->getObjectTagParams($params, array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
  594. return "input_date_range_tag($name, $default_value, $params)";
  595. }
  596. else if ($type == PropelColumnTypes::BOOLEAN)
  597. {
  598. $defaultIncludeCustom = '__("yes or no")';
  599. $option_params = $this->getObjectTagParams($params, array('include_custom' => $defaultIncludeCustom));
  600. $params = $this->getObjectTagParams($params);
  601. // little hack
  602. $option_params = preg_replace("/'".preg_quote($defaultIncludeCustom)."'/", $defaultIncludeCustom, $option_params);
  603. $options = "options_for_select(array(1 => __('yes'), 0 => __('no')), $default_value, $option_params)";
  604. return "select_tag($name, $options, $params)";
  605. }
  606. else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR || $type == PropelColumnTypes::LONGVARCHAR)
  607. {
  608. $size = ($column->getSize() < 15 ? $column->getSize() : 15);
  609. $params = $this->getObjectTagParams($params, array('size' => $size));
  610. return "input_tag($name, $default_value, $params)";
  611. }
  612. else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT)
  613. {
  614. $params = $this->getObjectTagParams($params, array('size' => 7));
  615. return "input_tag($name, $default_value, $params)";
  616. }
  617. else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL)
  618. {
  619. $params = $this->getObjectTagParams($params, array('size' => 7));
  620. return "input_tag($name, $default_value, $params)";
  621. }
  622. else
  623. {
  624. $params = $this->getObjectTagParams($params, array('disabled' => true));
  625. return "input_tag($name, $default_value, $params)";
  626. }
  627. }
  628. /**
  629. * Gets the form object
  630. *
  631. * @return sfForm
  632. */
  633. public function getFormObject()
  634. {
  635. if (is_null($this->formObject))
  636. {
  637. $class = $this->getFormClassName();
  638. $this->formObject = new $class();
  639. }
  640. return $this->formObject;
  641. }
  642. /**
  643. * Gets the form class name
  644. *
  645. * @return string The form class name associated with this generator
  646. */
  647. public function getFormClassName()
  648. {
  649. return isset($this->params['form_class']) ? $this->params['form_class'] : $this->getClassName().'Form';
  650. }
  651. /**
  652. * Retrieves all hidden fields in the widget schema
  653. *
  654. * @return array
  655. */
  656. public function getHiddenFields()
  657. {
  658. $form = $this->getFormObject();
  659. $hiddenFields = array();
  660. foreach ($form->getWidgetSchema()->getPositions() as $name)
  661. {
  662. if ($form[$name]->isHidden())
  663. {
  664. $hiddenFields[] = $name;
  665. }
  666. }
  667. return $hiddenFields;
  668. }
  669. /**
  670. * Gets the hidden fields as a string
  671. *
  672. * @return array
  673. */
  674. public function getHiddenFieldsAsString()
  675. {
  676. $hiddenFields = '';
  677. foreach ($this->getHiddenFields() as $name)
  678. {
  679. $hiddenFields .= ' [?php echo $form[\''.$name.'\'] ?]'."\n";
  680. }
  681. return "\n".$hiddenFields;
  682. }
  683. public function getLastNonHiddenField()
  684. {
  685. $form = $this->getFormObject();
  686. $positions = $form->getWidgetSchema()->getPositions();
  687. $last = count($positions) - 1;
  688. for ($i = count($positions) - 1; $i >= 0; $i--)
  689. {
  690. if ($form[$positions[$i]]->isHidden())
  691. {
  692. $last = $i - 1;
  693. }
  694. else
  695. {
  696. break;
  697. }
  698. }
  699. return $last;
  700. }
  701. /**
  702. * Gets the HTML to add to the form tag if the form is multipart.
  703. *
  704. * @return string
  705. */
  706. public function getFormMultipartHtml()
  707. {
  708. if (isset($this->params['non_verbose_templates']) && $this->params['non_verbose_templates'])
  709. {
  710. return '[?php $form->isMultipart() and print \' enctype="multipart/form-data"\' ?]';
  711. }
  712. else
  713. {
  714. return $this->getFormObject()->isMultipart() ? ' enctype="multipart/form-data"' : '';
  715. }
  716. }
  717. /**
  718. * Escapes a string.
  719. *
  720. * @param string $string
  721. */
  722. protected function escapeString($string)
  723. {
  724. return preg_replace('/\'/', '\\\'', $string);
  725. }
  726. }
  727. /**
  728. * Admin generator column.
  729. *
  730. * @package symfony
  731. * @subpackage generator
  732. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  733. * @version SVN: $Id: sfAdminGenerator.class.php 13060 2008-11-17 11:52:11Z fabien $
  734. */
  735. class sfAdminColumn
  736. {
  737. protected
  738. $phpName = '',
  739. $column = null,
  740. $flags = array();
  741. /**
  742. * Constructor.
  743. *
  744. * @param string $phpName The column php name
  745. * @param string $column The column name
  746. * @param array $flags The column flags
  747. */
  748. public function __construct($phpName, $column = null, $flags = array())
  749. {
  750. $this->phpName = $phpName;
  751. $this->column = $column;
  752. $this->flags = (array) $flags;
  753. }
  754. /**
  755. * Returns true if the column maps a database column.
  756. *
  757. * @return boolean true if the column maps a database column, false otherwise
  758. */
  759. public function isReal()
  760. {
  761. return $this->column ? true : false;
  762. }
  763. /**
  764. * Gets the name of the column.
  765. *
  766. * @return string The column name
  767. */
  768. public function getName()
  769. {
  770. return sfInflector::underscore($this->phpName);
  771. }
  772. /**
  773. * Returns true if the column is a partial.
  774. *
  775. * @return boolean true if the column is a partial, false otherwise
  776. */
  777. public function isPartial()
  778. {
  779. return in_array('_', $this->flags) ? true : false;
  780. }
  781. /**
  782. * Returns true if the column is a component.
  783. *
  784. * @return boolean true if the column is a component, false otherwise
  785. */
  786. public function isComponent()
  787. {
  788. return in_array('~', $this->flags) ? true : false;
  789. }
  790. /**
  791. * Returns true if the column has a link.
  792. *
  793. * @return boolean true if the column has a link, false otherwise
  794. */
  795. public function isLink()
  796. {
  797. return (in_array('=', $this->flags) || $this->isPrimaryKey()) ? true : false;
  798. }
  799. /**
  800. * Gets the php name of the column.
  801. *
  802. * @return string The php name
  803. */
  804. public function getPhpName()
  805. {
  806. return $this->phpName;
  807. }
  808. // FIXME: those methods are only used in the propel admin generator
  809. public function __call($name, $arguments)
  810. {
  811. return $this->column ? $this->column->$name() : null;
  812. }
  813. }