PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/www/app/AdminModule/components/Datagrid/DataGridAction.php

https://github.com/bazo/Mokuji
PHP | 104 lines | 43 code | 23 blank | 38 comment | 7 complexity | 3eb2ff4223ec41621e9248bd6fbaa146 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. require_once dirname(__FILE__) . '/IDataGridAction.php';
  3. /**
  4. * Representation of data grid action.
  5. *
  6. * @author Roman Sklenář
  7. * @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
  8. * @license New BSD License
  9. * @example http://nettephp.com/extras/datagrid
  10. * @package Nette\Extras\DataGrid
  11. * @version $Id$
  12. */
  13. class DataGridAction extends Component implements IDataGridAction
  14. {
  15. /**#@+ special action key */
  16. const WITH_KEY = TRUE;
  17. const WITHOUT_KEY = FALSE;
  18. /**#@-*/
  19. /** @var Html action element template */
  20. protected $html;
  21. /** @var string */
  22. static public $ajaxClass = 'datagrid-ajax';
  23. /** @var string */
  24. public $destination;
  25. /** @var bool|string */
  26. public $key;
  27. /**
  28. * Data grid action constructor.
  29. * @note for full ajax support, destination should not change module,
  30. * @note presenter or action and must be ended with exclamation mark (!)
  31. *
  32. * @param string textual title
  33. * @param string textual link destination
  34. * @param Html element which is added to a generated link
  35. * @param bool use ajax? (add class self::$ajaxClass into generated link)
  36. * @param mixed generate link with argument? (if yes you can specify name of parameter
  37. * otherwise variable DataGrid::$keyName will be used and must be defined)
  38. * @return void
  39. */
  40. public function __construct($title, $destination, Html $icon = NULL, $useAjax = FALSE, $key = self::WITH_KEY)
  41. {
  42. parent::__construct();
  43. $this->destination = $destination;
  44. $this->key = $key;
  45. $a = Html::el('a')->title($title);
  46. if ($useAjax) $a->addClass(self::$ajaxClass);
  47. if ($icon !== NULL && $icon instanceof Html) {
  48. $a->add($icon);
  49. } else {
  50. $a->setText($title);
  51. }
  52. $this->html = $a;
  53. }
  54. /**
  55. * Generates action's link. (use before data grid is going to be rendered)
  56. * @return void
  57. */
  58. public function generateLink(array $args = NULL)
  59. {
  60. $dataGrid = $this->lookup('DataGrid', TRUE);
  61. $control = $dataGrid->lookup('Nette\Application\Control', TRUE);
  62. switch ($this->key) {
  63. case self::WITHOUT_KEY:
  64. $link = $control->link($this->destination); break;
  65. case self::WITH_KEY:
  66. default:
  67. $key = $this->key == NULL || is_bool($this->key) ? $dataGrid->keyName : $this->key;
  68. $link = $control->link($this->destination, array($key => $args[$dataGrid->keyName])); break;
  69. }
  70. $this->html->href($link);
  71. }
  72. /********************* interface \IDataGridAction *********************/
  73. /**
  74. * Gets action element template.
  75. * @return Html
  76. */
  77. public function getHtml()
  78. {
  79. return $this->html;
  80. }
  81. }