/modules/mod_metamod/mod_metamod/jomgenius/content.php

https://bitbucket.org/dreamriks/gift · PHP · 307 lines · 235 code · 37 blank · 35 comment · 53 complexity · b61440f8cd78ee5244f9ffa1bab03129 MD5 · raw file

  1. <?php
  2. // no direct access
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. /**
  5. * JomGenius classes
  6. *
  7. * @package JomGenius
  8. * @version 5
  9. * @license GNU/GPL
  10. * @copyright Copyright (C) 2010-2011 Brandon IT Consulting. All rights reserved.
  11. */
  12. class JomGeniusClassContent extends JomGeniusParent {
  13. var $Itemid;
  14. var $view;
  15. var $option;
  16. var $category_id;
  17. var $section_id;
  18. var $layout;
  19. var $task;
  20. var $id;
  21. function __construct() {
  22. $this->Itemid = JRequest::getVar('Itemid');
  23. $this->view = JRequest::getWord('view');
  24. $this->option = JRequest::getVar('option');
  25. $this->category_id = JRequest::getVar('category_id');
  26. $this->section_id = JRequest::getVar('section_id');
  27. $this->layout = JRequest::getVar('layout');
  28. $this->task = JRequest::getVar('task');
  29. $this->id = JRequest::getVar('id');
  30. }
  31. function shouldInstantiate() {
  32. return true;
  33. // we allow instantiation even if we are not viewing a com_content page,
  34. // as there are some things we can query without being on that page.
  35. //( $this->option == 'com_content' );
  36. }
  37. /* particular methods for this component */
  38. /**
  39. * A generic function that knows how to get lots of different info about the current article, category or section.
  40. */
  41. function info( $type ) {
  42. // some special handling, that need not hit the database
  43. switch( $type ) {
  44. case 'category_id': return $this->categoryId();
  45. case 'section_id' : return $this->sectionId();
  46. case 'pagetype':
  47. case 'page_type' : return $this->pageType();
  48. case 'page_number':
  49. case 'pagenumber' : return $this->pageNumber(); // pagination control, 0 is 1st page
  50. default:
  51. }
  52. if ( $this->option != 'com_content' ) return null;
  53. // from here on, only serves info relating to com_content pages that we are on now.
  54. // everything else requires the database
  55. if ( $this->view == 'article' ) {
  56. $row = $this->_infoForArticleId( $this->id );
  57. } else if ( $this->view == 'category' ) {
  58. $row = $this->_infoForCategoryId( $this->id );
  59. } else if ( $this->view == 'section' ) {
  60. $row = $this->_infoForSectionId( $this->id );
  61. }
  62. switch( $type ) {
  63. case 'article_id':
  64. case 'article_title':
  65. case 'article_alias':
  66. case 'article_hits':
  67. case 'article_version':
  68. case 'article_created_by':
  69. case 'article_modified_by':
  70. case 'article_introtext':
  71. case 'article_fulltext':
  72. case 'article_created':
  73. case 'article_modified':
  74. case 'article_publishup':
  75. case 'article_publishdown':
  76. case 'article_metakeywords':
  77. case 'article_metadescription':
  78. case 'article_created_age':
  79. case 'article_modified_age':
  80. case 'article_frontpage':
  81. case 'category_id':
  82. case 'category_title':
  83. case 'category_alias':
  84. case 'category_count':
  85. case 'section_id':
  86. case 'section_title':
  87. case 'section_alias':
  88. case 'section_count':
  89. return @$row->$type;
  90. default:
  91. }
  92. // are there some more things that we might need to calculate?
  93. return null;
  94. }
  95. /**
  96. * pageNumber() gives pagination information. 0 = 1st page of pagination; index increases from there.
  97. */
  98. function pageNumber() {
  99. if ( $this->option != 'com_content' ) return null;
  100. if ( $this->view == 'article' ) {
  101. return JRequest::getInt( 'limitstart', 0 );
  102. }
  103. $limitstart = JRequest::getInt( 'limitstart', 0 );
  104. $limit = JRequest::getInt( 'limit', 0 ); // provided by Joomla, not in the URL itself. # items per page.
  105. if ( $limit == 0 ) return 0; // we're not on a paginated page?
  106. return (int)( $limit / $limitstart );
  107. }
  108. function pageType() {
  109. if ( $this->option != 'com_content' ) return null;
  110. switch ( $this->view ) {
  111. case 'frontpage':
  112. return 'frontpage';
  113. case 'article':
  114. if ($this->task == 'edit') return 'articleedit';
  115. if ($this->layout == 'form') return 'articlesubmit';
  116. return 'article';
  117. case 'section':
  118. if ($this->layout == 'blog' ) return 'sectionblog';
  119. return 'sectionlist';
  120. case 'category':
  121. if ($this->layout == 'blog' ) return 'categoryblog';
  122. return 'categorylist';
  123. case 'archive':
  124. return 'archive';
  125. }
  126. if ( $this->task == 'new' ) return "articlesubmit";
  127. return '';
  128. }
  129. /* returns the category id of the list, or the item being displayed.
  130. * If the list: this is taken from the URL
  131. * If the item: if a category id was in the URL then this is used. Otherwise,
  132. * the category of the item is used.
  133. */
  134. function categoryId() {
  135. if ( $this->option != 'com_content' ) return null;
  136. $category_id = null;
  137. if ( $this->view == "category" ) {
  138. /* category list pages (blog or list style) */
  139. $category_id = (int)$this->id;
  140. } else if (array_key_exists("catid",$_REQUEST)) {
  141. /* if the category id is in the URL */
  142. $category_id = (int)JRequest::getInt("catid",0);
  143. }
  144. if ( $category_id === null && $this->view == "article" ) {
  145. /* if it's an article page without the catid mentioned in the url */
  146. $row = $this->_infoForArticleId( $this->id );
  147. $category_id = (int)@$row->category_id;
  148. }
  149. return $category_id;
  150. }
  151. function sectionId() {
  152. if ( $this->option != 'com_content' ) return null;
  153. $section_id = null;
  154. if ( $this->view == "section" ) {
  155. /* section list pages (blog or list style) */
  156. $section_id = (int)$this->id;
  157. } else if (array_key_exists("sectionid",$_REQUEST)) {
  158. /* if the section id is in the URL */
  159. $section_id = (int)JRequest::getInt("sectionid",0);
  160. }
  161. if ( $section_id === null && $this->view == "article" ) {
  162. /* if it's an article page without the sectionid mentioned in the url */
  163. $row = $this->_infoForArticleId( $this->id );
  164. $section_id = (int)@$row->section_id;
  165. }
  166. if ( $section_id === null && $this->view == "category" ) {
  167. /* if it's an category page without the sectionid mentioned in the url */
  168. $row = $this->_infoForCategoryId( (int)$this->id );
  169. $section_id = (int)@$row->section_id;
  170. }
  171. return $section_id;
  172. }
  173. function _infoForArticleId( $id ) {
  174. static $rows = array();
  175. if ( !array_key_exists( $id, $rows ) ) {
  176. $db =& JFactory::getDBO();
  177. $nullDate = $db->Quote( $db->getNullDate() );
  178. $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
  179. $jnow =& JFactory::getDate();
  180. $now = $db->Quote( $db->getEscaped( $jnow->toMySQL() ) );
  181. $query = "SELECT
  182. a.id as article_id,
  183. a.title as article_title,
  184. a.alias as article_alias,
  185. a.hits as article_hits,
  186. a.version as article_version,
  187. a.created_by as article_created_by,
  188. a.modified_by as article_modified_by,
  189. a.introtext as article_introtext,
  190. a.fulltext as article_fulltext,
  191. a.created as article_created,
  192. a.modified as article_modified,
  193. a.publish_up as article_publishup,
  194. a.publish_down as article_publishdown,
  195. a.metakey as article_metakeywords,
  196. a.metadesc as article_metadescription,
  197. floor(time_to_sec(timediff(now(),a.created))/60) as article_created_age,
  198. floor(time_to_sec(timediff(now(),a.modified))/60) as article_modified_age,
  199. a.catid as category_id,
  200. c.title as category_title,
  201. c.alias as category_alias,
  202. c.count as category_count,
  203. s.id as section_id,
  204. s.title as section_title,
  205. s.alias as section_alias,
  206. s.count as section_count,
  207. (f.content_id is not null) as article_frontpage
  208. FROM #__content a
  209. LEFT JOIN #__categories c ON a.catid = c.id
  210. LEFT JOIN #__sections s ON a.sectionid = s.id
  211. LEFT JOIN #__content_frontpage f ON a.id = f.content_id
  212. WHERE a.id = $my_id AND a.state = 1
  213. AND ( a.publish_up = $nullDate OR a.publish_up <= $now )
  214. AND ( a.publish_down = $nullDate OR a.publish_down >= $now )
  215. ";
  216. $db->setQuery( $query, 0, 1 );
  217. $row = $db->loadObject();
  218. $rows[$id] = $row;
  219. }
  220. return @$rows[$id];
  221. }
  222. function _infoForCategoryId( $id ) {
  223. static $rows = array();
  224. if ( !array_key_exists( $id, $rows ) ) {
  225. $db =& JFactory::getDBO();
  226. $nullDate = $db->Quote( $db->getNullDate() );
  227. $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
  228. $query = "SELECT
  229. c.id as category_id,
  230. c.title as category_title,
  231. c.alias as category_alias,
  232. c.count as category_count,
  233. c.description as category_description,
  234. s.id as section_id,
  235. s.title as section_title,
  236. s.alias as section_alias,
  237. s.count as section_count,
  238. s.description as section_description
  239. FROM #__categories c
  240. LEFT JOIN #__sections s ON c.section = s.id
  241. WHERE c.id = $my_id AND c.published = 1
  242. ";
  243. $db->setQuery( $query, 0, 1 );
  244. $row = $db->loadObject();
  245. $rows[$id] = $row;
  246. }
  247. return @$rows[$id];
  248. }
  249. function _infoForSectionId( $id ) {
  250. static $rows = array();
  251. if ( !array_key_exists( $id, $rows ) ) {
  252. $db =& JFactory::getDBO();
  253. $nullDate = $db->Quote( $db->getNullDate() );
  254. $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
  255. $query = "SELECT
  256. s.id as section_id,
  257. s.title as section_title,
  258. s.alias as section_alias,
  259. s.count as section_count,
  260. s.description as section_description
  261. FROM #__sections s
  262. WHERE s.id = $my_id AND s.published = 1
  263. ";
  264. $db->setQuery( $query, 0, 1 );
  265. $row = $db->loadObject();
  266. $rows[$id] = $row;
  267. }
  268. return @$rows[$id];
  269. }
  270. }