PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/bxpress/trunk/class/bxfunctions.class.php

http://bitcero-modules.googlecode.com/
PHP | 347 lines | 229 code | 56 blank | 62 comment | 28 complexity | 403d51c5658b37956de13de9b40de052 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id: bxfunctions.class.php 1034 2012-09-06 02:30:13Z i.bitcero $
  3. // --------------------------------------------------------------
  4. // bXpress Forums
  5. // An simple forums module for XOOPS and Common Utilities
  6. // Author: Eduardo Cortés <i.bitcero@gmail.com>
  7. // Email: i.bitcero@gmail.com
  8. // License: GPL 2.0
  9. // --------------------------------------------------------------
  10. /**
  11. * @desc Clase para el manejo de funciones internas del foro
  12. */
  13. class bXFunctions
  14. {
  15. private $db;
  16. public function __construct(){
  17. $this->db = XoopsDatabaseFactory::getDatabaseConnection();
  18. }
  19. public function menu_bar(){
  20. RMTemplate::get()->add_tool(__('Dashboard','exmbb'), './index.php', '../images/dash.png', 'dashboard');
  21. RMTemplate::get()->add_tool(__('Categories','exmbb'), './categos.php', '../images/categos.png', 'categories');
  22. RMTemplate::get()->add_tool(__('Forums','exmbb'), './forums.php', '../images/forums.png', 'forums');
  23. RMTemplate::get()->add_tool(__('Announcements','exmbb'), './announcements.php', '../images/bell.png', 'messages');
  24. RMTemplate::get()->add_tool(__('Reports','exmbb'), './reports.php', '../images/reports.png', 'reports');
  25. RMTemplate::get()->add_tool(__('Prune','exmbb'), './prune.php', '../images/prune.png', 'prune');
  26. }
  27. public function get(){
  28. static $instance;
  29. if (!isset($instance)) {
  30. $instance = new bXFunctions();
  31. }
  32. return $instance;
  33. }
  34. /**
  35. * @desc Obtiene el último usuario registrado
  36. * @return objeto {@link XoopsUser}
  37. */
  38. function getLastUser(){
  39. $db = XoopsDatabaseFactory::getDatabaseConnection();
  40. $result = $db->query("SELECT * FROM ".$db->prefix("users")." WHERE level>'0' ORDER BY uid DESC LIMIT 0,1");
  41. if ($db->getRowsNum($result)>0){
  42. $row = $db->fetchArray($result);
  43. $user = new XoopsUser();
  44. $user->assignVars($row);
  45. return $user;
  46. }
  47. return false;
  48. }
  49. /**
  50. * @desc Obtiene el número de usuarios conectados
  51. * @param int $type Determina el tipo de usuario que devolvera:
  52. * 0 Devuelve usuarios anonimos
  53. * 1 Devuelve Usuarios registrados
  54. * 2 Devuelve todos los usuarios conectados
  55. * @return int
  56. */
  57. public function getOnlineCount($type = 1){
  58. global $xoopsModule;
  59. $db = XoopsDatabaseFactory::getDatabaseConnection();
  60. $sql = "SELECT COUNT(*) FROM ".$db->prefix("online")." WHERE online_module='".$xoopsModule->mid()."'";
  61. if ($type==0){
  62. $sql .= " AND online_uid<'1'";
  63. }elseif($type==1){
  64. $sql .= " AND online_uid>'0'";
  65. }
  66. list($num) = $db->fetchRow($db->query($sql));
  67. return $num;
  68. }
  69. /**
  70. * @desc Total de Usuarios Registrados
  71. * @return int
  72. */
  73. public function totalUsers(){
  74. $db = XoopsDatabaseFactory::getDatabaseConnection();
  75. list($num) = $db->fetchRow($db->query("SELECT COUNT(*) FROM ".$db->prefix("users")." WHERE level>'0'"));
  76. return $num;
  77. }
  78. /**
  79. * @desc Total de Temas en los Foros
  80. */
  81. public function totalTopics(){
  82. $db = XoopsDatabaseFactory::getDatabaseConnection();
  83. list($num) = $db->fetchRow($db->query("SELECT COUNT(*) FROM ".$db->prefix("bxpress_topics")));
  84. return $num;
  85. }
  86. /**
  87. * @desc Total de Mensajes en los Foros
  88. */
  89. public function totalPosts(){
  90. $db = XoopsDatabaseFactory::getDatabaseConnection();
  91. list($num) = $db->fetchRow($db->query("SELECT COUNT(*) FROM ".$db->prefix("bxpress_posts")));
  92. return $num;
  93. }
  94. /**
  95. * @desc Formatea la fecha
  96. */
  97. public function formatDate($time){
  98. global $mc;
  99. $real = time() - $time;
  100. $today = date('G',time()) * 3600;
  101. $today += (date('i', time()) * 60);
  102. $today += date('s', time());
  103. if ($real<=$today){
  104. return sprintf(__('Today %s','bxpress'), date('H:i:s', $time));
  105. }elseif ($real<=($today-86400)){
  106. return sprintf(__('Yesterday %s','bxpress'), date('H:i:s', $time));
  107. }else{
  108. return formatTimeStamp($time);
  109. }
  110. }
  111. /**
  112. * @desc Creamos el encabezado del módulo
  113. */
  114. public function makeHeader(){
  115. global $xoopsTpl, $xoopsModuleConfig, $xoopsUser;
  116. $tpl = $xoopsTpl;
  117. $tpl->assign('lang_index', __('Forum Index','bxpress'));
  118. $tpl->assign('forums_title', $xoopsModuleConfig['forum_title']);
  119. if ($xoopsUser || $xoopsModuleConfig['search']){
  120. $tpl->assign('lang_search',__('Search','bxpress'));
  121. $tpl->assign('lang_searchq', __('Search:','bxpress'));
  122. $tpl->assign('can_search',1);
  123. }
  124. }
  125. /**
  126. * @desc Crea las páginas para el indice de temas
  127. */
  128. public function paginateIndex($tpages, $limit=3){
  129. $ret = array();
  130. for ($i=1;$i<=$tpages;$i++){
  131. $ret[] = $i;
  132. if ($i==$limit && $tpages>$limit){
  133. $i = $tpages-1;
  134. $ret[] = '...';
  135. }
  136. }
  137. return $ret;
  138. }
  139. /**
  140. * @desc Determina la página del tema dependiendo del id de un post
  141. */
  142. public function pageFromPID($pid){
  143. global $xoopsModuleConfig;
  144. $db = XoopsDatabaseFactory::getDatabaseConnection();
  145. $result = $db->query('SELECT id_topic FROM '.$db->prefix('bxpress_posts')." WHERE id_post='$pid'");
  146. if (!$db->getRowsNum($result)) return;
  147. list($id) = $db->fetchRow($result);
  148. // Determine on what page the post is located (depending on $pun_user['disp_posts'])
  149. $result = $db->query("SELECT id_post FROM ".$db->prefix('bxpress_posts')." WHERE id_topic='$id' ORDER BY post_time");
  150. $num = $db->getRowsNum($result);
  151. for ($i = 0; $i < $num; ++$i)
  152. {
  153. list($cur_id) = $db->fetchRow($result);
  154. if ($cur_id == $pid)
  155. break;
  156. }
  157. ++$i; // we started at 0
  158. $_GET['pag'] = ceil($i / $xoopsModuleConfig['perpage']);
  159. return $id;
  160. }
  161. /**
  162. * @desc Obtiene el id del primer mensaje de un tema
  163. * @param int Id del tema
  164. * @return int
  165. */
  166. public function getFirstId($topic_id){
  167. $db = XoopsDatabaseFactory::getDatabaseConnection();
  168. $sql = "SELECT MIN(id_post) FROM ".$db->prefix("bxpress_posts")." WHERE id_topic='".$topic_id."'";
  169. list($first_id) = $db->fetchRow($db->query($sql));
  170. return $first_id;
  171. }
  172. public function forumList($varname = 'forums'){
  173. global $db, $tpl;
  174. $db = XoopsDatabaseFactory::getDatabaseConnection();
  175. $sql = "SELECT * FROM ".$db->prefix("bxpress_forums")." WHERE active='1' ORDER BY cat,`order`";
  176. $result = $db->query($sql);
  177. while ($row = $db->fetchArray($result)){
  178. $forum = new bXForum();
  179. $forum->assignVars($row);
  180. $tpl->append($varname, array('id'=>$forum->id(),'title'=>$forum->name()));
  181. }
  182. }
  183. /**
  184. * Load announcements form database
  185. * @param int Where to search (0 = home page, 1 = froum, 2 = all module)
  186. * @return array
  187. */
  188. public function loadAnnouncements($w, $forum=0){
  189. global $xoopsModuleConfig, $tpl;
  190. if (!$xoopsModuleConfig['announcements']) return;
  191. $db = XoopsDatabaseFactory::getDatabaseConnection();
  192. // Primero purgamos la tabla
  193. $db->queryF("DELETE FROM ".$db->prefix("bxpress_announcements")." WHERE expire<='".time()."'");
  194. $mc =& $xoopsModuleConfig;
  195. $sql = "SELECT * FROM ".$db->prefix("bxpress_announcements");
  196. switch ($w){
  197. case 0:
  198. $sql .= " WHERE `where`=0 OR `where`=2 ";
  199. break;
  200. case 1:
  201. $sql .= " WHERE `where`=2 OR (`where`='1' AND forum='$forum') ";
  202. break;
  203. }
  204. if ($mc['announcements_mode']){
  205. $sql .= " ORDER BY RAND() ";
  206. } else {
  207. $sql .= " ORDER BY `date` DESC ";
  208. }
  209. $sql .= "LIMIT 0, $mc[announcements_max]";
  210. $result = $db->query($sql);
  211. while ($row = $db->fetchArray($result)){
  212. $an = new bXAnnouncement();
  213. $an->assignVars($row);
  214. $tpl->append('announcements', array('text'=>$an->text('s')));
  215. }
  216. return true;
  217. }
  218. /**
  219. * @desc Notifica al grupo de administradores la creación de un nuevo tema no aprobado
  220. * @param {@link } Objetos de Foro, Tema y mensaje
  221. * @param int edit indica si es la edición de un mensaje o un nuevo tema no aprobado
  222. **/
  223. public function notifyAdmin($moderators,BBForum &$forum, BBTopic &$topic, BBPost &$post,$edit=0){
  224. global $db, $xoopsModule, $rmc_config;
  225. $bxf = bXFunctions::get();
  226. $mhand = new XoopsMemberHandler($db);
  227. $configCat = new XoopsConfigCategory('mailer', 'mailer');
  228. $config =& $configCat->getConfigs(3);
  229. $users = $moderators;
  230. if (!$edit){
  231. if (file_exists(XOOPS_ROOT_PATH.'/modules/bxpress/lang/'.RMCLANG.'/admin_notify.tpl')){
  232. $tpldir = XOOPS_ROOT_PATH.'/modules/bxpress/lang/'.RMCLANG;
  233. } else {
  234. $tpldir = XOOPS_ROOT_PATH.'/modules/bxpress/lang/en';
  235. }
  236. }else{
  237. if (file_exists(XOOPS_ROOT_PATH.'/modules/bxpress/lang/'.RMCLANG.'/admin_notify_post.tpl')){
  238. $tpldir = XOOPS_ROOT_PATH.'/modules/bxpress/lang/'.RMCLANG;
  239. } else {
  240. $tpldir = XOOPS_ROOT_PATH.'/modules/bxpress/lang/en';
  241. }
  242. }
  243. foreach ($users as $k){
  244. $xoopsMailer =& getMailer();
  245. $xoopsMailer->setFromEmail($config['from']);
  246. $xoopsMailer->setFromName($config['fromname']);
  247. $xoopsMailer->setTemplateDir($tpldir);
  248. if (!$edit){
  249. $xoopsMailer->setSubject(sprintf(__('New topic created','bxpress'), $forum->name()));
  250. $xoopsMailer->setTemplate('admin_notify.tpl');
  251. }else{
  252. $xoopsMailer->setSubject(sprintf(__('A unapproved message has been edited','dtransport'), $topic->title()));
  253. $xoopsMailer->setTemplate('admin_notify_post.tpl');
  254. }
  255. $xoopsMailer->assign('FORUM_NAME',$forum->name());
  256. $xoopsMailer->assign('FORUM_MODNAME', $xoopsModule->name());
  257. $xoopsMailer->assign('TOPIC_UNAME', $topic->posterName());
  258. $xoopsMailer->assign('TOPIC_NAME', $topic->title());
  259. $xoopsMailer->assign('TOPIC_APPROVED', $topic->approved() ? _YES : _NO);
  260. $xoopsMailer->assign('TOPIC_LINK', $bxf->url().'/moderate.php?id='.$forum->id());
  261. $xoopsMailer->assign('POST_UNAME',$post->uname());
  262. $xoopsMailer->assign('POST_LINK',$post->permalink());
  263. $user = new XoopsUser($k);
  264. $xoopsMailer->setToUsers($user);
  265. $xoopsMailer->isMail = $user->getVar('notify_method')==2;
  266. $xoopsMailer->isPM = $user->getVar('notify_method')==1;
  267. $xoopsMailer->send(true);
  268. $xoopsMailer->clearAddresses();
  269. }
  270. echo $xoopsMailer->getErrors();
  271. }
  272. public function getRanks(){
  273. $db =& XoopsDatabaseFactory::getDatabaseConnection();
  274. $myts =& MyTextSanitizer::getInstance();
  275. $sql = sprintf('SELECT rank_id, rank_title, rank_image FROM ' . $db->prefix('ranks') . ' WHERE rank_special = %u', 1);
  276. $ret = array();
  277. $result = $db->query($sql);
  278. while ($myrow = $db->fetchArray($result)) {
  279. $ret[$myrow['rank_id']] = array('title'=>$myrow['rank_title'],'image'=>$myrow['rank_image']);
  280. }
  281. return $ret;
  282. }
  283. public function url(){
  284. $mc = RMUtilities::module_config('bxpress');
  285. if($mc['urlmode']){
  286. return XOOPS_URL.'/'.$mc['htbase'];
  287. } else {
  288. return XOOPS_URL.'/modules/bxpress';
  289. }
  290. }
  291. }