PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/zii/widgets/CDetailView.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 240 lines | 96 code | 19 blank | 125 comment | 24 complexity | bf9106691fa66330ed0308cd83c2fac3 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. * CDetailView 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. /**
  11. * CDetailView displays the detail of a single data model.
  12. *
  13. * CDetailView is best used for displaying a model in a regular format (e.g. each model attribute
  14. * is displayed as a row in a table.) The model can be either an instance of {@link CModel}
  15. * or an associative array.
  16. *
  17. * CDetailView uses the {@link attributes} property to determines which model attributes
  18. * should be displayed and how they should be formatted.
  19. *
  20. * A typical usage of CDetailView is as follows:
  21. * <pre>
  22. * $this->widget('zii.widgets.CDetailView', array(
  23. * 'data'=>$model,
  24. * 'attributes'=>array(
  25. * 'title', // title attribute (in plain text)
  26. * 'owner.name', // an attribute of the related object "owner"
  27. * 'description:html', // description attribute in HTML
  28. * array( // related city displayed as a link
  29. * 'label'=>'City',
  30. * 'type'=>'raw',
  31. * 'value'=>CHtml::link(CHtml::encode($model->city->name),
  32. * array('city/view','id'=>$model->city->id)),
  33. * ),
  34. * ),
  35. * ));
  36. * </pre>
  37. *
  38. * @author Qiang Xue <qiang.xue@gmail.com>
  39. * @version $Id: CDetailView.php 2799 2011-01-01 19:31:13Z qiang.xue $
  40. * @package zii.widgets
  41. * @since 1.1
  42. */
  43. class CDetailView extends CWidget
  44. {
  45. private $_formatter;
  46. /**
  47. * @var mixed the data model whose details are to be displayed. This can be either a {@link CModel} instance
  48. * (e.g. a {@link CActiveRecord} object or a {@link CFormModel} object) or an associative array.
  49. */
  50. public $data;
  51. /**
  52. * @var array a list of attributes to be displayed in the detail view. Each array element
  53. * represents the specification for displaying one particular attribute.
  54. *
  55. * An attribute can be specified as a string in the format of "Name:Type:Label".
  56. * Both "Type" and "Label" are optional.
  57. *
  58. * "Name" refers to the attribute name. It can be either a property (e.g. "title") or a sub-property (e.g. "owner.username").
  59. *
  60. * "Label" represents the label for the attribute display. If it is not given, "Name" will be used to generate the appropriate label.
  61. *
  62. * "Type" represents the type of the attribute. It determines how the attribute value should be formatted and displayed.
  63. * It is defaulted to be 'text'.
  64. * "Type" should be recognizable by the {@link formatter}. In particular, if "Type" is "xyz", then the "formatXyz" method
  65. * of {@link formatter} will be invoked to format the attribute value for display. By default when {@link CFormatter} is used,
  66. * these "Type" values are valid: raw, text, ntext, html, date, time, datetime, boolean, number, email, image, url.
  67. * For more details about these types, please refer to {@link CFormatter}.
  68. *
  69. * An attribute can also be specified in terms of an array with the following elements:
  70. * <ul>
  71. * <li>label: the label associated with the attribute. If this is not specified, the following "name" element
  72. * will be used to generate an appropriate label.</li>
  73. * <li>name: the name of the attribute. This can be either a property or a sub-property of the model.
  74. * If the below "value" element is specified, this will be ignored.</li>
  75. * <li>value: the value to be displayed. If this is not specified, the above "name" element will be used
  76. * to retrieve the corresponding attribute value for display. Note that this value will be formatted according
  77. * to the "type" option as described below.</li>
  78. * <li>type: the type of the attribute that determines how the attribute value would be formatted.
  79. * Please see above for possible values.
  80. * <li>cssClass: the CSS class to be used for this item. This option is available since version 1.1.3.</li>
  81. * <li>template: the template used to render the attribute. If this is not specified, {@link itemTemplate}
  82. * will be used instead. For more details on how to set this option, please refer to {@link itemTemplate}.
  83. * This option is available since version 1.1.1.</li>
  84. * <li>visible: whether the attribute is visible. If set to <code>false</code>, the table row for the attribute will not be rendered.
  85. * This option is available since version 1.1.5.</li>
  86. * </ul>
  87. */
  88. public $attributes;
  89. /**
  90. * @var string the text to be displayed when an attribute value is null. Defaults to "Not set".
  91. */
  92. public $nullDisplay;
  93. /**
  94. * @var string the name of the tag for rendering the detail view. Defaults to 'table'.
  95. * @see itemTemplate
  96. */
  97. public $tagName='table';
  98. /**
  99. * @var string the template used to render a single attribute. Defaults to a table row.
  100. * These tokens are recognized: "{class}", "{label}" and "{value}". They will be replaced
  101. * with the CSS class name for the item, the label and the attribute value, respectively.
  102. * @see itemCssClass
  103. */
  104. public $itemTemplate="<tr class=\"{class}\"><th>{label}</th><td>{value}</td></tr>\n";
  105. /**
  106. * @var array the CSS class names for the items displaying attribute values. If multiple CSS class names are given,
  107. * they will be assigned to the items sequentially and repeatedly.
  108. * Defaults to <code>array('odd', 'even')</code>.
  109. */
  110. public $itemCssClass=array('odd','even');
  111. /**
  112. * @var array the HTML options used for {@link tagName}
  113. */
  114. public $htmlOptions=array('class'=>'detail-view');
  115. /**
  116. * @var string the base script URL for all detail view resources (e.g. javascript, CSS file, images).
  117. * Defaults to null, meaning using the integrated detail view resources (which are published as assets).
  118. */
  119. public $baseScriptUrl;
  120. /**
  121. * @var string the URL of the CSS file used by this detail view. Defaults to null, meaning using the integrated
  122. * CSS file. If this is set false, you are responsible to explicitly include the necessary CSS file in your page.
  123. */
  124. public $cssFile;
  125. /**
  126. * Initializes the detail view.
  127. * This method will initialize required property values.
  128. */
  129. public function init()
  130. {
  131. if($this->data===null)
  132. throw new CException(Yii::t('zii','Please specify the "data" property.'));
  133. if($this->attributes===null)
  134. {
  135. if($this->data instanceof CModel)
  136. $this->attributes=$this->data->attributeNames();
  137. else if(is_array($this->data))
  138. $this->attributes=array_keys($this->data);
  139. else
  140. throw new CException(Yii::t('zii','Please specify the "attributes" property.'));
  141. }
  142. if($this->nullDisplay===null)
  143. $this->nullDisplay='<span class="null">'.Yii::t('zii','Not set').'</span>';
  144. $this->htmlOptions['id']=$this->getId();
  145. if($this->baseScriptUrl===null)
  146. $this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/detailview';
  147. if($this->cssFile!==false)
  148. {
  149. if($this->cssFile===null)
  150. $this->cssFile=$this->baseScriptUrl.'/styles.css';
  151. Yii::app()->getClientScript()->registerCssFile($this->cssFile);
  152. }
  153. }
  154. /**
  155. * Renders the detail view.
  156. * This is the main entry of the whole detail view rendering.
  157. */
  158. public function run()
  159. {
  160. $formatter=$this->getFormatter();
  161. echo CHtml::openTag($this->tagName,$this->htmlOptions);
  162. $i=0;
  163. $n=is_array($this->itemCssClass) ? count($this->itemCssClass) : 0;
  164. foreach($this->attributes as $attribute)
  165. {
  166. if(is_string($attribute))
  167. {
  168. if(!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/',$attribute,$matches))
  169. throw new CException(Yii::t('zii','The attribute must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
  170. $attribute=array(
  171. 'name'=>$matches[1],
  172. 'type'=>isset($matches[3]) ? $matches[3] : 'text',
  173. );
  174. if(isset($matches[5]))
  175. $attribute['label']=$matches[5];
  176. }
  177. if(isset($attribute['visible']) && !$attribute['visible'])
  178. continue;
  179. $tr=array('{label}'=>'', '{class}'=>$n ? $this->itemCssClass[$i%$n] : '');
  180. if(isset($attribute['cssClass']))
  181. $tr['{class}']=$attribute['cssClass'].' '.($n ? $tr['{class}'] : '');
  182. if(isset($attribute['label']))
  183. $tr['{label}']=$attribute['label'];
  184. else if(isset($attribute['name']))
  185. {
  186. if($this->data instanceof CModel)
  187. $tr['{label}']=$this->data->getAttributeLabel($attribute['name']);
  188. else
  189. $tr['{label}']=ucwords(trim(strtolower(str_replace(array('-','_','.'),' ',preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $attribute['name'])))));
  190. }
  191. if(!isset($attribute['type']))
  192. $attribute['type']='text';
  193. if(isset($attribute['value']))
  194. $value=$attribute['value'];
  195. else if(isset($attribute['name']))
  196. $value=CHtml::value($this->data,$attribute['name']);
  197. else
  198. $value=null;
  199. $tr['{value}']=$value===null ? $this->nullDisplay : $formatter->format($value,$attribute['type']);
  200. echo strtr(isset($attribute['template']) ? $attribute['template'] : $this->itemTemplate,$tr);
  201. $i++;
  202. }
  203. echo CHtml::closeTag($this->tagName);
  204. }
  205. /**
  206. * @return CFormatter the formatter instance. Defaults to the 'format' application component.
  207. */
  208. public function getFormatter()
  209. {
  210. if($this->_formatter===null)
  211. $this->_formatter=Yii::app()->format;
  212. return $this->_formatter;
  213. }
  214. /**
  215. * @param CFormatter $value the formatter instance
  216. */
  217. public function setFormatter($value)
  218. {
  219. $this->_formatter=$value;
  220. }
  221. }