root/branch/1.5.x/plugins/jomsocial/mysms_jomsocial.php

Revision 291, 8.0 KB (checked in by axel, 20 months ago)

Update to new group concept and newsletter admin handling °!

Line 
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: $
10* $Rev: $
11* $HeadURL: $
12*
13* $Id:  $
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
29// Check to ensure this file is included in Joomla!
30defined('_JEXEC') or die( 'Restricted access' );
31
32jimport('joomla.plugin.plugin');
33
34$mainframe->registerEvent( 'onLoadContacts', 'plgMySMSJomSocial' );
35
36class plgMySMSJomSocial extends JPlugin
37{
38        var $db;
39        var $user;
40        var $params;
41        var $contacts;
42                       
43        function plgMySMSJomSocial( &$subject ) 
44        {               
45                parent::__construct($subject);
46               
47                $this->db   = &JFactory::getDBO();
48                $this->user = &JFactory::getUser(); 
49               
50                $plugin =& JPluginHelper::getPlugin( 'mysms', 'mysms_jomsocial');
51                $this->params = new JParameter( $plugin->params );     
52
53                $obj  = &JFactory::getLanguage();                       
54                $basepath = dirname(__FILE__). DS . '..' . DS . '..' . DS . 'administrator';
55                $obj->load( 'plg_mysms_jomsocial', $basepath );
56        }
57       
58        function FilterBlocked( $data )
59        {                               
60                $sql = 'SELECT userid FROM jos_community_blocklist WHERE blocked_userid=%s';           
61                $sql = sprintf( $sql, $this->user->id );
62               
63                $this->db->setQuery( $sql );           
64                $blocklist = $this->db->loadAssocList ();
65               
66                $result = array();
67               
68                foreach ( $data as $set )
69                {
70                        //-- filter out if someone else blocked me, all i blocked are filterd by the sql statment above
71                       
72                        $block = false;
73                       
74                        foreach( $blocklist as $b )
75                        {
76                                if( $b['userid'] == $set['id'] )
77                                {
78                                        $block = true;
79                                        break;
80                                }
81                        }
82                       
83                        if( $block == true )
84                        {
85                                continue;
86                        }
87                       
88                        array_push( $result, $set );                   
89                }               
90               
91                return $result;
92        }
93       
94        function FilterNoSMSNewsletterField( $data )
95        {
96                $NoSMSNewsletterFiled   = $this->params->get( 'NoSMSNewsletterFiled' );
97
98                if( strlen( $NoSMSNewsletterFiled ) == 0 )
99                {
100                        return $data;
101                }
102               
103                $blockVal = JText::_( 'PLG_MYSMS_JOMSOCIAL_BLOCK_NEWSLETTER_NO' );
104               
105                $sql = 'SELECT user_id as userid
106                                FROM #__community_fields_values
107                                WHERE  field_id = ( SELECT id FROM jos_community_fields where fieldcode = \'%s\' AND UPPER( VALUE ) = UPPER( \'%s\' ) )';
108                               
109                $sql = sprintf( $sql, $NoSMSNewsletterFiled, $blockVal );
110               
111                $this->db->setQuery( $sql );
112               
113                $blocklist = $this->db->loadAssocList();
114                $result = array();
115                               
116                foreach ( $data as $set )
117                {
118                        $block = false;
119                       
120                        foreach( $blocklist as $b )
121                        {                                       
122                                //found blocked user
123                                if( $set['id'] == $b['userid'] )
124                                {
125                                        $block = true; 
126                                        break;
127                                }
128                        }                       
129                       
130                        if( $block == true )
131                        {
132                                continue;
133                        }
134                       
135                       
136                        array_push( $result, $set );                   
137                }       
138
139                return $result;                 
140        }
141       
142       
143        function loadContacts()
144        {
145                $PhoneFiled                     = $this->params->get( 'PhoneField' );
146                                               
147                //Special Case, the newsletter admin is able to load all data
148                //Also from blocked contacts
149                if( $this->IsNewsletterAdmin() )
150                {
151                        $sql = 'SELECT A.user_id as id, B.username as name, A.value as number
152                                    FROM #__community_fields_values A LEFT JOIN #__users B ON ( A.user_id = B.id )
153                                    WHERE  A.field_id = ( SELECT id FROM #__community_fields where fieldcode = \'%s\' ) AND
154                                    B.block = 0 AND A.value != \'\''; 
155                       
156                         $sql = sprintf( $sql, $PhoneFiled );           
157                                                                       
158                }else{
159                        //Default user handling respect blocked state
160                        $sql = 'SELECT A.user_id as id, B.username as name, A.value as number
161                                        FROM #__community_fields_values A LEFT JOIN #__users B ON ( A.user_id = B.id )
162                                        WHERE  A.field_id = ( SELECT id FROM #__community_fields where fieldcode = \'%s\' ) AND
163                                        B.block = 0 AND A.value != \'\' AND
164                                        A.user_id NOT IN ( SELECT blocked_userid FROM jos_community_blocklist WHERE userid = %s )';
165
166                        $sql = sprintf( $sql, $PhoneFiled, $this->user->id  );
167                }
168                                                                       
169                $this->db->setQuery( $sql );
170                $data = $this->db->loadAssocList ();
171               
172                //Filter against blocked user, only ignore this filter if we are the newsletter admin           
173                if( ! $this->IsNewsletterAdmin() )
174                {
175                        $data = $this->FilterBlocked( $data );
176                }
177               
178                //Filter against blocked newsletter
179                $this->contacts = $this->FilterNoSMSNewsletterField( $data );                           
180                return $this->contacts;
181        }
182       
183        function IsVaildContact( $id )
184        {               
185                if( !is_array($this->contacts))
186                {
187                        return false;
188                }
189               
190                foreach( $this->contacts as $c )
191                {                       
192                        if( $c['id'] == $id )
193                        {
194                                return true;
195                        }                       
196                }
197               
198                return false;           
199        }
200       
201        function loadGroups()
202        {
203                $result = array();
204               
205                if( false == $this->IsNewsletterAdmin() )
206                {
207                        return;
208                }
209               
210                $sql = 'SELECT A.groupid, B.name  FROM #__community_groups_members A
211                                LEFT JOIN #__community_groups B ON ( A.groupid = B.ID ) WHERE memberid=%s';
212               
213                $sql = sprintf( $sql, $this->user->id );
214               
215                $this->db->setQuery( $sql );
216                $groups = $this->db->loadAssocList();
217                                                                               
218                //special handling for all contacts
219                $all    = array( 'id'     => 'plugin_1111111',
220                                                 'name'   => 'newsletter',
221                                             'member' => array() );     
222               
223                foreach( $this->contacts as $c )
224                {
225                        array_push( $all['member'], array( 'id' => $c['id'] ) );                       
226                }
227                               
228                array_push( $result , $all );           
229               
230                foreach( $groups as $g )
231                {
232                        $sql = 'SELECT memberid FROM #__community_groups_members WHERE groupid=%s';
233                        $sql = sprintf( $sql, $g['groupid'] );
234                                               
235                        $this->db->setQuery( $sql );
236                        $members = $this->db->loadAssocList();
237                       
238                        $set = array( 'id'     => $g['groupid'],
239                                                  'name'   => $g['name'],
240                                                  'member' => array() );
241                       
242                        foreach($members as $m )
243                        {
244                                if( $this->IsVaildContact( $m['memberid'] ) )
245                                {
246                                        array_push( $set['member'], array( 'id' => $m['memberid'] ) );
247                                }                               
248                        }
249
250                        //push only the group if we have more than 0 contacts, so no empty group to show
251                        if( count( $set['member'] )  > 0 )
252                        {
253                                array_push( $result, $set );
254                        }                       
255                }
256                               
257                return $result;         
258        }
259       
260        function onLoadContacts()
261        {
262               
263                $this->InitMySMSUserNumber();
264                /**
265                 * $dummy = array( 'contacts' => array(  array( 'id'       => 1,
266                                                                                                 'number'  => '1234',
267                                                                                             'name'    => 'JomSocial' ),
268                                                                                  array( 'id'      => 2,
269                                                                                                 'number'  => '5678',
270                                                                                             'name'    => 'JomSocial' ) ),
271                                                                                 
272                                         'groups'   => array(  array( 'id' => 1,
273                                                                                                  'name' => 'TestGroup',
274                                                                                          'member' => array( array( 'id' => 1 ),
275                                                                                                                                 array( 'id' => 2 ) ) ),
276                                                                                   )                                                                             
277                                        );     
278                                       
279                 */                                     
280                $result = array( 'contacts' => $this->loadContacts(),
281                                                 'groups'       => $this->loadGroups()  );
282               
283                return $result;         
284        }
285       
286        function IsNewsletterAdmin()
287        {
288                $NewsletterAdmin                = (int)$this->params->get( 'NewsletterAdmin' ); 
289                return $this->user->id == $NewsletterAdmin;
290        }
291       
292        function InitMySMSUserNumber()
293        {       
294                $sql = "SELECT number FROM #__mysms_joomlauser WHERE userid=%s";
295                $sql = sprintf( $sql, $this->user->id );
296                $this->db->setQuery( $sql );
297                $this->db->query( $sql );
298                $number = $this->db->loadResult();
299               
300                //a number is already setup
301                if( 0 < strlen( $number ) )
302                {
303                        return;
304                }
305
306                $PhoneFiled = $this->params->get( 'PhoneField' );               
307                $sql = 'SELECT value FROM jos_community_fields_values where user_id=%s AND field_id in ( SELECT id FROM #__community_fields WHERE fieldcode=\'%s\' )';
308                $sql = sprintf( $sql, $this->user->id, $PhoneFiled );
309                $this->db->setQuery( $sql );
310                $this->db->query( $sql );
311                $number = $this->db->loadResult();
312                       
313                if( 0 == strlen( $number ) ) //no number in jomsocial
314                {
315                        return;
316                } 
317               
318                $sql = "UPDATE #__mysms_joomlauser SET number='%s' WHERE userid=%s LIMIT 1";
319                $sql = sprintf( $sql, $number, $this->user->id );
320                $this->db->setQuery( $sql );
321                $this->db->query( $sql );
322        }
323               
324}
325?>
Note: See TracBrowser for help on using the browser.