PageRenderTime 130ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/zii/widgets/grid/CButtonColumn.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 323 lines | 149 code | 17 blank | 157 comment | 24 complexity | 9ed43d2fcaf136a31c03441eb8eaed40 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * CButtonColumn class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('zii.widgets.grid.CGridColumn');
  11. /**
  12. * CButtonColumn represents a grid view column that renders one or several buttons.
  13. *
  14. * By default, it will display three buttons, "view", "update" and "delete", which triggers the corresponding
  15. * actions on the model of the row.
  16. *
  17. * By configuring {@link buttons} and {@link template} properties, the column can display other buttons
  18. * and customize the display order of the buttons.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @version $Id: CButtonColumn.php 3272 2011-06-14 19:41:37Z mdomba $
  22. * @package zii.widgets.grid
  23. * @since 1.1
  24. */
  25. class CButtonColumn extends CGridColumn
  26. {
  27. /**
  28. * @var array the HTML options for the data cell tags.
  29. */
  30. public $htmlOptions=array('class'=>'button-column');
  31. /**
  32. * @var array the HTML options for the header cell tag.
  33. */
  34. public $headerHtmlOptions=array('class'=>'button-column');
  35. /**
  36. * @var array the HTML options for the footer cell tag.
  37. */
  38. public $footerHtmlOptions=array('class'=>'button-column');
  39. /**
  40. * @var string the template that is used to render the content in each data cell.
  41. * These default tokens are recognized: {view}, {update} and {delete}. If the {@link buttons} property
  42. * defines additional buttons, their IDs are also recognized here. For example, if a button named 'preview'
  43. * is declared in {@link buttons}, we can use the token '{preview}' here to specify where to display the button.
  44. */
  45. public $template='{view} {update} {delete}';
  46. /**
  47. * @var string the label for the view button. Defaults to "View".
  48. * Note that the label will not be HTML-encoded when rendering.
  49. */
  50. public $viewButtonLabel;
  51. /**
  52. * @var string the image URL for the view button. If not set, an integrated image will be used.
  53. * You may set this property to be false to render a text link instead.
  54. */
  55. public $viewButtonImageUrl;
  56. /**
  57. * @var string a PHP expression that is evaluated for every view button and whose result is used
  58. * as the URL for the view button. In this expression, the variable
  59. * <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
  60. * and <code>$this</code> the column object.
  61. */
  62. public $viewButtonUrl='Yii::app()->controller->createUrl("view",array("id"=>$data->primaryKey))';
  63. /**
  64. * @var array the HTML options for the view button tag.
  65. */
  66. public $viewButtonOptions=array('class'=>'view');
  67. /**
  68. * @var string the label for the update button. Defaults to "Update".
  69. * Note that the label will not be HTML-encoded when rendering.
  70. */
  71. public $updateButtonLabel;
  72. /**
  73. * @var string the image URL for the update button. If not set, an integrated image will be used.
  74. * You may set this property to be false to render a text link instead.
  75. */
  76. public $updateButtonImageUrl;
  77. /**
  78. * @var string a PHP expression that is evaluated for every update button and whose result is used
  79. * as the URL for the update button. In this expression, the variable
  80. * <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
  81. * and <code>$this</code> the column object.
  82. */
  83. public $updateButtonUrl='Yii::app()->controller->createUrl("update",array("id"=>$data->primaryKey))';
  84. /**
  85. * @var array the HTML options for the update button tag.
  86. */
  87. public $updateButtonOptions=array('class'=>'update');
  88. /**
  89. * @var string the label for the delete button. Defaults to "Delete".
  90. * Note that the label will not be HTML-encoded when rendering.
  91. */
  92. public $deleteButtonLabel;
  93. /**
  94. * @var string the image URL for the delete button. If not set, an integrated image will be used.
  95. * You may set this property to be false to render a text link instead.
  96. */
  97. public $deleteButtonImageUrl;
  98. /**
  99. * @var string a PHP expression that is evaluated for every delete button and whose result is used
  100. * as the URL for the delete button. In this expression, the variable
  101. * <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
  102. * and <code>$this</code> the column object.
  103. */
  104. public $deleteButtonUrl='Yii::app()->controller->createUrl("delete",array("id"=>$data->primaryKey))';
  105. /**
  106. * @var array the HTML options for the view button tag.
  107. */
  108. public $deleteButtonOptions=array('class'=>'delete');
  109. /**
  110. * @var string the confirmation message to be displayed when delete button is clicked.
  111. * By setting this property to be false, no confirmation message will be displayed.
  112. * This property is used only if <code>$this->buttons['delete']['click']</code> is not set.
  113. */
  114. public $deleteConfirmation;
  115. /**
  116. * @var string a javascript function that will be invoked after the delete ajax call.
  117. * This property is used only if <code>$this->buttons['delete']['click']</code> is not set.
  118. *
  119. * The function signature is <code>function(link, success, data)</code>
  120. * <ul>
  121. * <li><code>link</code> references the delete link.</li>
  122. * <li><code>success</code> status of the ajax call, true if the ajax call was successful, false if the ajax call failed.
  123. * <li><code>data</code> the data returned by the server in case of a successful call or XHR object in case of error.
  124. * </ul>
  125. * Note that if success is true it does not mean that the delete was successful, it only means that the ajax call was successful.
  126. *
  127. * Example:
  128. * <pre>
  129. * array(
  130. * class'=>'CButtonColumn',
  131. * 'afterDelete'=>'function(link,success,data){ if(success) alert("Delete completed successfuly"); }',
  132. * ),
  133. * </pre>
  134. */
  135. public $afterDelete;
  136. /**
  137. * @var array the configuration for additional buttons. Each array element specifies a single button
  138. * which has the following format:
  139. * <pre>
  140. * 'buttonID' => array(
  141. * 'label'=>'...', // text label of the button
  142. * 'url'=>'...', // a PHP expression for generating the URL of the button
  143. * 'imageUrl'=>'...', // image URL of the button. If not set or false, a text link is used
  144. * 'options'=>array(...), // HTML options for the button tag
  145. * 'click'=>'...', // a JS function to be invoked when the button is clicked
  146. * 'visible'=>'...', // a PHP expression for determining whether the button is visible
  147. * )
  148. * </pre>
  149. * In the PHP expression for the 'url' option and/or 'visible' option, the variable <code>$row</code>
  150. * refers to the current row number (zero-based), and <code>$data</code> refers to the data model for
  151. * the row.
  152. *
  153. * Note that in order to display these additional buttons, the {@link template} property needs to
  154. * be configured so that the corresponding button IDs appear as tokens in the template.
  155. */
  156. public $buttons=array();
  157. /**
  158. * Initializes the column.
  159. * This method registers necessary client script for the button column.
  160. */
  161. public function init()
  162. {
  163. $this->initDefaultButtons();
  164. foreach($this->buttons as $id=>$button)
  165. {
  166. if(strpos($this->template,'{'.$id.'}')===false)
  167. unset($this->buttons[$id]);
  168. else if(isset($button['click']))
  169. {
  170. if(!isset($button['options']['class']))
  171. $this->buttons[$id]['options']['class']=$id;
  172. if(strpos($button['click'],'js:')!==0)
  173. $this->buttons[$id]['click']='js:'.$button['click'];
  174. }
  175. }
  176. $this->registerClientScript();
  177. }
  178. /**
  179. * Initializes the default buttons (view, update and delete).
  180. */
  181. protected function initDefaultButtons()
  182. {
  183. if($this->viewButtonLabel===null)
  184. $this->viewButtonLabel=Yii::t('zii','View');
  185. if($this->updateButtonLabel===null)
  186. $this->updateButtonLabel=Yii::t('zii','Update');
  187. if($this->deleteButtonLabel===null)
  188. $this->deleteButtonLabel=Yii::t('zii','Delete');
  189. if($this->viewButtonImageUrl===null)
  190. $this->viewButtonImageUrl=$this->grid->baseScriptUrl.'/view.png';
  191. if($this->updateButtonImageUrl===null)
  192. $this->updateButtonImageUrl=$this->grid->baseScriptUrl.'/update.png';
  193. if($this->deleteButtonImageUrl===null)
  194. $this->deleteButtonImageUrl=$this->grid->baseScriptUrl.'/delete.png';
  195. if($this->deleteConfirmation===null)
  196. $this->deleteConfirmation=Yii::t('zii','Are you sure you want to delete this item?');
  197. foreach(array('view','update','delete') as $id)
  198. {
  199. $button=array(
  200. 'label'=>$this->{$id.'ButtonLabel'},
  201. 'url'=>$this->{$id.'ButtonUrl'},
  202. 'imageUrl'=>$this->{$id.'ButtonImageUrl'},
  203. 'options'=>$this->{$id.'ButtonOptions'},
  204. );
  205. if(isset($this->buttons[$id]))
  206. $this->buttons[$id]=array_merge($button,$this->buttons[$id]);
  207. else
  208. $this->buttons[$id]=$button;
  209. }
  210. if(!isset($this->buttons['delete']['click']))
  211. {
  212. if(is_string($this->deleteConfirmation))
  213. $confirmation="if(!confirm(".CJavaScript::encode($this->deleteConfirmation).")) return false;";
  214. else
  215. $confirmation='';
  216. if(Yii::app()->request->enableCsrfValidation)
  217. {
  218. $csrfTokenName = Yii::app()->request->csrfTokenName;
  219. $csrfToken = Yii::app()->request->csrfToken;
  220. $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
  221. }
  222. else
  223. $csrf = '';
  224. if($this->afterDelete===null)
  225. $this->afterDelete='function(){}';
  226. $this->buttons['delete']['click']=<<<EOD
  227. function() {
  228. $confirmation
  229. var th=this;
  230. var afterDelete=$this->afterDelete;
  231. $.fn.yiiGridView.update('{$this->grid->id}', {
  232. type:'POST',
  233. url:$(this).attr('href'),$csrf
  234. success:function(data) {
  235. $.fn.yiiGridView.update('{$this->grid->id}');
  236. afterDelete(th,true,data);
  237. },
  238. error:function(XHR) {
  239. return afterDelete(th,false,XHR);
  240. }
  241. });
  242. return false;
  243. }
  244. EOD;
  245. }
  246. }
  247. /**
  248. * Registers the client scripts for the button column.
  249. */
  250. protected function registerClientScript()
  251. {
  252. $js=array();
  253. foreach($this->buttons as $id=>$button)
  254. {
  255. if(isset($button['click']))
  256. {
  257. $function=CJavaScript::encode($button['click']);
  258. $class=preg_replace('/\s+/','.',$button['options']['class']);
  259. $js[]="jQuery('#{$this->grid->id} a.{$class}').live('click',$function);";
  260. }
  261. }
  262. if($js!==array())
  263. Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id, implode("\n",$js));
  264. }
  265. /**
  266. * Renders the data cell content.
  267. * This method renders the view, update and delete buttons in the data cell.
  268. * @param integer $row the row number (zero-based)
  269. * @param mixed $data the data associated with the row
  270. */
  271. protected function renderDataCellContent($row,$data)
  272. {
  273. $tr=array();
  274. ob_start();
  275. foreach($this->buttons as $id=>$button)
  276. {
  277. $this->renderButton($id,$button,$row,$data);
  278. $tr['{'.$id.'}']=ob_get_contents();
  279. ob_clean();
  280. }
  281. ob_end_clean();
  282. echo strtr($this->template,$tr);
  283. }
  284. /**
  285. * Renders a link button.
  286. * @param string $id the ID of the button
  287. * @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
  288. * See {@link buttons} for more details.
  289. * @param integer $row the row number (zero-based)
  290. * @param mixed $data the data object associated with the row
  291. */
  292. protected function renderButton($id,$button,$row,$data)
  293. {
  294. if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
  295. return;
  296. $label=isset($button['label']) ? $button['label'] : $id;
  297. $url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
  298. $options=isset($button['options']) ? $button['options'] : array();
  299. if(!isset($options['title']))
  300. $options['title']=$label;
  301. if(isset($button['imageUrl']) && is_string($button['imageUrl']))
  302. echo CHtml::link(CHtml::image($button['imageUrl'],$label),$url,$options);
  303. else
  304. echo CHtml::link($label,$url,$options);
  305. }
  306. }