PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/modules/blog/actions/index.php

http://github.com/forkcms/forkcms
PHP | 266 lines | 118 code | 50 blank | 98 comment | 19 complexity | 0e084bb1b763622828aa9fb4c3e85936 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of Fork CMS.
  4. *
  5. * For the full copyright and license information, please view the license
  6. * file that was distributed with this source code.
  7. */
  8. /**
  9. * This is the index-action (default), it will display the overview of blog posts
  10. *
  11. * @author Davy Hellemans <davy.hellemans@netlash.com>
  12. * @author Dave Lens <dave.lens@netlash.com>
  13. * @author Tijs Verkoyen <tijs@sumocoders.com>
  14. * @author Matthias Mullie <matthias@mullie.eu>
  15. */
  16. class BackendBlogIndex extends BackendBaseActionIndex
  17. {
  18. /**
  19. * The category where is filtered on
  20. *
  21. * @var array
  22. */
  23. private $category;
  24. /**
  25. * The id of the category where is filtered on
  26. *
  27. * @var int
  28. */
  29. private $categoryId;
  30. /**
  31. * DataGrids
  32. *
  33. * @var SpoonDataGrid
  34. */
  35. private $dgDrafts, $dgPosts, $dgRecent;
  36. /**
  37. * Execute the action
  38. */
  39. public function execute()
  40. {
  41. parent::execute();
  42. // set category id
  43. $this->categoryId = SpoonFilter::getGetValue('category', null, null, 'int');
  44. if($this->categoryId == 0) $this->categoryId = null;
  45. else
  46. {
  47. // get category
  48. $this->category = BackendBlogModel::getCategory($this->categoryId);
  49. // reset
  50. if(empty($this->category))
  51. {
  52. // reset GET to trick Spoon
  53. $_GET['category'] = null;
  54. // reset
  55. $this->categoryId = null;
  56. }
  57. }
  58. $this->loadDataGrids();
  59. $this->parse();
  60. $this->display();
  61. }
  62. /**
  63. * Loads the datagrid with all the posts
  64. */
  65. private function loadDataGridAllPosts()
  66. {
  67. // filter on category?
  68. if($this->categoryId != null)
  69. {
  70. // create datagrid
  71. $this->dgPosts = new BackendDataGridDB(BackendBlogModel::QRY_DATAGRID_BROWSE_FOR_CATEGORY, array($this->categoryId, 'active', BL::getWorkingLanguage()));
  72. // set the URL
  73. $this->dgPosts->setURL('&amp;category=' . $this->categoryId, true);
  74. }
  75. else
  76. {
  77. // create datagrid
  78. $this->dgPosts = new BackendDataGridDB(BackendBlogModel::QRY_DATAGRID_BROWSE, array('active', BL::getWorkingLanguage()));
  79. }
  80. // set headers
  81. $this->dgPosts->setHeaderLabels(array('user_id' => SpoonFilter::ucfirst(BL::lbl('Author')), 'publish_on' => SpoonFilter::ucfirst(BL::lbl('PublishedOn'))));
  82. // hide columns
  83. $this->dgPosts->setColumnsHidden(array('revision_id'));
  84. // sorting columns
  85. $this->dgPosts->setSortingColumns(array('publish_on', 'title', 'user_id', 'comments'), 'publish_on');
  86. $this->dgPosts->setSortParameter('desc');
  87. // set column functions
  88. $this->dgPosts->setColumnFunction(array('BackendDataGridFunctions', 'getLongDate'), array('[publish_on]'), 'publish_on', true);
  89. $this->dgPosts->setColumnFunction(array('BackendDataGridFunctions', 'getUser'), array('[user_id]'), 'user_id', true);
  90. // our JS needs to know an id, so we can highlight it
  91. $this->dgPosts->setRowAttributes(array('id' => 'row-[revision_id]'));
  92. // check if this action is allowed
  93. if(BackendAuthentication::isAllowedAction('edit'))
  94. {
  95. // set column URLs
  96. $this->dgPosts->setColumnURL('title', BackendModel::createURLForAction('edit') . '&amp;id=[id]&amp;category=' . $this->categoryId);
  97. // add edit column
  98. $this->dgPosts->addColumn('edit', null, BL::lbl('Edit'), BackendModel::createURLForAction('edit') . '&amp;id=[id]&amp;category=' . $this->categoryId, BL::lbl('Edit'));
  99. }
  100. }
  101. /**
  102. * Loads the datagrid with all the drafts
  103. */
  104. private function loadDataGridDrafts()
  105. {
  106. // filter on category?
  107. if($this->categoryId != null)
  108. {
  109. // create datagrid
  110. $this->dgDrafts = new BackendDataGridDB(BackendBlogModel::QRY_DATAGRID_BROWSE_DRAFTS_FOR_CATEGORY, array($this->categoryId, 'draft', BackendAuthentication::getUser()->getUserId(), BL::getWorkingLanguage()));
  111. // set the URL
  112. $this->dgDrafts->setURL('&amp;category=' . $this->categoryId, true);
  113. }
  114. else
  115. {
  116. // create datagrid
  117. $this->dgDrafts = new BackendDataGridDB(BackendBlogModel::QRY_DATAGRID_BROWSE_DRAFTS, array('draft', BackendAuthentication::getUser()->getUserId(), BL::getWorkingLanguage()));
  118. }
  119. // set headers
  120. $this->dgDrafts->setHeaderLabels(array('user_id' => SpoonFilter::ucfirst(BL::lbl('Author'))));
  121. // hide columns
  122. $this->dgDrafts->setColumnsHidden(array('revision_id'));
  123. // sorting columns
  124. $this->dgDrafts->setSortingColumns(array('edited_on', 'title', 'user_id', 'comments'), 'edited_on');
  125. $this->dgDrafts->setSortParameter('desc');
  126. // set column functions
  127. $this->dgDrafts->setColumnFunction(array('BackendDataGridFunctions', 'getLongDate'), array('[edited_on]'), 'edited_on', true);
  128. $this->dgDrafts->setColumnFunction(array('BackendDataGridFunctions', 'getUser'), array('[user_id]'), 'user_id', true);
  129. // our JS needs to know an id, so we can highlight it
  130. $this->dgDrafts->setRowAttributes(array('id' => 'row-[revision_id]'));
  131. // check if this action is allowed
  132. if(BackendAuthentication::isAllowedAction('edit'))
  133. {
  134. // set colum URLs
  135. $this->dgDrafts->setColumnURL('title', BackendModel::createURLForAction('edit') . '&amp;id=[id]&amp;draft=[revision_id]&amp;category=' . $this->categoryId);
  136. // add edit column
  137. $this->dgDrafts->addColumn('edit', null, BL::lbl('Edit'), BackendModel::createURLForAction('edit') . '&amp;id=[id]&amp;draft=[revision_id]&amp;category=' . $this->categoryId, BL::lbl('Edit'));
  138. }
  139. }
  140. /**
  141. * Loads the datagrid with the most recent posts.
  142. */
  143. private function loadDataGridRecentPosts()
  144. {
  145. // filter on category?
  146. if($this->categoryId != null)
  147. {
  148. // create datagrid
  149. $this->dgRecent = new BackendDataGridDB(BackendBlogModel::QRY_DATAGRID_BROWSE_RECENT_FOR_CATEGORY, array($this->categoryId, 'active', BL::getWorkingLanguage(), 4));
  150. // set the URL
  151. $this->dgRecent->setURL('&amp;category=' . $this->categoryId, true);
  152. }
  153. else
  154. {
  155. // create datagrid
  156. $this->dgRecent = new BackendDataGridDB(BackendBlogModel::QRY_DATAGRID_BROWSE_RECENT, array('active', BL::getWorkingLanguage(), 4));
  157. }
  158. // set headers
  159. $this->dgRecent->setHeaderLabels(array('user_id' => SpoonFilter::ucfirst(BL::lbl('Author'))));
  160. // hide columns
  161. $this->dgRecent->setColumnsHidden(array('revision_id'));
  162. // set paging
  163. $this->dgRecent->setPaging(false);
  164. // set column functions
  165. $this->dgRecent->setColumnFunction(array('BackendDataGridFunctions', 'getLongDate'), array('[edited_on]'), 'edited_on', true);
  166. $this->dgRecent->setColumnFunction(array('BackendDataGridFunctions', 'getUser'), array('[user_id]'), 'user_id', true);
  167. // our JS needs to know an id, so we can highlight it
  168. $this->dgRecent->setRowAttributes(array('id' => 'row-[revision_id]'));
  169. // check if this action is allowed
  170. if(BackendAuthentication::isAllowedAction('edit'))
  171. {
  172. // set colum URLs
  173. $this->dgRecent->setColumnURL('title', BackendModel::createURLForAction('edit') . '&amp;id=[id]&amp;category=' . $this->categoryId);
  174. // add edit column
  175. $this->dgRecent->addColumn('edit', null, BL::lbl('Edit'), BackendModel::createURLForAction('edit') . '&amp;id=[id]&amp;category=' . $this->categoryId, BL::lbl('Edit'));
  176. }
  177. }
  178. /**
  179. * Loads the datagrids for the blogposts
  180. */
  181. private function loadDataGrids()
  182. {
  183. $this->loadDataGridAllPosts();
  184. $this->loadDataGridDrafts();
  185. // the most recent blogposts, only shown when we have more than 1 page in total
  186. if($this->dgPosts->getNumResults() > $this->dgPosts->getPagingLimit()) $this->loadDataGridRecentPosts();
  187. }
  188. /**
  189. * Parse all datagrids
  190. */
  191. protected function parse()
  192. {
  193. parent::parse();
  194. // parse the datagrid for the drafts
  195. $this->tpl->assign('dgDrafts', ($this->dgDrafts->getNumResults() != 0) ? $this->dgDrafts->getContent() : false);
  196. // parse the datagrid for all blogposts
  197. $this->tpl->assign('dgPosts', ($this->dgPosts->getNumResults() != 0) ? $this->dgPosts->getContent() : false);
  198. // parse the datagrid for the most recent blogposts
  199. $this->tpl->assign('dgRecent', (is_object($this->dgRecent) && $this->dgRecent->getNumResults() != 0) ? $this->dgRecent->getContent() : false);
  200. // get categories
  201. $categories = BackendBlogModel::getCategories(true);
  202. // multiple categories?
  203. if(count($categories) > 1)
  204. {
  205. // create form
  206. $frm = new BackendForm('filter', null, 'get', false);
  207. // create element
  208. $frm->addDropdown('category', $categories, $this->categoryId);
  209. $frm->getField('category')->setDefaultElement('');
  210. // parse the form
  211. $frm->parse($this->tpl);
  212. }
  213. // parse category
  214. if(!empty($this->category)) $this->tpl->assign('filterCategory', $this->category);
  215. }
  216. }