PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bxpress/trunk/class/bxforum.class.php

http://bitcero-modules.googlecode.com/
PHP | 509 lines | 266 code | 47 blank | 196 comment | 28 complexity | 42469de53d57ef58a0d54b3924484689 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id: bxforum.class.php 896 2012-01-02 18:43:23Z 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 Constantes para selección de permisos del usuario
  12. */
  13. define('BXPRESS_PERM_VIEW','view');
  14. define('BXPRESS_PERM_TOPIC','topic');
  15. define('BXPRESS_PERM_REPLY','reply');
  16. define('BXPRESS_PERM_EDIT','edit');
  17. define('BXPRESS_PERM_DELETE','delete');
  18. define('BXPRESS_PERM_VOTE','vote');
  19. define('BXPRESS_PERM_ATTACH','attach');
  20. define('BXPRESS_PERM_APPROVE','approve');
  21. /**
  22. * @desc Clase para el manejo de objetos foro
  23. */
  24. class bXForum extends RMObject
  25. {
  26. /**
  27. * @param int $id Identificador del Foro
  28. * @param string $id Identificador alfanumerico
  29. */
  30. public function __construct($id=null){
  31. $this->db = XoopsDatabaseFactory::getDatabaseConnection();
  32. $this->_dbtable = $this->db->prefix("bxpress_forums");
  33. $this->setNew();
  34. $this->initVarsFromTable();
  35. $this->setVarType('moderators', XOBJ_DTYPE_ARRAY);
  36. $this->setVarType('permissions', XOBJ_DTYPE_ARRAY);
  37. $this->setVarType('attach_ext', XOBJ_DTYPE_ARRAY);
  38. if (!isset($id)) return;
  39. /**
  40. * Cargamos los datos del foro
  41. */
  42. if (is_numeric($id)){
  43. if (!$this->loadValues($id)) return;
  44. $this->unsetNew();
  45. } else {
  46. $this->primary = 'friendname';
  47. if ($this->loadValues($id)) $this->unsetNew();
  48. $this->primary = 'id_cat';
  49. }
  50. }
  51. /**
  52. * @desc Metodos para acceso a las propiedades
  53. */
  54. public function id(){
  55. return $this->getVar('id_forum');
  56. }
  57. /**
  58. * @desc Nombre del Foro
  59. */
  60. public function name(){
  61. return $this->getVar('name');
  62. }
  63. public function setName($value){
  64. return $this->setVar('name', $value);
  65. }
  66. /**
  67. * @desc Descripción del Foro
  68. */
  69. public function description(){
  70. return $this->getVar('desc');
  71. }
  72. public function setDescription($value){
  73. return $this->setVar('desc', $value);
  74. }
  75. /**
  76. * @desc Foro padre del foro actual
  77. */
  78. public function parent(){
  79. return $this->getVar('parent');
  80. }
  81. /**
  82. * @param int $value Identificador del foro padre
  83. */
  84. public function setParent($value){
  85. return $this->setVar('parent', $value);
  86. }
  87. /**
  88. * @desc Moderadores del Foro
  89. * @return array Ids de los moderadores
  90. */
  91. public function moderators(){
  92. return $this->getVar('moderators');
  93. }
  94. /**
  95. * @param array $value Ids de los moderadores
  96. * @param string $value Lista separada por comas con los ids
  97. */
  98. public function setModerators($value){
  99. return $this->setVar('moderators', $value);
  100. }
  101. /**
  102. * @desc Número de temas existentes en el foro
  103. * @return int
  104. */
  105. public function topics(){
  106. return $this->getVar('topics');
  107. }
  108. /**
  109. * @param int $value Numero de temas del foro
  110. */
  111. public function setTopics($value){
  112. return $this->setVar('topics', $value);
  113. }
  114. /**
  115. * @desc Incrementa el contador de temas
  116. */
  117. public function addTopic(){
  118. $this->setTopics($this->topics()+1);
  119. }
  120. /**
  121. * @desc Obtiene el Número de envios en el foro
  122. * @return int
  123. */
  124. public function posts(){
  125. return $this->getVar('posts');
  126. }
  127. /**
  128. * @desc Establece el número de envios en el foro
  129. * @param int $value Numero de envios del foro
  130. */
  131. public function setPosts($value){
  132. return $this->setVar('posts', $value);
  133. }
  134. /**
  135. * @desc Incrementa en uno el contador de mensajes
  136. */
  137. public function addPost(){
  138. $this->setPosts($this->posts()+1);
  139. }
  140. /**
  141. * @desc Devuelve el id del último envío en el foro
  142. * @return int
  143. */
  144. public function lastPostId(){
  145. return $this->getVar('last_post_id');
  146. }
  147. /**
  148. * @desc Establece el id del último envío en el foro
  149. * @param int $value Id del último envío
  150. */
  151. public function setPostId($value){
  152. return $this->setVar('last_post_id', $value);
  153. }
  154. /**
  155. * @desc Devuelve el id de la categoría a la que pertenece el foro
  156. * @return int
  157. */
  158. public function category(){
  159. return $this->getVar('cat');
  160. }
  161. /**
  162. * @desc Establece el id de la categoría a la que pertenece el foro
  163. * @param int $value Id de la categoría
  164. */
  165. public function setCategory($value){
  166. return $this->setVar('cat', $value);
  167. }
  168. /**
  169. * @desc Inidica si el módulo esta activo (1) o inactivo (0)
  170. * @return int
  171. */
  172. public function active(){
  173. return $this->getVar('active');
  174. }
  175. /**
  176. * @desc Establece l módulo como activo (1) o inactivo (0)
  177. * @param int $value 1 o 0
  178. */
  179. public function setActive($value){
  180. return $this->setVar('active', $value);
  181. }
  182. /**
  183. * @desc Permite saber si estanb activas (1) o inactivas (0) las firmas en los envios
  184. * @return int
  185. */
  186. public function signature(){
  187. return $this->getVar('sig');
  188. }
  189. /**
  190. * @desc Activa (1) o desactiva (0) las firmas en lso envios
  191. * @param int $value 1 o 0
  192. */
  193. public function setSignature($value){
  194. return $this->setVar('sig', $value);
  195. }
  196. /**
  197. * @desc Determina si los prefijos para titulos de envios estan activos(1) o inactivos(0)
  198. * @return int
  199. */
  200. public function prefix(){
  201. return $this->getVar('prefix');
  202. }
  203. /**
  204. * @desc Activa (1) o desactiva (0) el uso de prefijos en lso titulos de los envios
  205. * @param int $value 1 o 0
  206. */
  207. public function setPrefix($value){
  208. return $this->setVar('prefix', $value);
  209. }
  210. /**
  211. * @desc Devuelve el numero de respuestas para considerar popular un tema
  212. * @return int
  213. */
  214. public function hotThreshold(){
  215. return $this->getVar('hot_threshold');
  216. }
  217. /**
  218. * @desc Establece el numero de envios para considerar popular un tema
  219. * @param int $value Numero de envios
  220. */
  221. public function setHotThreshold($value){
  222. return $this->setVar('hot_threshold', $value);
  223. }
  224. /**
  225. * @desc Orden del foro
  226. * @return int
  227. */
  228. public function order(){
  229. return $this->getVar('order');
  230. }
  231. public function setOrder($value){
  232. return $this->setVar('order', $value);
  233. }
  234. /**
  235. * @desc Acepta archivos adjuntos
  236. * @return int
  237. */
  238. public function attachments(){
  239. return $this->getVar('attachments');
  240. }
  241. public function setAttachments($value){
  242. return $this->setVar('attachments', $value);
  243. }
  244. /**
  245. * @desc Tamańo máximo de los archivos adjuntos
  246. * @return int
  247. */
  248. public function maxSize(){
  249. return $this->getVar('attach_maxkb');
  250. }
  251. public function setMaxSize($value){
  252. return $this->setVar('attach_maxkb', $value);
  253. }
  254. /**
  255. * @desc Extensiones permitidas en los archivos adjuntos
  256. * @return array
  257. */
  258. public function extensions(){
  259. return $this->getVar('attach_ext');
  260. }
  261. /**
  262. * @desc Establece el tipo de extensiones permitidas para los archivos adjuntos
  263. * @param array $value
  264. * @param string $value Extensiones separadas por coma
  265. */
  266. public function setExtensions($value){
  267. if (!is_array($value)){
  268. $value = explode(',', trim($ext));
  269. }
  270. return $this->setVar('attach_ext', $value);
  271. }
  272. /**
  273. * @desc Numero de subforos
  274. * @return int
  275. */
  276. public function subforums(){
  277. return $this->getVar('subforums');
  278. }
  279. public function setSubforums($value){
  280. return $this->setVar('subforums', $value);
  281. }
  282. /**
  283. * @desc Nombre amigable para urls
  284. * @return int
  285. */
  286. public function friendName(){
  287. return $this->getVar('friendname');
  288. }
  289. public function setFriendName($value){
  290. return $this->setVar('friendname', $value);
  291. }
  292. /**
  293. * @desc Permisos del foro
  294. * @return array
  295. */
  296. public function permissions(){
  297. return $this->getVar('permissions');
  298. }
  299. /**
  300. * @param $value = array
  301. */
  302. public function setPermissions($value){
  303. if (!is_array($value)) return false;
  304. return $this->setVar('permissions', $value);
  305. }
  306. /**
  307. * @desc Crea el enlace hacia al foro apartir del metodo de urls
  308. * @param int $mode 0 por Defecto, 1 Basado en Nombres
  309. */
  310. public function makeLink(){
  311. $mc = RMUtilities::module_config('bxpress');
  312. if($mc['urlmode']){
  313. $link = XOOPS_URL.$mc['htbase'].'/forum.php?id='.$this->id();
  314. } else {
  315. $link = XOOPS_URL.'/modules/bxpress/';
  316. $link .= "forum.php?id=".$this->id();
  317. }
  318. return $link;
  319. }
  320. public function permalink(){
  321. return $this->makeLink();
  322. }
  323. /**
  324. * @desc Permite saber si un usuario cuenta con permisos
  325. * especificos en el foro dependiendo de su grupo
  326. * @param int,array Id del grupo
  327. * @param string $type Tipo de permiso (view,topic,reply,edit,delete,vote,attach,approve)
  328. * @return bool
  329. */
  330. public function isAllowed($gid, $type){
  331. if ($type=='') return false;
  332. $perms =& $this->permissions();
  333. if (!isset($perms[$type])) return false;
  334. // Comprobamos si "Todos" esta activo
  335. if (in_array(0, $perms[$type])) return true;
  336. if (!is_array($gid)){
  337. // Comprobamos si el grupo de usuario es "Administradores"
  338. if ($gid==XOOPS_GROUP_ADMIN) return true;
  339. return in_array($gid, $perms[$type]);
  340. }
  341. if (in_array(XOOPS_GROUP_ADMIN, $gid)) return true;
  342. foreach ($gid as $id){
  343. if (in_array($id, $perms[$type])) return true;
  344. }
  345. return false;
  346. }
  347. /**
  348. * @desc Determina si un usuario es moderador del foro
  349. * @param int Id del Usuario
  350. * @return bool
  351. */
  352. public function isModerator($id){
  353. if ($id<=0) return false;
  354. $moderators = $this->moderators();
  355. if (!is_array($moderators)) return false;
  356. return in_array($id, $moderators);
  357. }
  358. /**
  359. * @desc Obtiene todos los temas del foro
  360. * @param int $level 0 = Sticky, 1 = Sticky y Multiples, 2 = Todos
  361. */
  362. public function getTopics($level = 2){
  363. $tbl1 = $this->db->prefix("bxpress_topics");
  364. $tbl2 = $this->db->prefix("bxpress_forumtopics");
  365. $sql = "SELECT tbl1.* FROM $tbl1,$tbl2 WHERE $tbl2.forum='".$this->id()."' AND
  366. $tbl1.id_topic=$tbl2.topic ORDER BY sticky, date DESC";
  367. }
  368. /**
  369. * @desc Obtenemos el último mensaje del foro
  370. **/
  371. public function getLastPost(){
  372. $post = 0;
  373. $sql = "SELECT a.* FROM ".$this->db->prefix('bxpress_posts')." a INNER JOIN ".$this->db->prefix('bxpress_topics')." b ON ";
  374. $sql.= " (a.id_topic=b.id_topic AND a.id_forum=".$this->id()." AND b.approved=1) ORDER BY a.post_time DESC";
  375. $result = $this->db->query($sql);
  376. while ($rows = $this->db->fetchArray($result)){
  377. $post = $rows['id_post'];
  378. break;
  379. }
  380. return $post;
  381. }
  382. public function save(){
  383. if ($this->isNew()){
  384. return $this->saveToTable();
  385. } else {
  386. return $this->updateTable();
  387. }
  388. }
  389. /**
  390. * @desc Elimina un foro junto con sus temas y mensajes
  391. */
  392. public function delete(){
  393. $sql = "SELECT * FROM ".$this->db->prefix("bxpress_topics")." WHERE id_forum='".$this->id()."'";
  394. $result = $this->db->query($sql);
  395. while ($row = $this->db->fetchArray($result)){
  396. $topic = new bXTopic();
  397. $topic->assignVars($row);
  398. $topic->delete();
  399. }
  400. return $this->deleteFromTable();
  401. }
  402. }
  403. /**
  404. * @desc Manejador para la tabla de foros
  405. */
  406. class bXForumHandler
  407. {
  408. private $db;
  409. private $table = '';
  410. function __construct(){
  411. $this->db = XoopsDatabaseFactory::getDatabaseConnection();
  412. $this->table = $this->db->prefix("bxpress_forums");
  413. }
  414. /**
  415. * @desc Obtiene todos los foros de la base de datos
  416. * @param int $parent Id del foro raíz
  417. * @param int $category Identificador de la categoría. 0 indica todas
  418. * @param int $activo -1 devuelve todos los foros, 0 solo los inactivos y 1 solo activos
  419. * @param bool $object Indica si se devueven los objetos bXForum
  420. */
  421. public function getForums($category=0, $active=-1, $object = false){
  422. $db = XoopsDatabaseFactory::getDatabaseConnection();
  423. $sql = "SELECT * FROM ".$db->prefix("bxpress_forums")." WHERE ".($active>-1 ? " active='$active' " : '').
  424. ($category>0 ? " AND cat='$category' " : '')." ORDER BY `cat`,`order`";
  425. $result = $db->queryF($sql);
  426. $retorno = array();
  427. while ($row = $db->fetchArray($result)){
  428. if ($object){
  429. $forum = new bXForum();
  430. $forum->assignVars($row);
  431. $retorno[] = $forum;
  432. } else {
  433. $retorno[] = $row;
  434. }
  435. }
  436. return $retorno;
  437. }
  438. /**
  439. * @desc Obtiene los foros ordenados en padres e hijos
  440. * @param array $retorno Referencia al arreglo que se llenará
  441. * @param int $parent Id del foro en el cual se empiezan a buscar hijos
  442. * @param int $category Id de la categoria. 0 Indica todas
  443. * @param int $saltos Número de saltos que se asignarán al nivel actual
  444. * @param int $activo -1 devuelve todos los foros, 0 solo los inactivos y 1 solo activos
  445. * @param bool $object Indica si se devueven los objetos bXForum
  446. */
  447. private function getForumsTree($retorno, $parent = 0, $category=0, $saltos = 0, $active = -1, $object = false){
  448. $sql = "SELECT * FROM $this->table WHERE `parent`='$parent'".($active>-1 ? " AND active='$active' " : '').
  449. ($category>0 ? " AND cat='$category' " : '')." ORDER BY `cat`,`order`";
  450. $result = $this->db->queryF($sql);
  451. while ($row = $this->db->fetchArray($result)){
  452. if ($object){
  453. $forum = new bXForum();
  454. $forum->assignVars($row);
  455. $retorno[] =& $forum;
  456. } else {
  457. $retorno[] = $row;
  458. }
  459. $this->getForumsTree($retorno, $row['id_forum'], $category, $saltos + 1, $active, $object);
  460. }
  461. }
  462. }