PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/framework/zii/widgets/CDetailView.php

https://github.com/ashie1287/headfirst
PHP | 230 lines | 92 code | 15 blank | 123 comment | 22 complexity | 7d6594fb709a250ddde81d51a9b47e9d MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, LGPL-2.1
  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-2010 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 2326 2010-08-20 17:02:07Z 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 above "value" element is specified, this will be ignored.</li>
  75. * <li>value: the value to be displayed. If this is not specified, the following "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. * </ul>
  85. */
  86. public $attributes;
  87. /**
  88. * @var string the text to be displayed when an attribute value is null. Defaults to "Not set".
  89. */
  90. public $nullDisplay;
  91. /**
  92. * @var string the name of the tag for rendering the detail view. Defaults to 'table'.
  93. * @see itemTemplate
  94. */
  95. public $tagName='table';
  96. /**
  97. * @var string the template used to render a single attribute. Defaults to a table row.
  98. * These tokens are recognized: "{class}", "{label}" and "{value}". They will be replaced
  99. * with the CSS class name for the item, the label and the attribute value, respectively.
  100. * @see itemCssClass
  101. */
  102. public $itemTemplate="<tr class=\"{class}\"><th>{label}</th><td>{value}</td></tr>\n";
  103. /**
  104. * @var array the CSS class names for the items displaying attribute values. If multiple CSS class names are given,
  105. * they will be assigned to the items sequentially and repeatedly.
  106. * Defaults to <code>array('odd', 'even')</code>.
  107. */
  108. public $itemCssClass=array('odd','even');
  109. /**
  110. * @var array the HTML options used for {@link tagName}
  111. */
  112. public $htmlOptions=array('class'=>'detail-view');
  113. /**
  114. * @var string the base script URL for all detail view resources (e.g. javascript, CSS file, images).
  115. * Defaults to null, meaning using the integrated detail view resources (which are published as assets).
  116. */
  117. public $baseScriptUrl;
  118. /**
  119. * @var string the URL of the CSS file used by this detail view. Defaults to null, meaning using the integrated
  120. * CSS file. If this is set false, you are responsible to explicitly include the necessary CSS file in your page.
  121. */
  122. public $cssFile;
  123. /**
  124. * Initializes the detail view.
  125. * This method will initialize required property values.
  126. */
  127. public function init()
  128. {
  129. if($this->data===null)
  130. throw new CException(Yii::t('zii','Please specify the "data" property.'));
  131. if($this->attributes===null)
  132. {
  133. if($this->data instanceof CModel)
  134. $this->attributes=$this->data->attributeNames();
  135. else if(is_array($this->data))
  136. $this->attributes=array_keys($this->data);
  137. else
  138. throw new CException(Yii::t('zii','Please specify the "attributes" property.'));
  139. }
  140. if($this->nullDisplay===null)
  141. $this->nullDisplay='<span class="null">'.Yii::t('zii','Not set').'</span>';
  142. $this->htmlOptions['id']=$this->getId();
  143. if($this->baseScriptUrl===null)
  144. $this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/detailview';
  145. if($this->cssFile!==false)
  146. {
  147. if($this->cssFile===null)
  148. $this->cssFile=$this->baseScriptUrl.'/styles.css';
  149. Yii::app()->getClientScript()->registerCssFile($this->cssFile);
  150. }
  151. }
  152. /**
  153. * Renders the detail view.
  154. * This is the main entry of the whole detail view rendering.
  155. */
  156. public function run()
  157. {
  158. $formatter=$this->getFormatter();
  159. echo CHtml::openTag($this->tagName,$this->htmlOptions);
  160. $n=is_array($this->itemCssClass) ? count($this->itemCssClass) : 0;
  161. foreach($this->attributes as $i=>$attribute)
  162. {
  163. if(is_string($attribute))
  164. {
  165. if(!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/',$attribute,$matches))
  166. throw new CException(Yii::t('zii','The attribute must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
  167. $attribute=array(
  168. 'name'=>$matches[1],
  169. 'type'=>isset($matches[3]) ? $matches[3] : 'text',
  170. );
  171. if(isset($matches[5]))
  172. $attribute['label']=$matches[5];
  173. }
  174. $tr=array('{label}'=>'', '{class}'=>$n ? $this->itemCssClass[$i%$n] : '');
  175. if(isset($attribute['cssClass']))
  176. $tr['{class}']=$attribute['cssClass'].' '.($n ? $tr['{class}'] : '');
  177. if(isset($attribute['label']))
  178. $tr['{label}']=$attribute['label'];
  179. else if(isset($attribute['name']))
  180. {
  181. if($this->data instanceof CModel)
  182. $tr['{label}']=$this->data->getAttributeLabel($attribute['name']);
  183. else
  184. $tr['{label}']=ucwords(trim(strtolower(str_replace(array('-','_','.'),' ',preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $attribute['name'])))));
  185. }
  186. if(!isset($attribute['type']))
  187. $attribute['type']='text';
  188. if(isset($attribute['value']))
  189. $value=$attribute['value'];
  190. else if(isset($attribute['name']))
  191. $value=CHtml::value($this->data,$attribute['name']);
  192. else
  193. $value=null;
  194. $tr['{value}']=$value===null ? $this->nullDisplay : $formatter->format($value,$attribute['type']);
  195. echo strtr(isset($attribute['template']) ? $attribute['template'] : $this->itemTemplate,$tr);
  196. }
  197. echo CHtml::closeTag($this->tagName);
  198. }
  199. /**
  200. * @return CFormatter the formatter instance. Defaults to the 'format' application component.
  201. */
  202. public function getFormatter()
  203. {
  204. if($this->_formatter===null)
  205. $this->_formatter=Yii::app()->format;
  206. return $this->_formatter;
  207. }
  208. /**
  209. * @param CFormatter the formatter instance
  210. */
  211. public function setFormatter($value)
  212. {
  213. $this->_formatter=$value;
  214. }
  215. }