/administrator/components/com_mysms/mysms.group.php

https://gitlab.com/mattyhead/mysms · PHP · 285 lines · 168 code · 58 blank · 59 comment · 22 complexity · a6e739a800ddbf6dcc6ef87a4e90c53b MD5 · raw file

  1. <?php
  2. /**
  3. * MySMS - Simple SMS Component for Joomla
  4. *
  5. * Axel Sauerhoefer < mysms[at]quelloffen.com >
  6. *
  7. * http://www.willcodejoomlaforfood.de
  8. *
  9. * $Author: axel $
  10. * $Rev: 203 $
  11. * $HeadURL: svn://willcodejoomlaforfood.de/mysms/trunk/administrator/components/com_mysms/mysms.group.php $
  12. *
  13. * $Id: mysms.group.php 203 2010-02-04 18:59:38Z axel $
  14. *
  15. * All rights reserved.
  16. *
  17. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  18. * MySMS! is free software. This version may have been modified pursuant
  19. * to the GNU General Public License, and as distributed it includes or
  20. * is derivative of works licensed under the GNU General Public License or
  21. * other free or open source software licenses.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  26. *
  27. **/
  28. //check if joomla call us
  29. defined( '_JEXEC' ) or die( 'Restricted access' );
  30. if( defined( 'MYSMS_BACKEND_GROUPS_PHP' ) == true )
  31. {
  32. return;
  33. }
  34. /**
  35. * Define our class constant to precent multipe definition
  36. */
  37. define( 'MYSMS_BACKEND_GROUPS_PHP', 1 );
  38. /**
  39. * mySMS Group class
  40. *
  41. * @package MySMS
  42. * @subpackage Util
  43. **/
  44. class mySMSGroup {
  45. var $_id; //id of the group unique
  46. var $_ownerID; //owner id
  47. var $_name; //group name
  48. var $_members;//group members
  49. var $_db; //refernce to the global db object
  50. /**
  51. * The constructor creates a new user group
  52. *
  53. **/
  54. function mySMSGroup()
  55. {
  56. $this->_id = -99;
  57. $this->_name = '';
  58. $this->_members = array();
  59. $this->_db = &JFactory::getDBO();
  60. }
  61. function init($idORName)
  62. {
  63. //prevent null values
  64. if( is_null($idORName ) )
  65. {
  66. return;
  67. }
  68. if( is_numeric($idORName) )
  69. {
  70. $sql = "SELECT COUNT(*) AS COUNT from #__mysms_groups WHERE id='$idORName'";
  71. }else{
  72. $sql = "SELECT COUNT(*) AS COUNT from #__mysms_groups WHERE name='$idORName'";
  73. }
  74. $this->_db->setQuery($sql);
  75. if( $this->_db->query() === false )
  76. {
  77. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  78. die;
  79. }
  80. $g = $this->_db->loadObject();
  81. //we can only create our group if we have a name, checkup
  82. if( !is_numeric( $idORName ) )
  83. {
  84. //group not exists, create it, and call us again
  85. if( $g->COUNT <= 0 )
  86. {
  87. $my = &JFactory::getUser();
  88. $id = $my->get('id');
  89. $sql = "INSERT INTO #__mysms_groups VALUES(0, '$idORName', $id )";
  90. $this->_db->setQuery($sql);
  91. if( $this->_db->query() === false )
  92. {
  93. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  94. die;
  95. }
  96. }
  97. }
  98. if( is_numeric( $idORName ) )
  99. {
  100. $sql = "SELECT * from #__mysms_groups WHERE id='$idORName'";
  101. }else{
  102. $sql = "SELECT * from #__mysms_groups WHERE name='$idORName'";
  103. }
  104. $this->_db->setQuery($sql);
  105. if( $this->_db->query() === false )
  106. {
  107. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  108. die;
  109. }
  110. unset($g);
  111. $g = $this->_db->loadObject();
  112. $this->_ownerID = $g->ownerid;
  113. $this->_name = $g->name;
  114. $this->_id = $g->id;
  115. //now collect all user in groups from #__mysms_usergroups
  116. $sql = "SELECT * FROM #__mysms_usergroups WHERE groupid=". $this->_id;
  117. $this->_db->setQuery($sql);
  118. if( $this->_db->query() === false )
  119. {
  120. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  121. die;
  122. }
  123. $lst = $this->_db->loadObjectList();
  124. //now we have member id, we need name, from our table
  125. //collect a user in our group
  126. foreach($lst as $l)
  127. {
  128. $sql ="SELECT id, name, number FROM #__mysms_phonebook WHERE id=$l->memberid";
  129. $this->_db->setQuery($sql);
  130. if( $this->_db->query() === false )
  131. {
  132. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  133. die;
  134. }
  135. $u = $this->_db->loadObject();
  136. $this->_members[$l->id] = $u;
  137. unset($u);
  138. //data structure
  139. //unique usergroups id --> object ( id, name, number ) phonebook
  140. }
  141. }
  142. function addMember($id)
  143. {
  144. //given id, is the id from the phonebook entry !!!
  145. //check input
  146. if( !is_numeric( $id ) )
  147. {
  148. return false;
  149. }
  150. $my = &JFactory::getUser();
  151. //check if given id is a user phonebook entry
  152. $sql = "SELECT COUNT(*) AS COUNTER FROM #__mysms_phonebook WHERE id=$id AND ownerid=". $my->get('id');
  153. $this->_db->setQuery($sql);
  154. if( $this->_db->query() === false )
  155. {
  156. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  157. die;
  158. }
  159. $u = $this->_db->loadObject();
  160. //given id is invalid
  161. if( $u->COUNTER <= 0 )
  162. {
  163. return false;
  164. }
  165. //insert new entry
  166. $sql = "INSERT INTO #__mysms_usergroups VALUES(0, $id, $this->_id)";
  167. $this->_db->setQuery($sql);
  168. if( $this->_db->query() === false )
  169. {
  170. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  171. die;
  172. }
  173. return true;
  174. }
  175. function deleteMember( $id )
  176. {
  177. //check input
  178. if( !is_numeric( $id ) )
  179. {
  180. return false;
  181. }
  182. $my = &JFactory::getUser();
  183. //check if given id is a user phonebook entry
  184. $sql = "SELECT COUNT(*) AS COUNTER FROM #__mysms_phonebook WHERE id=$id AND ownerid=". $my->get('id');
  185. $this->_db->setQuery($sql);
  186. if( $this->_db->query() === false )
  187. {
  188. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  189. die;
  190. }
  191. $u = $this->_db->loadObject();
  192. //given id is invalid
  193. if( $u->COUNTER <= 0 )
  194. {
  195. return false;
  196. }
  197. //insert new entry
  198. $sql = sprintf( "DELETE FROM #__mysms_usergroups where memberid=%s and groupid=%s LIMIT 1", $id, $this->_id );
  199. $this->_db->setQuery($sql);
  200. if( $this->_db->query() === false )
  201. {
  202. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  203. die;
  204. }
  205. return true;
  206. }
  207. function delete()
  208. {
  209. //first delete all entries from #__mysms_usergroups
  210. $sql ="DELETE FROM #__mysms_usergroups WHERE groupid=$this->_id";
  211. $this->_db->setQuery($sql);
  212. if( $this->_db->query() === false )
  213. {
  214. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  215. die;
  216. }
  217. //now delete group itself from #__mysms_groups
  218. $sql ="DELETE FROM #__mysms_groups WHERE id=$this->_id";
  219. $this->_db->setQuery($sql);
  220. if( $this->_db->query() === false )
  221. {
  222. mySMSError::Alert( JText::_( 'MYSMS_SQLQUERY_ERROR' ) );
  223. die;
  224. }
  225. }
  226. }
  227. ?>