source: trunk/home/portalhelper.php @ 1820

Revision 1820, 9.6 KB checked in by Xiping.Wang, 21 months ago (diff)

[trunk]merge the code from trunk

Line 
1<?php
2require_once( dirname(__file__) . '/../common/omconfig.php' );
3require_once( dirname(__file__) . '/../common/json_service.php' );
4require_once( FS_COMMON_SKIN_DIRECTORY . '/common.php' );
5require_once( FS_COMMON_DIRECTORY.'/utils.php');
6
7require_once('includes/config.php');                                                                                             
8require_once ('includes/classes/DB.php' );
9
10require_once('portlets/PortletsClass.php');
11
12
13class PortalHelper {
14
15        private $bIsLoggedOn;
16        private $oDb;   //Wiki DB Connection
17        private $bDb;   //Blog DB Connection
18        private $mDb;   //Bookmark DB Connection
19        private $fDb;   //Forum DB Connection
20
21        public function __construct(){
22
23                $this->bIsLoggedOn = GetLoggedInStateFromWikiCookies();
24
25                $this->oDb          = &newDb( 'wiki',true );
26                $this->bDb          = &newDb( 'blogs',true);
27                $this->mDb          = &newDb( 'bookmarks',true );
28                $this->fDb          = &newDb( 'forum',true );
29                if( $this->bIsLoggedOn ){
30                        $this->iUserId      = $_COOKIE[ OMWIKI_DATABASE_NAME . '_' . OMWIKI_DB_TABLE_PREFIX . 'UserID' ];
31                        $this->sUserName    = $_COOKIE[ OMWIKI_DATABASE_NAME . '_' . OMWIKI_DB_TABLE_PREFIX . 'UserName' ];
32                }
33                session_start();
34        }
35
36        private function loadHomeData(){
37                $SkillPortlet = new SkillPortlet($this->oDb,$this->bDb,$this->mDb,$this->fDb);
38                $ActivityPortlet = new ActivityPortlet($this->oDb,$this->bDb,$this->mDb,$this->fDb);
39                //              $ContributionStatistics = new ContributionStatistics('', false);
40                //              $ContributionStatistics -> setExistingDBConnection($this->oDb,$this->bDb,$this->mDb,$this->fDb);
41                if ($this->bIsLoggedOn){
42                        //recent activity -> my contribution
43                        if(!isset($_SESSION['PrivateActivity']))
44                                $_SESSION['PrivateActivity'] = $ActivityPortlet->loadPrivateActivity(10);
45
46                        //community skills ->update log
47                        if(!isset($_SESSION['Activity']))
48                                $_SESSION['Activity'] = $SkillPortlet->loadActivity(10);
49                        //community skills ->skill rating
50                        if(!isset($_SESSION['RatingLog']))
51                                $_SESSION['RatingLog'] = $SkillPortlet->loadRatingLog(10);
52                }
53        }
54
55    // Changed to make enable option work in omcollab_portlets table by zhongzhen on 2010-07-29
56        public function loadLayout(){
57                if( $this->bIsLoggedOn ){
58                        $sSql = 'SELECT ol.portlet_name as i, ol.col as c, ol.tab as t '
59                        .'    FROM ' . OMWIKI_DB_TABLE_PREFIX . 'omcollab_layout as ol, ' . OMWIKI_DB_TABLE_PREFIX . 'omcollab_portlets as op '
60                        .'    WHERE ol.user_id=' . $this->iUserId . ' AND ol.portlet_name=op.name AND op.enable=1'
61                        .'        ORDER BY ol.seq';
62                } else
63                $sSql = 'SELECT ol.portlet_name as i, ol.col as c, ol.tab as t '
64                .'    FROM ' . OMWIKI_DB_TABLE_PREFIX . 'omcollab_layout as ol, ' . OMWIKI_DB_TABLE_PREFIX . 'omcollab_portlets as op '
65                .'    WHERE ol.user_id=0 AND ol.portlet_name=op.name AND op.enable=1'
66                .'        ORDER BY ol.seq';
67                $rows = $this->oDb->GetRows( $sSql );
68
69                if($rows==null){
70                        $sSql = 'SELECT ol.portlet_name as i, ol.col as c, ol.tab as t '
71                        .'    FROM ' . OMWIKI_DB_TABLE_PREFIX . 'omcollab_layout as ol, ' . OMWIKI_DB_TABLE_PREFIX . 'omcollab_portlets as op '
72                        .'    WHERE ol.user_id=0 AND ol.portlet_name=op.name AND op.enable=1'
73        .'      ORDER BY ol.seq';
74                        $rows = $this->oDb->GetRows( $sSql );
75                }
76                // create a new instance of Services_JSON
77                $json = new Services_JSON();
78                $output = $json->encode($rows);
79                return $output;
80        }
81
82        public function saveLayout($layout){
83                if( $this->bIsLoggedOn ){
84                        //remove old layout
85                        $sSql = 'DELETE FROM '. OMWIKI_DB_TABLE_PREFIX . 'omcollab_layout WHERE user_id='.$this->iUserId;
86                        $rows = $this->oDb->ExecuteSql($sSql);
87
88                        //save new layout
89                        $seq=1;
90                        foreach($layout as $record => $obj){
91                                $sSql = 'insert into '. OMWIKI_DB_TABLE_PREFIX . 'omcollab_layout(user_id,portlet_name,col,seq,tab)'
92                                .'    values('.$this->iUserId.',"'.$obj->i.'","'.$obj->c.'",'.$seq.',"'.$obj->t.'")';
93                                       
94                                $this->oDb->ExecuteSql($sSql);
95                                $seq++;
96                        }
97                }
98        }
99   
100    // Changed to make enable option work in omcollab_portlets table by zhongzhen on 2010-07-29
101        public function loadPortlets(){
102                $this->loadHomeData();
103                $sSql = 'SELECT name,url as l,title as t, color as c '
104                .'    FROM '. OMWIKI_DB_TABLE_PREFIX . 'omcollab_portlets'
105        .'    WHERE enable=1'
106                .'        ORDER BY name';
107                $rows = $this->oDb->GetRows( $sSql );
108
109
110
111                $output = '({';
112
113                $i = 0;
114                foreach( $rows as $row ){
115                        $output .= ''.$row['name'].':{';
116                        $output .= '"l":"'.$row['l'].'",';
117                        $output .= '"t":"'.$row['t'].'",';
118                        $output .= '"c":"'.$row['c'].'"';
119                        $output .= '}';
120
121                        //fix for IE to remove error message "Expected identifier, string or number"
122                        $i++;
123                        if($i<count($rows)) $output .= ',';
124                }
125
126                $output .= '})';
127
128                return $output;
129        }
130
131        public function loadTabs(){
132                $sSql = 'SELECT * '
133                .'    FROM '. OMWIKI_DB_TABLE_PREFIX . 'omcollab_tabs';
134
135                $rows = $this->oDb->GetRows( $sSql );
136
137                $output = '({';
138                $i = 0;
139                foreach( $rows as $row ){
140                        $output .= $row['tab_name'].':{';
141                        $output .= 'c1:"'.$row['c1'].'",';
142                        $output .= 'c2:"'.$row['c2'].'",';
143                        $output .= 'c3:"'.$row['c3'].'",';
144                        $output .= 'help:'.$row['helper'].'';
145                        $output .= '}';
146
147                        $i++;
148                        if($i<count($rows)) $output .= ',';
149                }
150                $output .= '})';
151                return $output;
152        }
153
154        public function showPortletPanel(){
155
156                if( $this->bIsLoggedOn )
157                //Show unused portlets
158                $sSql = 'SELECT name,url as l,title as t, color as c '
159                .'    FROM '. OMWIKI_DB_TABLE_PREFIX . 'omcollab_portlets'
160                .'        WHERE name NOT IN ('
161                . '                     SELECT portlet_name'
162                .'                FROM '. OMWIKI_DB_TABLE_PREFIX . 'omcollab_layout'
163                .'               WHERE user_id='.$this->iUserId .')'
164                .'        ORDER BY name';
165                else
166                $sSql = 'SELECT name,url as l,title as t, color as c '
167                .'    FROM '. OMWIKI_DB_TABLE_PREFIX . 'omcollab_portlets'
168                .'        ORDER BY name';
169
170                $rows = $this->oDb->GetRows( $sSql );
171                $output = '';
172                $i = 1;
173                foreach( $rows as $row ){
174                        $output .= "<td><a href='#' class='portlets' style='text-decoration:none;'><img src='home/portlets/{$row['name']}/logo.png'/><br/>{$row['name']}</a></td>";
175                        //let's have 2 items in each line
176                        if ($i % 2==0) {
177                                $output .= '</tr>'; $output .= '<tr>';
178                        }
179                        $i++;
180                }
181                if($output =='') $output = '<td>You have all portlets on your desktop.</td>';
182
183                $dialog = "
184<div id='portlet-dialog' title='Add a portlet to your desktop'>
185        <table width='100%'>
186                <tr>
187                {$output}
188                </tr>
189        </table>       
190</div>
191<script type='text/javascript'>
192        //remove present portlets , it works for anonymous user
193        jQuery('.portlets').each(function(){
194                var p = jQuery(this).text();
195                var is_found = false;
196        jQuery('.portlet_', '#main').each(function(){
197            if(this.id==p) {
198                is_found = true;
199            }
200        });
201        if(is_found) jQuery(this).parent().empty();
202    });
203    //show information 
204        if (jQuery('.portlets').size()==0) {
205                jQuery('#portlet-dialog td:first').text('You have all portlets on your desktop.');
206        }                       
207   
208        jQuery('.portlets').click(function(){
209        var p = jQuery(this).text();
210       
211        var is_found = false;
212        jQuery('.portlet_', '#main').each(function(){
213            if(this.id==p) {
214                is_found = true;
215            }
216        });
217
218        if(!is_found) {
219                    m = _modules[p];
220               
221                //add portlet
222                jQuery('#c3').addModule({id:p,
223                                title:m.t,
224                                url:m.l,
225                                color:m.c||null});
226                     //save layout
227             jQuery('#'+p).css('display', 'block');
228            saveLayout();
229                //hide the dialog
230                jQuery('#portlet-dialog').dialog('destroy').remove();
231        }
232    });
233   
234    jQuery('#portlet-dialog').dialog();
235</script>";
236                return $dialog;
237
238        }
239}
240
241$portalhelper = new PortalHelper();
242
243if(OMCOLLAB_CACHE_DISABLED){
244        $cache_file_name = './omcache/WelcomePage.html';
245        if(file_exists($cache_file_name)){
246                $next_cache_edit_time = filemtime($cache_file_name) + 60*OMCOLLAB_CACHE_UPDATED_INTERVAL;
247                if($next_cache_edit_time <= time()){
248                        //initiate and send post
249                        $url =  getHostName()."/home/home_cache_cron.php?key=".urlencode(OMCOLLAB_CACHE_KEY);
250                        $options = array( CURLOPT_RETURNTRANSFER => true,     // return web page
251                        CURLOPT_HEADER         => false,    // don't return headers
252                        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
253                        CURLOPT_ENCODING       => "",       // handle all encodings
254                        CURLOPT_USERAGENT      => "spider", // who am i
255                        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
256                        CURLOPT_CONNECTTIMEOUT => 60,      // timeout on connect
257                        CURLOPT_TIMEOUT        => 60,      // timeout on response
258                        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
259                        );
260
261                        //initiate and send post
262                        $ch      = curl_init( $url );
263                        curl_setopt_array( $ch, $options );
264                        $content = curl_exec( $ch );
265                        $err     = curl_errno( $ch );
266                        $errmsg  = curl_error( $ch );
267                        $header  = curl_getinfo( $ch );
268                        curl_close( $ch );
269                }
270        }
271}
272
273if( isset($_GET['action']) ){
274        $action = $_GET['action'];
275        if ($action == 'loadLayout'){
276                echo $portalhelper->loadLayout();
277        }
278        if ($action == 'loadPortlets'){
279                echo $portalhelper->loadPortlets();
280        }
281        if ($action == 'loadTabs'){
282                echo $portalhelper->loadTabs();
283        }
284        if ($action == 'showPortletPanel'){
285                echo $portalhelper->showPortletPanel();
286        }
287}
288
289if( isset($_POST['action']) ){
290
291        $action = $_POST['action'];
292        if ($action == 'saveLayout'){
293                $json = new Services_JSON();
294
295                //decode javascript string
296                //xiping.wang at gmail.com 2010-01-16
297                //fix ticket #229
298                $layout = $json->decode(stripslashes($_POST['layout']));
299
300                //Save to database
301                $portalhelper->saveLayout($layout);
302        }
303}
304?>
Note: See TracBrowser for help on using the repository browser.