• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

cms/widget.lib.php

Go to the documentation of this file.
00001 <?php
00002 if(!defined('__PRAGYAN_CMS'))
00003 { 
00004         header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
00005         echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
00006         echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
00007         exit(1);
00008 }
00108 function populateWidgetVariables($pageId)
00109 {
00110 
00111         global $cmsFolder,$widgetFolder,$WIDGETS;       
00112         $enwidgets=getEnabledWidgets($pageId);
00113         $inwidgets=getInheritedWidgets($pageId);
00114         $enwidgets=array_merge($enwidgets,$inwidgets);
00115         $widgetsenmap=array();
00116         
00117         foreach( $enwidgets as $enwidget )
00118         {
00119                 $classname=$enwidget['classname'];
00120                 require_once("$widgetFolder/{$enwidget['foldername']}/widget.class.php");
00121                 $widget = new $classname($enwidget['id'],$enwidget['instanceid'],$pageId);
00122                 if(!($widget instanceof widgetFramework))
00123                 {
00124                         displayerror("The widget {$enwidget['name']} doesn't extends widgetFramework class");
00125                         return "";
00126                 }
00127                 
00128                 $class_methods = get_class_methods($widget);
00129                 if(!in_array('initWidget',$class_methods) || !in_array('getHTML',$class_methods))
00130                 {
00131                         displayerror("The widget {$enwidget['name']} is not properly defined.");
00132                         return "";
00133                 }
00134                 
00135                 $widget->initWidget();
00136                 
00137                 if(!isset($widgetsenmap[$enwidget['id']]) || $widgetsenmap[$enwidget['id']]==false)
00138                 {
00139                         if(empty($WIDGETS[(int)$enwidget['location']]))
00140                           $WIDGETS[(int)$enwidget['location']] = $widget->getCommonHTML();
00141                         else $WIDGETS[(int)$enwidget['location']] .= $widget->getCommonHTML();
00142                         $widgetsenmap[$enwidget['id']]=true;
00143                 }
00144                 
00145                 $WIDGETS[(int)$enwidget['location']] .= $widget->getHTML();
00146         }
00147 }
00148 
00149 
00155 function handleWidgetPageSettings($pageId)
00156 {
00157         global $ICONS,$ICONS_SRC,$urlRequestRoot,$cmsFolder,$moduleFolder;
00158         
00162         $quicklinks ="
00163         <fieldset>
00164         <legend>{$ICONS['Widgets']['small']}Widgets</legend>
00165         <table class='iconspanel'>
00166         <tr>
00167         <td><a href='./+widgets#enabledwidgets'><div>{$ICONS['Widgets']['large']}<br/>Enabled Widgets for this Page</div></a></td>
00168         <td><a href='./+widgets#inheritedwidgets'><div>{$ICONS['Propagate']['large']}<br/>Inherited Widgets for this Page</div></a></td>
00169         <td><a href='./+widgets&subaction=enable'><div>{$ICONS['Add']['large']}<br/>Add More Widgets</div></a></td>
00170         </tr>
00171         </table>   
00172         </fieldset>
00173         ";
00174         $html = "";
00175         
00176         if(isset($_GET['subaction']) && $_GET['subaction']=='enable')
00177         {
00178                 if(isset($_GET['widgetid']))
00179                 {
00180                         $widgetId=escape($_GET['widgetid']);
00181                         createWidgetInstance($pageId,$widgetId);        
00182                 }
00183                 
00184                 $widgetsarr=getAllWidgetsInfo();
00185                 $allwidgets = "<fieldset><legend>{$ICONS['Add']['small']}Add Widgets</legend>";
00186         
00187                 $allwidgets .= "<table width=100%><tr><th colspan=4>Available Widgets<br/><i>Mouse over for description</i></th></tr>
00188                 <tr><th>Name</th><th>Version</th><th>Author</th><th>Add</th></tr>";
00189                 foreach( $widgetsarr as $widget )
00190                 {
00191                         $allwidgets.="\n<tr><td><a title='".$widget['description']."'>".$widget['name']."</a></td><td>{$widget['version']}</td><td>{$widget['author']}</td><td><a href='./+widgets&subaction=enable&widgetid={$widget['id']}'><img src='{$ICONS_SRC['Add']['large']}' title='Add an instance of this widget' /></a></td></tr>";
00192                 }
00193                 $allwidgets.="</table></fieldset>";
00194                 
00195                 $html.=$allwidgets;
00196         }
00197         if(isset($_GET['subaction']) && isset($_GET['widgetid']) && isset($_GET['widgetinstanceid']))
00198         {
00199                 $subaction=escape($_GET['subaction']);
00200                 $widgetid=escape($_GET['widgetid']);
00201                 $widgetinstanceid=escape($_GET['widgetinstanceid']);
00202                 
00203                 if($subaction=="config")
00204                 {
00206                         if(isset($_GET['subsubaction']) && $_GET['subsubaction']=="update")
00207                                 updateWidgetConf($widgetid,$widgetinstanceid,FALSE); 
00208                         
00209                         
00210                         $widgetinfo=getWidgetInfo($widgetid);
00211                         $widgetpageconfigs=getWidgetPageConfigInfo($widgetid);
00212                 
00214                         $containsFileUploadFields = false;
00215                         $formElements=getConfigFormAsArray($widgetpageconfigs,$containsFileUploadFields,$widgetinstanceid,FALSE);
00216                         
00217                         $jsPath = "$urlRequestRoot/$cmsFolder/templates/common/scripts/formValidator.js";//validation.js
00218                         $calpath = "$urlRequestRoot/$cmsFolder/$moduleFolder/form/calendar";
00219                         $jsPathMooTools = "$urlRequestRoot/$cmsFolder/templates/common/scripts/mootools-1.11-allCompressed.js";
00220                 
00221                         $html .= '<link rel="stylesheet" type="text/css" media="all" href="'.$calpath.'/calendar.css" title="Aqua" />' .
00222                                                          '<script type="text/javascript" src="'.$calpath.'/calendar.js"></script>';
00223                         $html .= '<fieldset><legend>'.$ICONS['Widgets']['small'].'Widget Page Settings</legend><div class="registrationform"><form class="fValidator-form" name="widgetpagesettings" action="./+widgets&subaction=config&subsubaction=update&widgetid='.$widgetid.'&widgetinstanceid='.$widgetinstanceid.'" method="post"';
00224                 
00225                         if($containsFileUploadFields)
00226                                 $html .= ' enctype="multipart/form-data"';
00227                         $html .= '>';
00228                 
00229                         $html.="<table class='pragyan_fulltable'><tr><th colspan=2>Widget : {$widgetinfo['name']}</th><tr>";
00230                         $html.="<tr><td>Description : </td><td> {$widgetinfo['description']}</td></tr>";
00231                         $html .="<tr>".join($formElements, "</tr>\n<tr>")."</tr>";
00232                 
00233                         $html.="</table><input name='update_widget_page_settings' type='submit' value='Update'/>" .
00234                                         "<input type='reset' value='Reset'/>";
00235                         $html.="</form><br/></fieldset>";
00236                 }
00237                 else if($subaction=="delete")
00238                         deleteWidgetInstance($widgetid,$widgetinstanceid);
00239                 else if($subaction=='propagate')
00240                         propagateWidgetInstance($widgetid,$widgetinstanceid);
00241                 else if($subaction=='unpropagate')
00242                         unpropagateWidgetInstance($widgetid,$widgetinstanceid);
00243         }
00244         if(isset($_GET['subaction']) && isset($_GET['subsubaction']) && isset($_GET['widgetid']) && isset($_GET['widgetinstanceid']) )
00245         {
00246                 $subaction=escape($_GET['subaction']);
00247                 $subsubaction=escape($_GET['subsubaction']);
00248                 $widgetid=escape($_GET['widgetid']);
00249                 $widgetinstanceid=escape($_GET['widgetinstanceid']);
00250                 
00251                 if($subaction=="location")
00252                 {
00253                         if($subsubaction=="up")
00254                         {
00255                                 modifyWidgetInstanceLocation($pageId,$widgetid,$widgetinstanceid,"-1");
00256                         }
00257                         if($subsubaction=="down")
00258                         {
00259                                 modifyWidgetInstanceLocation($pageId,$widgetid,$widgetinstanceid,"+1");
00260                         }
00261                 }
00262                 else if($subaction=="order")
00263                 {
00264                         if($subsubaction=="up")
00265                         {
00266                                 modifyWidgetInstanceOrder($pageId,$widgetid,$widgetinstanceid,"-1");
00267                         }
00268                         if($subsubaction=="down")
00269                         {
00270                                 modifyWidgetInstanceOrder($pageId,$widgetid,$widgetinstanceid,"+1");
00271                         }
00272                 }
00273         }
00274         
00275         $enabledwidgetsarr=getEnabledWidgets($pageId);
00276         
00277         $enabled = "<fieldset><legend>{$ICONS['Widgets']['small']}Enabled Widgets</legend><a name='enabledwidgets'></a>";
00278         $enabled .= "<table class='pragyan_fulltable'><tbody><tr><th colspan=4>Enabled Widgets <br/><i>in order of their appearance</i></th></tr>
00279         <tr><th>Widget</th><th>Location</th><th>Order</th><th>Actions</th></tr>";
00280         
00281         foreach ( $enabledwidgetsarr as $widget )
00282         {
00283                 $propagatebtn = "<a href='./+widgets&subaction=propagate&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'><img src='{$ICONS_SRC['Propagate']['small']}' title='Propagate : Add this widget to all the child pages recursively. Widget will retain its location.' /></a>";
00284                 $unpropagatebtn = "<a href='./+widgets&subaction=unpropagate&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'><img src='{$ICONS_SRC['Unpropagate']['small']}' title='Unpropagate : Remove the copies of this widget from all the child pages recursively.' /></a>";
00285                 
00286                 $configbtn = "<a href='./+widgets&subaction=config&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'><img src='{$ICONS_SRC['Edit']['small']}' title='Edit : Configure this instance of this widget' /></a>";
00287                 
00288                 $deletebtn = "<a href='./+widgets&subaction=delete&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'><img src='{$ICONS_SRC['Delete']['small']}' title='Delete : Delete this instance of this widget' /></a>";
00289                 $locationup = "<a href='./+widgets&subaction=location&subsubaction=up&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'><img src='{$ICONS_SRC['Up']['small']}' title='Move to an upper location' /></a>";
00290                 $locationdown = "<a href='./+widgets&subaction=location&subsubaction=down&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'><img src='{$ICONS_SRC['Down']['small']}' title='Move to a lower location' /></a>";
00291                 
00292                 $orderup = "<a href='./+widgets&subaction=order&subsubaction=up&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'><img src='{$ICONS_SRC['Up']['small']}' title='Move to an upper order' /></a>";
00293                 $orderdown = "<a href='./+widgets&subaction=order&subsubaction=down&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'><img src='{$ICONS_SRC['Down']['small']}' title='Move to a lower order' /></a>";
00294                 
00295                 if($widget['propagate']=='1') $propunpropbtn=$unpropagatebtn;
00296                 else $propunpropbtn=$propagatebtn;
00297                 
00298                 $enabled .= "\n<tr><td><a title='{$widget['description']}' href='./+widgets&subaction=config&widgetid={$widget['id']}&widgetinstanceid={$widget['instanceid']}'>{$widget['name']}</a></td><td>{$widget['location']} $locationup $locationdown</td><td>{$widget['order']} $orderup $orderdown</td><td>$configbtn $deletebtn $propunpropbtn</td></tr>";
00299                 
00300         }
00301         $enabled .="</tbody></table></fieldset>";
00302         $enabled .="<fieldset><legend>{$ICONS['Propagate']['small']}Inherited Widgets</legend><a name='inheritedwidgets'></a><b>Note: Inherited widgets can be configured from the origin page only and they are always ordered last in their location in the inherited pages.</b>";
00303         $enabled .="<table class='pragyan_fulltable'><tbody><tr><th colspan=3>Inherited Widgets <br/><i>due to propagation from a parent page</i></th></tr><tr><th>Widget</th><th>Location</th><th>Origin</th></tr>";
00304         
00305         $inheritedwidgetsarr=getInheritedWidgets($pageId);
00306         foreach ( $inheritedwidgetsarr as $widget )
00307         {
00308                 $link=hostURL().$widget['source'];
00309                 $enabled .= "\n<tr><td><a title='{$widget['description']}'>{$widget['name']}</a></td><td>{$widget['location']}</td><td><a href='$link'>{$widget['source']}</a></td></tr>";
00310         }
00311         $enabled .="</tbody></table>";
00312         
00313         
00314         
00315         $enabled .="</fieldset>";
00316         
00317         
00318         return $html.$enabled.$quicklinks;
00319         
00320 }
00326 function getInheritedWidgets($pageId)
00327 {
00328         $parentId=getParentPage($pageId);
00329         
00330         if($parentId==$pageId) return array();
00331         
00332         $query="SELECT t1.`widget_id` AS 'id', t1.`widget_instanceid` AS 'instanceid', t1.`widget_location` AS 'location', t2.`widget_name` AS 'name', t2.`widget_description` AS 'description', t2.`widget_author` AS 'author', t2.`widget_version` AS 'version', t2.`widget_classname` AS 'classname', t2.`widget_foldername` AS 'foldername' FROM `".MYSQL_DATABASE_PREFIX."widgets` AS t1, `".MYSQL_DATABASE_PREFIX."widgetsinfo` AS t2 WHERE t1.`page_id`=$parentId AND t1.`widget_propagate`=1 AND t2.`widget_id`=t1.`widget_id` ORDER BY t1.`widget_location` ASC";
00333         $result=mysql_query($query);
00334         $return=array();
00335         while($row=mysql_fetch_array($result))
00336         {
00337                 $row['source']=getPagePath($parentId);
00338                 $return[]=$row;
00339         }
00340         
00341         $more=getInheritedWidgets($parentId);
00342         $return=array_merge($return,$more);
00343         return $return;
00344 }
00345 
00351 function propagateWidgetInstance($widgetId,$widgetInstanceId)
00352 {
00353         $query="UPDATE `".MYSQL_DATABASE_PREFIX."widgets` SET `widget_propagate`=1 WHERE `widget_id`=$widgetId AND `widget_instanceid`=$widgetInstanceId";
00354         mysql_query($query);
00355         displayinfo("Widget has been succesfully propagated to all child pages recursively.");
00356 }
00357 
00363 function unpropagateWidgetInstance($widgetId,$widgetInstanceId)
00364 {
00365         $query="UPDATE `".MYSQL_DATABASE_PREFIX."widgets` SET `widget_propagate`=0 WHERE `widget_id`=$widgetId AND `widget_instanceid`=$widgetInstanceId";
00366         mysql_query($query);
00367         displayinfo("Widget copies has been succesfully removed from all child pages recursively.");
00368 }
00369 
00376 function deleteWidgetInstance($widgetId,$widgetInstanceId)
00377 {
00378         $query="DELETE FROM `".MYSQL_DATABASE_PREFIX."widgets` WHERE `widget_id`=$widgetId AND `widget_instanceid`=$widgetInstanceId";
00379         if(mysql_query($query)===FALSE)
00380         {
00381                 displayerror("Could not delete widget. Internal error occurred.");
00382                 return FALSE;
00383         }
00384         
00385         $query="DELETE FROM `".MYSQL_DATABASE_PREFIX."widgetsconfig` WHERE `widget_id`=$widgetId AND `widget_instanceid`=$widgetInstanceId";
00386         if(mysql_query($query)===FALSE)
00387         {
00388                 displayerror("Could not delete widget. Internal error occurred.");
00389                 return FALSE;
00390         }
00391         
00392         $query="DELETE FROM `".MYSQL_DATABASE_PREFIX."widgetsdata` WHERE `widget_id`=$widgetId AND `widget_instanceid`=$widgetInstanceId";
00393         if(mysql_query($query)===FALSE)
00394         {
00395                 displayerror("Could not delete widget. Internal error occurred.");
00396                 return FALSE;
00397         }
00398         
00399         displayinfo("Widget successfully deleted!");
00400         return TRUE;
00401         
00402 }
00403 
00413 function modifyWidgetInstanceLocation($pageId,$widgetId,$widgetInstanceId,$mod)
00414 {
00415         $query="UPDATE `".MYSQL_DATABASE_PREFIX."widgets` SET `widget_location`=`widget_location`$mod WHERE `page_id`=$pageId AND `widget_id`=$widgetId AND `widget_instanceid`=$widgetInstanceId AND `widget_location`$mod >= 0";
00416         $res=mysql_query($query);
00417         if(!$res) return false;
00418         if(mysql_affected_rows()==0)
00419                 displayerror("Could not move widget to that location. Location cannot be negative.");
00420         else displayinfo("Widget has been successfully relocated.");                    
00421 }
00422 
00431 function modifyWidgetInstanceOrder($pageId,$widgetId,$widgetInstanceId,$mod)
00432 {
00433         $query="UPDATE `".MYSQL_DATABASE_PREFIX."widgets` SET `widget_order`=`widget_order`$mod WHERE `page_id`=$pageId AND `widget_id`=$widgetId AND `widget_instanceid`=$widgetInstanceId";
00434         $res=mysql_query($query);
00435         if(!$res) return false;
00436         if(mysql_affected_rows()==0)
00437                 displayerror("Could not move widget. Some internal error occurred.");
00438         else displayinfo("Widget has been successfully reordered.");                    
00439 }
00440 
00448 function createWidgetInstance($pageId,$widgetId)
00449 {
00450         $query="SELECT `widget_name` AS 'name', `widget_classname` AS 'classname', `widget_foldername` AS 'foldername' FROM `".MYSQL_DATABASE_PREFIX."widgetsinfo` WHERE `widget_id`=$widgetId";
00451         $res=mysql_query($query);
00452         if(mysql_num_rows($res)==0)
00453         {
00454                 displayerror("Required widget is not registered with Pragyan CMS properly.");
00455                 return false;
00456         }
00457         $row=mysql_fetch_array($res);
00458         
00459         global $widgetFolder;
00460         $classname=$row['classname'];
00461         require_once("$widgetFolder/{$row['foldername']}/widget.class.php");
00462         
00464         $widget = new $classname($widgetId,-1,$pageId);
00465         if(!($widget instanceof widgetFramework))
00466         {
00467                 displayerror("The widget {$row['name']} doesn't extends widgetFramework class");
00468                 return false;
00469         }
00470         
00471         if(!$widget->validInstall())
00472         {
00473                 if(!$widget->installWidget())
00474                 {
00475                         displayerror("{$row['name']} widget is not installed properly.");
00476                         return false;
00477                 }
00478                 $widget->loadWidget();
00479         }
00480         
00481         $widgetLocation;
00482         $widgetOrder;
00483         
00485         $success=$widget->createWidget($pageId,$widgetLocation,$widgetOrder);
00486 
00487         if(!$success)
00488                 displayinfo("{$row['name']} widget instance successfully created at location $widgetLocation with ordering $widgetOrder. You can now change its configurations and location from <a href='./+widgets#enabledwidgets'>here</a>.");
00489         else displayerror("An error occurred while creating an instance of this widget.");
00490         
00491         return $success;
00492                 
00493 }
00500 function getEnabledWidgets($pageId)
00501 {
00502         $query="SELECT t1.`widget_id` AS 'id', t1.`widget_instanceid` AS 'instanceid', t1.`widget_location` AS 'location', t1.`widget_order` AS 'order', t1.`widget_propagate` AS 'propagate', t2.`widget_name` AS 'name', t2.`widget_description` AS 'description', t2.`widget_author` AS 'author', t2.`widget_version` AS 'version', t2.`widget_classname` AS 'classname', t2.`widget_foldername` AS 'foldername' FROM `".MYSQL_DATABASE_PREFIX."widgets` AS t1, `".MYSQL_DATABASE_PREFIX."widgetsinfo` AS t2 WHERE t1.`page_id`=$pageId AND t2.`widget_id`=t1.`widget_id` ORDER BY t1.`widget_location`, t1.`widget_order` ASC";
00503         $result=mysql_query($query);
00504         $return=array();
00505         while($row=mysql_fetch_array($result))
00506                 $return[]=$row;
00507         return $return;
00508 }
00509 
00510 // To be modified for widget
00511 function checkForWidgetIssues($modulePath,$moduleName,&$issues) {
00512         $id = 1;
00513         $i = 0;
00514         $j = 0;
00515 /*      if(!file_exists($modulePath . "moduleTables.txt")) {
00516                 addFatalIssue($issues,"Module Info file is missing",$id++);
00517                 $i = 1;
00518         }
00519         if(!file_exists($modulePath . $moduleName . ".lib.php")) {
00520                 addFatalIssue($issues,"The module file is corrupt, Please download a fresh copy of the module",$id++);
00521                 $i = 1;
00522         } else {
00523                 $content = file_get_contents($modulePath . $moduleName . ".lib.php");
00524                 $reqd = array("class ".$moduleName." implements module","public function getHtml","public function createModule","public function deleteModule","public function copyModule");
00525                 foreach($reqd as $var)
00526                         switch(mycount($content,$var)) {
00527                                 case 0:
00528                                         addFatalIssue($issues,"$var is missing",$id);
00529                                         $i = 1;
00530                                         $id++;
00531                                         break;
00532                                 case 1:
00533                                         break;
00534                                 default:
00535                                         addFatalIssue($issues,"$var is more than once",$id);
00536                                         $i = 1;
00537                                         $id++;
00538                         }
00539         }
00540         if(!file_exists($modulePath . $moduleName . ".sql")) {
00541                 addIssue($issue,"No sql file found",$id++);
00542                 $j = 1;
00543         }
00544 */
00545         return array($i,$j);
00546 }
00547 
00548 function actualWidgetPath($modulePath) {
00549         $moduleActualPath = $modulePath;
00550         $dirHandle = opendir($modulePath);
00551         while($file = readdir($dirHandle)) {
00552                 if($file=="widget.class.php")
00553                         return $modulePath;
00554                 elseif(is_dir($modulePath . $file) && $file != '.' && $file != '..') {
00555                         $return = actualWidgetPath($modulePath . $file . "/");
00556                         if($return != NULL)
00557                                 return $return;
00558                 }
00559         }
00560         return NULL;
00561 }
00562 
00563 function getWidgetName($actualPath) {
00564         $actualPath = substr($actualPath,0,-1);
00565         return substr(strrchr($actualPath,"/"),1);
00566 }
00567 
00573 function handleWidgetAdmin($pageId)
00574 {
00575         global $ICONS,$urlRequestRoot,$cmsFolder,$moduleFolder,$sourceFolder,$widgetFolder;
00576         $html = "";
00577         if(isset($_GET['subsubaction'])) {
00578                 if($_GET['subsubaction']=="installwidget") {
00579                         require_once("$sourceFolder/module.lib.php");
00580                         $uploadId = processUploaded("Widget");
00581                         if($uploadId != -1) {
00582                                 $ret = installModule($uploadId,"Widget");
00583                                 if($ret != "")
00584                                         return $ret;
00585                         }
00586                 } 
00587         }
00588         if(isset($_GET["deletewidget"])) {
00589                 $widgetId = escape($_GET['deletewidget']);
00590                 if(is_numeric($widgetId)) {
00591                         $widget = mysql_fetch_assoc(mysql_query("SELECT * FROM `" . MYSQL_DATABASE_PREFIX . "widgetsinfo` WHERE `widget_id` = '{$widgetId}'"));
00592                         $error = false;
00593                         $deletelist = array("widgets","widgetsinfo","widgetsconfiginfo","widgetsconfig","widgetsdata");
00594                         $rowCount = 0;
00595                         foreach($deletelist as $deleteitem) {
00596                                 $query = "DELETE FROM `" . MYSQL_DATABASE_PREFIX . $deleteitem . "` WHERE `widget_id` = '{$widgetId}'";
00597                                 mysql_query($query) or die($query . "<br><br>" . mysql_error());
00598                                 
00599                                 $ans = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `" . MYSQL_DATABASE_PREFIX . $deleteitem . "` WHERE `widget_id` = '{$widgetId}'"));
00600                                 $rowCount += $ans[0];
00601                         }
00602                         if(is_dir("$sourceFolder/$widgetFolder/{$widget['widget_foldername']}"))
00603                                 if(!delDir("$sourceFolder/$widgetFolder/{$widget['widget_foldername']}"))
00604                                         $error = true;
00605                         if($rowCount!=0||$error)
00606                                 displayerror("There was some error in deleting widget {$widget['widget_name']}");
00607                         else
00608                                 displayinfo("{$widget['widget_name']} successfully deleted.");
00609                 }
00610         }
00611 
00612         
00613         if(isset($_GET['widgetid']))
00614         {
00615                 $widgetid=escape($_GET['widgetid']);            
00616                 
00617                 $query="SELECT `widget_name` AS 'name', `widget_classname` AS 'classname', `widget_foldername` AS 'foldername' FROM `".MYSQL_DATABASE_PREFIX."widgetsinfo` WHERE `widget_id`=$widgetid";
00618                 $res=mysql_query($query);
00619                 if(mysql_num_rows($res)==0)
00620                 {
00621                         displayerror("Required widget is not registered with Pragyan CMS properly.");
00622                         return false;
00623                 }
00624                 $row=mysql_fetch_array($res);
00625         
00626                 global $widgetFolder;
00627                 $classname=$row['classname'];
00628                 require_once("$widgetFolder/{$row['foldername']}/widget.class.php");
00629         
00631                 $widget = new $classname($widgetid,-1,$pageId);
00632                 if(!($widget instanceof widgetFramework))
00633                 {
00634                         displayerror("The widget {$row['name']} doesn't extends widgetFramework class");
00635                         return false;
00636                 }
00637         
00638                 if(!$widget->validInstall())
00639                 {
00640                         if(!$widget->installWidget())
00641                         {
00642                                 displayerror("{$row['name']} widget is not installed properly.");
00643                                 return false;
00644                         }
00645                         $widget->loadWidget();
00646                 }
00647 
00649                 if(isset($_GET['subsubaction']) && $_GET['subsubaction']=="globalconf")
00650                         updateWidgetConf($widgetid,-1,TRUE); 
00651                         
00652                 $widgetinfo=getWidgetInfo($widgetid);
00653                 $widgetglobalconfigs=getWidgetGlobalConfigInfo($widgetid);
00654                 
00655         
00656                 $containsFileUploadFields = false;
00657                 $formElements=getConfigFormAsArray($widgetglobalconfigs,$containsFileUploadFields,-1,TRUE);
00658                 
00659                 $jsPath = "$urlRequestRoot/$cmsFolder/templates/common/scripts/formValidator.js";//validation.js
00660                 $calpath = "$urlRequestRoot/$cmsFolder/$moduleFolder/form/calendar";
00661                 $jsPathMooTools = "$urlRequestRoot/$cmsFolder/templates/common/scripts/mootools-1.11-allCompressed.js";
00662                 
00663                 $html = '<link rel="stylesheet" type="text/css" media="all" href="'.$calpath.'/calendar.css" title="Aqua" />' .
00664                                                  '<script type="text/javascript" src="'.$calpath.'/calendar.js"></script>';
00665                 $html .= '<div class="registrationform"><form class="fValidator-form" name="widgetglobalsettings" action="./+admin&subaction=widgets&subsubaction=globalconf&widgetid='.$widgetid.'" method="post"';
00666                 
00667                 if($containsFileUploadFields)
00668                         $html .= ' enctype="multipart/form-data"';
00669                 $html .= '>';
00670                 
00671                 $html.="<table width=100%><tr><th colspan=2>Widget : {$widgetinfo['name']}</th></tr>";
00672                 $html.="<tr><td>Description : </td><td> {$widgetinfo['description']}</td></tr>";
00673                 
00674                 //Uncomment when support for retrieving instances is there
00675                 /*$html.="<tr><td>Instances : </td><td>";
00676                 $instances=getWidgetInstances($widgetid);
00677                 if(count($instances)>0) $html.="<ol>";
00678                 else $html.="None"; 
00679                 foreach($instances as $instance)
00680                 {
00681                         $html.="<li><a href='$urlRequestRoot/{$instance['url']}/+settings&subaction=widgets'>".
00682                                         "{$instance['name']} [{instance['url']}]</li>";
00683                 }
00684                 if(count($instances)>0) $html.="</ol>";
00685                 $html.="</td></tr>";
00686                 */
00687                 
00688                 $html .="<tr>".join($formElements, "</tr>\n<tr>")."</tr>";
00689                 
00690                 $html.="</table><input name='update_global_settings' type='submit' value='Update'/>" .
00691                                 "<input type='reset' value='Reset'/>";
00692                 $html.="</form><br/>";
00693                 
00694         
00695         }
00696         
00697         
00698         $widgetsarr=getAllWidgetsInfo();
00699         
00700         
00701         $html .= "<fieldset><legend>{$ICONS['Widgets']['small']}Available Widgets</legend>";
00702         
00703         $html .= "<table width=100%><tr><th colspan=4>Available Widgets<br/><i>Mouse over for description and Click for configuration</i></th></tr>
00704         <tr><th>Name</th><th>Version</th><th>Author</th><th>Actions</th></tr>";
00705         foreach( $widgetsarr as $widget )
00706         {
00707                 $html.="<tr><td><a title='".$widget['description']."' href='./+admin&subaction=widgets&widgetid=".$widget['id']."'>".$widget['name']."</a></td><td>{$widget['version']}</td><td>{$widget['author']}</td><td><a href='./+admin&subaction=widgets&widgetid={$widget['id']}'>{$ICONS['Edit']['small']}</a><a href='./+admin&subaction=widgets&deletewidget={$widget['id']}'>{$ICONS['Delete']['small']}</a></td></tr>";
00708         }
00709         $html .= <<<HTML
00710 <tr><td>Install new widget:</td><td colspan=3>
00711 <form method='POST' action='./+admin&subaction=widgets&subsubaction=installwidget' enctype="multipart/form-data">
00712 <input type='file' name='file' id='file'><input type='submit' name='btn_install' value='Upload'>
00713 </form>
00714 </td></tr></table></fieldset>
00715 HTML;
00716         return $html;
00717 }
00727 function getConfigFormAsArray($widgetconfigs,$containsFileUploadFields,$widgetinstanceid,$isglobal)
00728 {
00729                 $containsFileUploadFields = false;
00730                 $formValues = array();
00731         
00732                 $formElements = array();
00733                 $confnames = array();
00734                 
00735                 if($isglobal) $widgetinstanceid=-1;
00736                 
00738                 foreach($widgetconfigs as $configentry)
00739                 {
00740                         $confnames[]=$configentry['confname'];
00741                         $formValues[$configentry['confname']]=$configentry['confdefault'];
00742                 }
00743                         
00744                 $query="SELECT `config_name` AS 'confname', `config_value` AS 'confvalue' FROM ".MYSQL_DATABASE_PREFIX."widgetsconfig WHERE `widget_instanceid`=$widgetinstanceid AND `widget_id`={$widgetconfigs[0]['id']}  AND `config_name` IN ('".join($confnames,"','")."')";
00745 
00746                 $res=mysql_query($query);
00747 
00749                 while($row=mysql_fetch_assoc($res))
00750                 {
00751                         $formValues[$row['confname']]=$row['confvalue'];        
00752                 }
00753                 
00754                 $jsValidationFunctions = array();
00755                 
00756                 foreach( $widgetconfigs as $configentry )
00757                 {
00758                         $jsOutput = '';
00759                         if($configentry['conftype'] == 'file') {
00760                                 $containsFileUploadFields = true;
00761                         }
00762                 
00763                         $formElements[] =       getFormInputField
00764                                                 (
00765                                                         $configentry,
00766                                                         isset($formValues[$configentry['confname']]) ? $formValues[$configentry['confname']] : '',
00767                                                         $isglobal
00768                                                 );
00769                         if($jsOutput != '') {
00770                                 $jsValidationFunctions[] = $jsOutput;
00771                         }
00772                 }
00773 
00774                 return $formElements;
00775 }
00776 
00784 function getFormInputField($configentry, $value="", $isglobal) {
00785 
00786 
00787         $elementType = $configentry['conftype'];
00788         
00789         if($elementType == 'noinput')
00790                 $htmlOutput = '<td colspan=2>';
00791         else
00792                 $htmlOutput = '<td>';
00793                 
00794         $htmlOutput .=  $configentry['confdisplay'];
00795 
00796         
00797         $elementTypeOptions = $configentry['confoptions'];
00798         
00799         $options = explode('|', $elementTypeOptions);
00801         $options=array_map("htmlentities",$options);
00802         
00803         $value = htmlentities($value);
00804         
00805         if($isglobal) $formname='globalconfform';
00806         else $formname='pageconfform';
00807         
00808         $elementName = "{$formname}_" .  $configentry['confname'];
00809         
00810         if($elementType != 'noinput')
00811                 $htmlOutput .='</td><td>';
00812         
00813         $functionName = "render".ucfirst(strtolower($elementType))."TypeField";
00814         
00815         
00816         if($functionName($elementName,$value,$options,$htmlOutput)==false)
00817                 displayerror("Unable to run function ".$functionName);
00818 
00819         return $htmlOutput . "</td>\n";
00820 }
00829 function renderTextareaTypeField($elementName,$value,$options,&$htmlOutput)
00830 {
00831                         $rows = 5;
00832                         $cols = 20;
00833                         $htmlOutput .= '<textarea style="width:100%" rows="'.$rows.'" cols="'.$cols.'"  name="'.$elementName.'" id="'.$elementName.'" />'.$value.'</textarea>';
00834                         return true;
00835 }
00844 function renderSelectTypeField($elementName,$value,$options,&$htmlOutput)
00845 {
00846 
00847         
00848         $optionsHtml = '';
00849 
00850         for($i = 0; $i < count($options); $i++) {
00851                 if($options[$i] == $value) {
00852                         $optionsHtml .= '<option value="'.$options[$i].'" selected="selected" >' . $options[$i] . "</option>\n";
00853                 }
00854                 else {
00855                         $optionsHtml .= '<option value="'.$options[$i].'" >' . $options[$i] . "</option>\n";
00856                 }
00857         }
00858 
00859         $htmlOutput .= '<select name="'.$elementName.'" id="'.$elementName.'">' . $optionsHtml . '</select>';
00860         return true;
00861 }
00870 function renderRadioTypeField($elementName,$value,$options,&$htmlOutput)
00871 {
00872 
00873         
00874         
00875         $optionsHtml = '';
00876 
00877         for($i = 0; $i < count($options); $i++) {
00878                 $optionsHtml .= '<label><input type="radio" id="'.$elementName.'" name="'.$elementName.'" value="'.
00879                                                                                 $options[$i].'"';
00880 
00881                 if($options[$i] == $value) {
00882                         $optionsHtml .= ' checked="checked"';
00883                 }
00884 
00885                 $optionsHtml .= '/>'.$options[$i].'</label>&nbsp;&nbsp;';
00886         }
00887 
00888         $htmlOutput .= $optionsHtml;
00889         return true;
00890 }
00899 function renderBoolTypeField($elementName,$value,$options,&$htmlOutput)
00900 {
00901 
00902         
00903         $options = array("Yes","No");
00904 
00905         $value = ($value=='1'||$value=='Yes')?"Yes":"No";
00906         $optionsHtml = '';
00907 
00908         for($i = 0; $i < count($options); $i++) {
00909                 $optionsHtml .= '<label><input type="radio" id="'.$elementName.'" name="'.$elementName.'" value="'.
00910                                                                                 $options[$i].'"';
00911 
00912                 if($options[$i] == $value) {
00913                         $optionsHtml .= ' checked="checked"';
00914                 }
00915 
00916                 $optionsHtml .= '/>'.$options[$i].'</label>&nbsp;&nbsp;';
00917         }
00918 
00919         $htmlOutput .= $optionsHtml;
00920         return true;
00921 }
00930 function renderCheckboxTypeField($elementName,$value,$options,&$htmlOutput)
00931 {
00932 
00933         $optionsHtml = '';
00934         $values=explode("|",$value);
00935         for($i = 0; $i < count($options); $i++) {
00936                 $optionsHtml .= '<label><input type="checkbox" id="'.$elementName.'_'.$i.'" name="'.$elementName.'_'.$i.'" value="'.
00937                                                                                 $options[$i].'"';
00938 
00939                 if(array_search($options[$i],$values)!==FALSE) {
00940                         $optionsHtml .= ' checked="checked"';
00941                 }
00942 
00943                 $optionsHtml .= $validCheck.' />'.$options[$i].'</label>&nbsp;&nbsp;';
00944         }
00945 
00946         $htmlOutput .= $optionsHtml;
00947         return true;
00948 }
00958 function renderFileTypeField($elementName,$value,$options,&$htmlOutput)
00959 {
00960 
00961         global $sourceFolder;
00962         require_once("$sourceFolder/upload.lib.php");
00963         
00965         $htmlOutput .= getFileUploadField($elementName,"", 2*1024*1024, ""); 
00966 
00967         
00968         if($value != '') {
00969                 $htmlOutput .= '<br />(Leave blank to keep current file : <a href="./' . $value . '">'.$value.'</a>)';
00970         }
00971 
00972         return true;
00973 }
00982 function renderTextTypeField($elementName,$value,$options,&$htmlOutput)
00983 {
00984         
00985         $htmlOutput .= '<input type="text" name="'.$elementName.'" id="'.$elementName.'" value="'.$value.'"  />';
00986                                                                 
00987         return true;
00988 }
00997 function renderIntegerTypeField($elementName,$value,$options,&$htmlOutput)
00998 {
00999         
01000         $htmlOutput .= '<input type="text" class="numeric" name="'.$elementName.'" id="'.$elementName.'" value="'.$value.'"  />';
01001                                                                 
01002         return true;
01003 }
01012 function renderHiddenTypeField($elementName,$value,$options,&$htmlOutput)
01013 {
01014         
01015         $htmlOutput .= '<input type="hidden" name="'.$elementName.'" id="'.$elementName.'" value="'.$value.'"  />';
01016                                                                 
01017         return true;
01018 }
01027 function renderDatetimeTypeField($elementName,$value,$options,&$htmlOutput)
01028 {
01029         
01030         $datetimeFormat = "'%Y-%m-%d %H:%M'";
01031 
01032         $validCheck = ' dateformat="YY-MM-DD hh:mm" ';
01033 
01034 
01035         $htmlOutput .= '<input type="text" '. $validCheck . ' name="'.$elementName.'" value="' . $value . '" id="'.$elementName.'" /><input name="cal'.$elementName.'" type="reset" value=" ... " onclick="return showCalendar(\'' . $elementName . '\', '.$datetimeFormat.', \'24\', true);" />';
01036         return true;
01037 }
01046 function renderDateTypeField($elementName,$value,$options,&$htmlOutput)
01047 {
01048         
01049         $datetimeFormat = "'%Y-%m-%d'" ;
01050 
01051         $validCheck .= ' dateformat="YY-MM-DD" ';
01052 
01053 
01054         $htmlOutput .= '<input type="text" '. $validCheck . ' name="'.$elementName.'" value="' . $value . '" id="'.$elementName.'" /><input name="cal'.$elementName.'" type="reset" value=" ... " onclick="return showCalendar(\'' . $elementName . '\', '.$datetimeFormat.', \'24\', true);" />';
01055         return true;
01056 }
01057 
01066 function renderNoinputTypeField($elementName,$value,$options,&$htmlOutput)
01067 {
01068         return true;
01069 }
01070 
01076 function getWidgetPageConfigInfo($widgetid)
01077 {
01078         $query="SELECT `widget_id` AS 'id', `config_name` AS 'confname', `config_displaytext` AS 'confdisplay', `config_type` AS 'conftype',`config_options` AS 'confoptions',`config_default` AS 'confdefault'  FROM `".MYSQL_DATABASE_PREFIX."widgetsconfiginfo` WHERE `widget_id`=$widgetid AND `is_global`=0 ORDER BY `config_rank`";
01079         $res=mysql_query($query);
01080         $ret=array();
01081         while($arr=mysql_fetch_assoc($res))
01082         {
01083                 $ret[]=$arr;
01084         }
01085         return $ret;
01086 }
01087 
01093 function getWidgetGlobalConfigInfo($widgetid)
01094 {
01095         $query="SELECT `widget_id` AS 'id', `config_name` AS 'confname', `config_displaytext` AS 'confdisplay', `config_type` AS 'conftype',`config_options` AS 'confoptions',`config_default` AS 'confdefault'  FROM `".MYSQL_DATABASE_PREFIX."widgetsconfiginfo` WHERE `widget_id`=$widgetid AND `is_global`=1 ORDER BY `config_rank`";
01096         $res=mysql_query($query);
01097         $ret=array();
01098         while($arr=mysql_fetch_assoc($res))
01099         {
01100                 $ret[]=$arr;
01101         }
01102         return $ret;
01103 }
01109 function getWidgetInfo($widgetid)
01110 {
01111         $query="SELECT `widget_id` AS 'id', `widget_name` AS 'name', `widget_description` AS 'description', `widget_version` AS 'version', `widget_author` AS 'author', `widget_foldername` AS 'foldername' FROM `".MYSQL_DATABASE_PREFIX."widgetsinfo` WHERE `widget_id`=$widgetid";
01112         $res=mysql_query($query);
01113         return mysql_fetch_assoc($res);
01114 }
01119 function getAllWidgetsInfo()
01120 {
01121         $query="SELECT `widget_id` AS 'id',`widget_name` AS 'name',`widget_description` AS 'description', `widget_version` AS 'version', `widget_author` AS 'author' FROM `".MYSQL_DATABASE_PREFIX."widgetsinfo`";
01122         $res=mysql_query($query);
01123         $ret=array();
01124         while($row=mysql_fetch_array($res))
01125         {
01126                 $ret[]=$row;
01127         }
01128         return $ret;
01129 }
01137 function updateWidgetConf($widgetid,$widgetinstanceid=-1,$isglobal=TRUE)
01138 {
01139         $query="SELECT `config_name`,`config_type`,`config_default`,`config_options` FROM `".MYSQL_DATABASE_PREFIX."widgetsconfiginfo` WHERE `widget_id`=$widgetid AND `is_global`=".(int)$isglobal;
01140         
01141         $res=mysql_query($query);
01142         
01143         if($isglobal) $widgetinstanceid=-1;
01144         
01145         while($row=mysql_fetch_array($res))
01146         {
01147         
01148                 $conftype=$row['config_type'];
01149                 $confname=$row['config_name'];
01150                 $confdef=$row['config_default'];
01151                 $confoptions=$row['config_options'];
01152                 if($isglobal)
01153                         $postvar="globalconfform_".$confname;
01154                 else $postvar="pageconfform_".$confname;
01155                 
01156                 $confcur=false;
01157                 
01158                 $query="SELECT `config_value` FROM `".MYSQL_DATABASE_PREFIX."widgetsconfig` WHERE `config_name`='$confname' AND `widget_id`=$widgetid AND `widget_instanceid`=$widgetinstanceid";
01159         
01160                 $result=mysql_query($query);
01161                 
01162                 while($row=mysql_fetch_assoc($result))
01163                         $confcur=$row['config_value'];
01164                 
01165                 if($conftype=='checkbox')
01166                         $confval=escape(interpretSubmitValue($conftype,$postvar,$confoptions));
01167                 else    
01168                         $confval=escape(interpretSubmitValue($conftype,$postvar));
01169                 
01171                 $confval=($confval===false)?(($confcur===false)?$confdef:$confcur):$confval;
01172                 if(mysql_num_rows($result)==0)
01173                 {
01174                         $query="INSERT INTO `".MYSQL_DATABASE_PREFIX."widgetsconfig` (`widget_id`,`widget_instanceid`,`config_name`,`config_value`) VALUES ($widgetid,$widgetinstanceid,'$confname','$confval')";
01175                         mysql_query($query);
01176                 }       
01177                 else if($confval!=$confcur)
01178                 {
01179                         $query="UPDATE `".MYSQL_DATABASE_PREFIX."widgetsconfig` SET `config_value`='$confval' WHERE `config_name`='$confname' AND `widget_id`=$widgetid AND `widget_instanceid`=$widgetinstanceid";
01180                         mysql_query($query);
01181                 }
01182         
01183         }
01184         displayinfo("Configurations updated successfully!");
01185         
01186 }
01187 
01195 function interpretSubmitValue($conftype,$postvar,$options=NULL)
01196 {
01197         if($conftype=='textarea')
01198         {
01199                 return $_POST[$postvar];
01200         }
01201         else if($conftype=='select')
01202         {
01203                 return isset($_POST[$postvar])?$_POST[$postvar]:false;
01204         }
01205         else if($conftype=='radio')
01206         {
01207                 return isset($_POST[$postvar])?$_POST[$postvar]:false;
01208         }
01209         else if($conftype=='bool')
01210         {
01211                 return isset($_POST[$postvar])?$_POST[$postvar]:false;
01212         }
01213         else if($conftype=='checkbox')
01214         {
01215                 $optionvals = explode("|",$options);
01216                 $i=-1;
01217                 $values = array();
01218                 foreach($optionvals as $value) {
01219                         $i++;
01220                         if(!isset($_POST[$postvar."_".$i]))
01221                                 continue;
01222                         $values[] = $value;
01223                 }
01224                 $valuesString = join($values,"|");
01225                 return $valuesString;
01226         }
01227         else if($conftype=='text')
01228         {
01229                 return $_POST[$postvar];
01230         }
01231         else if($conftype=='integer')
01232         {
01233                 return (isset($_POST[$postvar])&&is_numeric($_POST[$postvar]))?$_POST[$postvar]:false;
01234         }
01235         else if($conftype=='hidden')
01236         {
01237                 return $_POST[$postvar];
01238         }
01239         else if($conftype=='datetime')
01240         {
01241                 return isset($_POST[$postvar])?$_POST[$postvar]:false;
01242         }
01243         else if($conftype=='date')
01244         {
01245                 return isset($_POST[$postvar])?$_POST[$postvar]:false;  
01246         }       
01247         else if($conftype=='noinput')
01248         {
01249                 return NULL;
01250         }
01251         return false;
01252         
01253 }
01259 function reloadWidgets()
01260 {
01261         // Load the widgets from widget/ directory and update proper entries in database. Should be there in admin/site-maintenaince
01262 }
01263 function getWidgetInstances($widgetid)
01264 {
01265         return array();
01266 }
01267  
01268 ?>

Generated on Sun Jan 2 2011 04:55:32 for Pragyan CMS by  doxygen 1.7.1