PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/page/widgets/PageItem.php

https://bitbucket.org/rohitrox/hotc
PHP | 140 lines | 124 code | 12 blank | 4 comment | 16 complexity | cc413be76d8ef7e9b3ad1cf8c1497bd9 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. class PageItem extends CWidget {
  3. public $data;
  4. public $fields = array();
  5. private $page;
  6. public function init() {
  7. //check for required arguments
  8. if (!$this->data)
  9. throw new CHttpException('500', '$data must be provided for PageItem Widget');
  10. //users do not tend to use array for single item
  11. if (!is_array($this->fields)) {
  12. $tmp = array();
  13. $tmp[0] = $this->fields;
  14. $this->fields = $tmp;
  15. }
  16. //get Page
  17. if (get_class($this->data) == 'Page')
  18. $this->page = $this->data;
  19. else
  20. $this->page = $this->data->page; //for update
  21. }
  22. public function run() {
  23. $page = $this->page;
  24. ?>
  25. <?php
  26. foreach ($this->fields as $key => $field) {
  27. //linkify the first field
  28. if ($key == 0) {
  29. ?>
  30. <h2><?php echo CHtml::link(CHtml::encode($page->$field), array('view', 'id' => $this->data->id)); ?></h2>
  31. <?php
  32. continue;
  33. }
  34. switch ($field) {
  35. case 'title':
  36. ?>
  37. <span itemprop="headline"><?php echo $page->title; ?></span>
  38. <?php
  39. break;
  40. case 'content':
  41. echo '<div class="rte-text" itemprop="articleBody">' . $page->content . "</div>";
  42. break;
  43. case 'created_at':
  44. echo '<div class="post-time">' . Yii::t('app', 'Posted on ') . '<span itemprop="datePublished">' . date('F d, Y h:m A', strtotime($page->created_at)) . "</span></div>";
  45. break;
  46. case 'excerpt':
  47. if (!empty($page->content)) {
  48. ?>
  49. <div class="field">
  50. <div class="field_value">
  51. <?php echo nl2br($page->getExcerpt()); ?>
  52. </div>
  53. </div>
  54. <?php
  55. }
  56. break;
  57. case 'sub-pages':
  58. if (count($page->pages)) {
  59. ?>
  60. <h2><?php echo CHtml::link(Yii::t('app', Awecms::pluralize('Sub-Page', 'Sub-Pages', count($page->pages))), array('/page/page')); ?></h2>
  61. <ul class="sub_pages">
  62. <?php
  63. if (is_array($page->pages))
  64. foreach ($page->pages as $foreignobj) {
  65. echo '<li>';
  66. echo CHtml::link($foreignobj->title, array('/page/page/view', 'id' => $foreignobj->id));
  67. }
  68. ?>
  69. </ul>
  70. <?php
  71. }
  72. break;
  73. case 'categories':
  74. if (Yii::app()->hasModule('category') && count($page->categories)) {
  75. ?>
  76. <h2><?php echo CHtml::link(Yii::t('app', Awecms::pluralize('Category', 'Categories', count($page->categories))), array('/category/category')); ?></h2>
  77. <ul class="categories">
  78. <?php
  79. if (is_array($page->categories))
  80. foreach ($page->categories as $foreignobj) {
  81. echo '<li>
  82. <a href="' . Yii::app()->createUrl('/category/category/view', array('id' => $foreignobj->id)) . '">
  83. <span itemprop="articleSection">' . $foreignobj->name . '</span>
  84. </a>
  85. </li>';
  86. }
  87. ?>
  88. </ul>
  89. <?php
  90. }
  91. break;
  92. case 'views':
  93. if (!empty($page->views)) {
  94. ?>
  95. <span class="field">
  96. <span class="field_name">
  97. <?php echo CHtml::encode($page->getAttributeLabel('views')); ?>:
  98. </pan>
  99. <span class="field_value">
  100. <?php
  101. echo CHtml::encode($page->views);
  102. ?>
  103. </span>
  104. </span>
  105. <?php
  106. }
  107. break;
  108. case 'tags':
  109. if (Yii::app()->hasModule('tag')) {
  110. $tags = $page->getTags();
  111. if (!empty($tags)) {
  112. ?>
  113. <div class="field">
  114. <?php echo Yii::t('app', 'Tags'); ?>:
  115. <?php
  116. echo '<span class="tags" itemprop="keywords">' . implode(', ', $tags) . '</span>';
  117. ?>
  118. </div>
  119. <?php
  120. }
  121. }
  122. break;
  123. default :
  124. break;
  125. }
  126. }
  127. }
  128. }