sfAdminGenerator.class.php 25 KB

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