PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/mod_featuredblog/helper.php

https://bitbucket.org/osobh/invertnet
PHP | 363 lines | 227 code | 58 blank | 78 comment | 37 complexity | baec5b480a1d662a405e224facc04079 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package hubzero-cms
  4. * @author Shawn Rice <zooley@purdue.edu>
  5. * @copyright Copyright 2005-2011 Purdue University. All rights reserved.
  6. * @license http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
  7. *
  8. * Copyright 2005-2011 Purdue University. All rights reserved.
  9. * All rights reserved.
  10. *
  11. * This file is part of: The HUBzero(R) Platform for Scientific Collaboration
  12. *
  13. * The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
  14. * software: you can redistribute it and/or modify it under the terms of
  15. * the GNU Lesser General Public License as published by the Free Software
  16. * Foundation, either version 3 of the License, or (at your option) any
  17. * later version.
  18. *
  19. * HUBzero is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. * HUBzero is a registered trademark of Purdue University.
  28. */
  29. // Check to ensure this file is included in Joomla!
  30. defined('_JEXEC') or die( 'Restricted access' );
  31. class modFeaturedblog
  32. {
  33. private $attributes = array();
  34. //-----------
  35. public function __construct( $params )
  36. {
  37. $this->params = $params;
  38. }
  39. //-----------
  40. public function __set($property, $value)
  41. {
  42. $this->attributes[$property] = $value;
  43. }
  44. //-----------
  45. public function __get($property)
  46. {
  47. if (isset($this->attributes[$property])) {
  48. return $this->attributes[$property];
  49. }
  50. }
  51. //-----------
  52. private function niceidformat($someid)
  53. {
  54. $pre = '';
  55. if ($someid < 0) {
  56. $pre = 'n';
  57. $someid = abs($someid);
  58. }
  59. while (strlen($someid) < 5)
  60. {
  61. $someid = 0 . "$someid";
  62. }
  63. return $pre.$someid;
  64. }
  65. //-----------
  66. public function encode_html($str, $quotes=1)
  67. {
  68. $str = $this->ampersands($str);
  69. $a = array(
  70. //'&' => '&#38;',
  71. '<' => '&#60;',
  72. '>' => '&#62;',
  73. );
  74. if ($quotes) $a = $a + array(
  75. "'" => '&#39;',
  76. '"' => '&#34;',
  77. );
  78. return strtr($str, $a);
  79. }
  80. //-----------
  81. private function ampersands( $str )
  82. {
  83. $str = stripslashes($str);
  84. $str = str_replace('&#','*-*', $str);
  85. $str = str_replace('&amp;','&',$str);
  86. $str = str_replace('&','&amp;',$str);
  87. $str = str_replace('*-*','&#', $str);
  88. return $str;
  89. }
  90. //-----------
  91. private function thumb( $thumb )
  92. {
  93. $image = explode('.',$thumb);
  94. $n = count($image);
  95. $image[$n-2] .= '_thumb';
  96. $end = array_pop($image);
  97. $image[] = $end;
  98. $thumb = implode('.',$image);
  99. return $thumb;
  100. }
  101. //-----------
  102. public function display()
  103. {
  104. require_once( JPATH_ROOT.DS.'components'.DS.'com_features'.DS.'tables'.DS.'history.php' );
  105. ximport('Hubzero_User_Profile');
  106. $this->error = false;
  107. if (!class_exists('FeaturesHistory')) {
  108. $this->error = true;
  109. return false;
  110. }
  111. $database =& JFactory::getDBO();
  112. $params =& $this->params;
  113. $pass = 1;
  114. $filters = array();
  115. $filters['limit'] = 1;
  116. $filters['show'] = trim($params->get( 'show' ));
  117. $this->cls = trim($params->get( 'moduleclass_sfx' ));
  118. $this->txt_length = trim($params->get( 'txt_length' ));
  119. $catid = trim($params->get( 'catid' ));
  120. //$min = trim($params->get( 'min_contributions' ));
  121. //$show = trim($params->get( 'show' ));
  122. $start = date('Y-m-d', mktime(0,0,0,date('m'),date('d'), date('Y')))." 00:00:00";
  123. $end = date('Y-m-d', mktime(0,0,0,date('m'),date('d'), date('Y')))." 23:59:59";
  124. $row = null;
  125. $fh = new FeaturesHistory( $database );
  126. // Is a specific content category set?
  127. if ($catid) {
  128. // Yes - so we need to check if there's an active article to display
  129. $juser =& JFactory::getUser();
  130. $aid = $juser->get('aid', 0);
  131. $contentConfig =& JComponentHelper::getParams( 'com_content' );
  132. $noauth = !$contentConfig->get('shownoauth');
  133. $date =& JFactory::getDate();
  134. $now = $date->toMySQL();
  135. $nullDate = $database->getNullDate();
  136. // Load an article
  137. $query = 'SELECT a.*,' .
  138. ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
  139. ' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug'.
  140. ' FROM #__content AS a' .
  141. ' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
  142. ' INNER JOIN #__sections AS s ON s.id = a.sectionid' .
  143. ' WHERE a.state = 1 ' .
  144. ($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').
  145. ' AND (a.publish_up = '.$database->Quote($nullDate).' OR a.publish_up <= '.$database->Quote($now).' ) ' .
  146. ' AND (a.publish_down = '.$database->Quote($nullDate).' OR a.publish_down >= '.$database->Quote($now).' )' .
  147. ' AND cc.id = '. (int) $catid .
  148. ' AND cc.section = s.id' .
  149. ' AND cc.published = 1' .
  150. ' AND s.published = 1' .
  151. ' ORDER BY a.ordering';
  152. $database->setQuery($query, 0, $filters['limit']);
  153. $rows = $database->loadObjectList();
  154. if (count($rows) > 0) {
  155. $row = $rows[0];
  156. }
  157. }
  158. $profile = null;
  159. // Do we have an article to display?
  160. if (!$row) {
  161. // No - so we need to display a member
  162. // Load some needed libraries
  163. include_once( JPATH_ROOT.DS.'components'.DS.'com_blog'.DS.'tables'.DS.'blog.comment.php' );
  164. include_once( JPATH_ROOT.DS.'components'.DS.'com_blog'.DS.'tables'.DS.'blog.entry.php' );
  165. // Check the feature history for today's feature
  166. $fh->loadActive($start, 'blogs');
  167. // Did we find a feature for today?
  168. if ($fh->id && $fh->tbl == 'blogs') {
  169. // Yes - load the member profile
  170. $row = new BlogEntry( $database );
  171. $row->load( $fh->objectid );
  172. } else {
  173. // No - so we need to randomly choose one
  174. // Get the pass number
  175. $query = "SELECT MAX(note) FROM jos_feature_history WHERE tbl='blogs'";
  176. $database->setQuery( $query );
  177. $pass = $database->loadResult();
  178. // Get all the items featured for the pass
  179. $query = "SELECT DISTINCT(objectid) FROM jos_feature_history WHERE tbl='blogs' AND note='$pass'";
  180. $database->setQuery( $query );
  181. $ids = $database->loadResultArray();
  182. // Some IDs found. So add SQL that tells the query not to select them (everyone should be featured at least once)
  183. if ($ids && count($ids) > 0) {
  184. $idst = implode("','",$ids);
  185. $filters['sql'] = "m.id NOT IN('$idst')";
  186. }
  187. $filters['start'] = 0;
  188. $filters['state'] = 'public';
  189. $filters['order'] = "RAND()";
  190. $filters['search'] = '';
  191. $filters['scope'] = 'member';
  192. $filters['group_id'] = 0;
  193. $filters['authorized'] = false;
  194. $mp = new BlogEntry( $database );
  195. $rows = $mp->getRecords( $filters );
  196. if ($rows && count($rows) > 0) {
  197. $row = $rows[0];
  198. } else {
  199. // No records found. Start a new pass.
  200. $filters['sql'] = "";
  201. $pass++;
  202. $rows = $mp->getRecords( $filters );
  203. if (count($rows) > 0) {
  204. $row = $rows[0];
  205. }
  206. }
  207. // Load their bio
  208. $profile = new Hubzero_User_Profile();
  209. $profile->load( $row->created_by );
  210. /*if (trim(strip_tags($profile->get('bio'))) == '') {
  211. return $this->display();
  212. }*/
  213. }
  214. }
  215. // Did we have a result to display?
  216. if ($row) {
  217. $this->row = $row;
  218. $config =& JComponentHelper::getParams( 'com_members' );
  219. // Is this a content article or a member profile?
  220. if (isset($row->catid)) {
  221. // Content article
  222. $this->title = $row->title;
  223. $this->id = $row->created_by_alias;
  224. $this->txt = $row->introtext;
  225. $profile = new Hubzero_User_Profile();
  226. $profile->load( $id );
  227. //$row->picture = $profile->get('picture');
  228. // Check if the article has been saved in the feature history
  229. $fh->loadObject($row->id, 'content');
  230. if (!$fh->id) {
  231. $fh->featured = $start;
  232. $fh->objectid = $row->id;
  233. $fh->tbl = 'content';
  234. $fh->note = $pass;
  235. $fh->store();
  236. }
  237. } else {
  238. if (!isset($profile) && !is_object($profile)) {
  239. $profile = new Hubzero_User_Profile();
  240. $profile->load( $row->created_by );
  241. }
  242. $this->txt = $row->content; //$profile->get('bio');
  243. // Member profile
  244. $this->title = $row->title;
  245. /*if (!trim($title)) {
  246. $title = $row->givenName.' '.$row->surname;
  247. }*/
  248. $this->id = $row->id;
  249. // Check if this has been saved in the feature history
  250. if (!$fh->id) {
  251. $fh->featured = $start;
  252. $fh->objectid = $row->id;
  253. $fh->tbl = 'blogs';
  254. $fh->note = $pass;
  255. $fh->store();
  256. }
  257. }
  258. // Do we have a picture?
  259. $thumb = '';
  260. if (isset($profile->picture) && $profile->picture != '') {
  261. // Yes - so build the path to it
  262. $thumb = $config->get('webpath');
  263. if (substr($thumb, 0, 1) != DS) {
  264. $thumb = DS.$thumb;
  265. }
  266. if (substr($thumb, -1, 1) == DS) {
  267. $thumb = substr($thumb, 0, (strlen($thumb) - 1));
  268. }
  269. $thumb .= DS.$this->niceidformat($profile->uidNumber).DS.$profile->picture;
  270. // No - use default picture
  271. if (is_file(JPATH_ROOT.$thumb)) {
  272. // Build a thumbnail filename based off the picture name
  273. $thumb = $this->thumb( $thumb );
  274. if (!is_file(JPATH_ROOT.$thumb)) {
  275. // Create a thumbnail image
  276. include_once( JPATH_ROOT.DS.'components'.DS.'com_members'.DS.'helpers'.DS.'imghandler.php' );
  277. $ih = new MembersImgHandler();
  278. $ih->set('image',$profile->picture);
  279. $ih->set('path',JPATH_ROOT.$config->get('webpath').DS.$this->niceidformat($profile->uidNumber).DS);
  280. $ih->set('maxWidth', 50);
  281. $ih->set('maxHeight', 50);
  282. $ih->set('cropratio', '1:1');
  283. $ih->set('outputName', $ih->createThumbName());
  284. /*if (!$ih->process()) {
  285. $html .= '<!-- Error: '. $ih->getError() .' -->';
  286. }*/
  287. }
  288. }
  289. }
  290. // No - use default picture
  291. if (!is_file(JPATH_ROOT.$thumb)) {
  292. $thumb = $config->get('defaultpic');
  293. if (substr($thumb, 0, 1) != DS) {
  294. $thumb = DS.$thumb;
  295. }
  296. // Build a thumbnail filename based off the picture name
  297. $thumb = $this->thumb( $thumb );
  298. }
  299. //$this->row = $row;
  300. $this->profile = $profile;
  301. $this->thumb = $thumb;
  302. $this->filters = $filters;
  303. }
  304. }
  305. }