public static function getManifest($userId, array $tabs, $format = 'xml')
 {
     # Verify if tabs parameters is not empty
     if (count($tabs) == 0) {
         throw new MwwException(MwwException::MODEL, 'WidgetSapce::getManifest / tabs argument must contain a least one argument');
     }
     # Init vars
     $db = DbUtil::accessFactory();
     $userId = $db->escape($userId);
     $wsTabs = array();
     # Process each tabs
     foreach ($tabs as $tabId) {
         # Init vars
         $tabId = $db->escape($tabId);
         $wsTabs[$tabId] = array();
         # Get widgets for the given tab
         $rs = $db->select("SELECT * FROM `interface` WHERE `userid` = {$userId} AND `tab` = '{$tabId}'");
         while ($rs->fetch()) {
             $widgetInstanceId = $rs->widgetinstanceid;
             $widgetId = WidgetInstances::getWidgetIdByInstanceId($widgetInstanceId);
             if (!$widgetId) {
                 throw new MwwException(MwwException::MODEL, 'WidgetSapce::getManifest / A widget instance id seems to be not linked to a widget id...');
             }
             # Save widget interface info
             $wsTabs[$tabId][$widgetInstanceId]['interface'] = array('widgetId' => $widgetId, 'column' => $rs->column, 'minimized' => $rs->minimized, 'position' => $rs->position);
             $wsTabs[$tabId][$widgetInstanceId]['userpreference'] = array();
             # Get and save widget user preference info
             $rs_pref = $db->select("SELECT * FROM `interface_preferences` WHERE `userid` = {$userId} AND `widgetinstanceid` = {$widgetInstanceId}");
             while ($rs_pref->fetch()) {
                 $wsTabs[$tabId][$widgetInstanceId]['userpreference'][$rs_pref->preference] = $rs_pref->value;
             }
         }
     }
     # return widget space
     if ($format == 'raw') {
         return $wsTabs;
     }
     # Before the creating of xml, try to canonize the widget position
     $widgetsTabOrdored = array();
     foreach ($wsTabs as $tabId => $tabInfo) {
         # Put widget into tab following their position in UI
         foreach ($tabInfo as $widgetInstanceId => $widgetInfo) {
             $widgetsTabOrdored[$tabId][$widgetInfo['interface']['column']][$widgetInfo['interface']['position']] = $widgetInfo;
         }
     }
     # Now canonize the position for each column
     $tmp = array();
     foreach ($widgetsTabOrdored as $tabId => $tabInfo) {
         $tmp[$tabId] = array();
         foreach ($tabInfo as $column => $columnContent) {
             $t = $columnContent;
             ksort($t);
             $n = count($t);
             foreach ($t as $oldKey => $content) {
                 $tmp[$tabId][$column][] = $content;
             }
             # To start at 0
         }
     }
     # To finish...
     $widgetsTabOrdored = $tmp;
     //var_dump($widgetsTabOrdored);
     # Now construct the xml output
     $doc = new DOMDocument('1.0', 'utf-8');
     $doc->formatOutput = true;
     $doc->preserveWhiteSpace = false;
     # Create the root node
     $wsXML = xa($doc, $doc, 'widgetspace');
     foreach ($widgetsTabOrdored as $tabId => $tabInfo) {
         $tabXML = xa($doc, $wsXML, 'tabs' . xs . 'title=' . $tabId);
         # TODO : Add here tab properties
         # Add a Layout
         $layoutXML = xa($doc, $tabXML, 'layout' . xs . 'type=three_col_layout');
         # Here widget must be added into section according to their order in the three_col_layout
         # Add widgets into layout
         foreach ($tabInfo as $columnPosition => $columnContent) {
             $sectionXML = xa($doc, $layoutXML, 'section' . xs . 'position=' . $columnPosition);
             # the position attribute is needed (because there are cases where specific column is empty)
             foreach ($columnContent as $position => $widgetInfo) {
                 $i = $widgetInfo['interface'];
                 $up = $widgetInfo['userpreference'];
                 $widgetXML = xa($doc, $sectionXML, 'widget' . xs . 'widgetid=' . $i['widgetId'] . xs . 'minimized=' . $i['minimized']);
                 foreach ($up as $prefname => $prefvalue) {
                     $propertyXML = xa($doc, $widgetXML, 'property');
                     xa($doc, $propertyXML, 'name', $prefname);
                     xa($doc, $propertyXML, 'value', $prefvalue);
                 }
             }
         }
     }
     # return the XML Document
     return $doc;
 }
 /**
  * Retrieves the XML manifest of a widget for a given user. Be carefull, the returned
  * manifest will not be exactly the same as the manifest deployed on the application server.
  * Actually, the return manifest will be provided with the preferences relevant with a given user,
  * and not the default values of the preferences as expected.
  *
  * @param integer $userId The user's identifier.
  * @param string $widgetInstanceId The widget's instance identifier.
  * @return string The requested manifest as a character stream.
  */
 public static function retrieveWidgetManifest($userId, $widgetInstanceId)
 {
     $instanceIsRequested = is_numeric($widgetInstanceId);
     # Get the widget associated with the instance
     if ($instanceIsRequested) {
         $widgetId = WidgetInstances::getWidgetIdByInstanceId($widgetInstanceId);
         if (!$widgetId) {
             throw new MwwException(MwwException::MODEL, 'Unable to find the any widget instance [' . $widgetInstanceId . ']');
         }
     } else {
         $widgetId = $widgetInstanceId;
     }
     // We load the document wich must be in the widgets direcory.
     try {
         $doc = new DOMDocument();
         $doc->preserveWhiteSpace = false;
         $doc->load('widgets/' . $widgetId . '/config.xml');
         // Try to determine if l10n module is activate
         $l10nModule = $doc->getElementsByTagNameNS('http://palette.ercim.org/ns/', 'active_l10n_module');
         if ($l10nModule->length == 1 && $l10nModule->item(0)->nodeValue == 'true') {
             # Now inject avalaible language into manifest
             $doc->createElementNS('http://palette.ercim.org/ns/', 'l10n_avalaible_language');
             $l = Widgets::retrieveAvalaibleLanguage($widgetId);
             if (!empty($l)) {
                 $l10nAvalaibleLanguageNode = $doc->createElementNS('http://palette.ercim.org/ns/', 'l10n_avalaible_language');
                 # Process All Avalaible language
                 foreach ($l as $l10n_code => $l10n_label) {
                     $langageNode = $doc->createElementNS('http://palette.ercim.org/ns/', 'l10n_langage');
                     $langageCodeNode = $doc->createAttribute('code');
                     $l10n_code = trim($l10n_code);
                     $l10n_label = trim($l10n_label);
                     $langageAttCodeValueNode = $doc->createTextNode($l10n_code);
                     $langageLabelValueNode = $doc->createTextNode($l10n_label);
                     $langageNode->appendChild($langageCodeNode);
                     $langageCodeNode->appendChild($langageAttCodeValueNode);
                     $langageNode->appendChild($langageLabelValueNode);
                     $l10nAvalaibleLanguageNode->appendChild($langageNode);
                 }
                 $widget = $doc->getElementsByTagName('widget')->item(0);
                 $widget->appendChild($l10nAvalaibleLanguageNode);
             }
         }
         if (VALIDATE_XML_FILE && !$doc->schemaValidate(XML_SCHEMA_FILE)) {
             throw new XMLValidationException(MwwException::MODEL, XML_SCHEMA_FILE);
         } else {
             if ($instanceIsRequested) {
                 // Manifest loaded and validated.
                 // Now we add it dynamic values relevant to the user preferences.
                 $db = DbUtil::accessFactory();
                 $userId = $db->escape($userId);
                 $widgetInstanceId = $db->escape($widgetInstanceId);
                 $prefs = $doc->getElementsByTagNameNS('http://palette.ercim.org/ns/', 'preference');
                 foreach ($prefs as $pref) {
                     $name = $pref->attributes->getNamedItem('name')->value;
                     $rs = $db->select("SELECT `value` FROM `interface_preferences` WHERE `widgetInstanceId` = '{$widgetInstanceId}' AND `userid` = {$userId} AND `preference` = '{$name}'");
                     /* Read the the result set into an associative array */
                     if ($rs->count() == 1) {
                         $pref->setAttribute('value', $rs->value);
                     } else {
                         if ($pref->attributes->getNamedItem('default_value')) {
                             $pref->setAttribute('value', $pref->attributes->getNamedItem('default_value')->value);
                         } else {
                             $pref->setAttribute('value', '');
                         }
                     }
                 }
             }
             return $doc->saveXML();
         }
     } catch (DOMException $e) {
         throw new FileException(MwwException::MODEL, 'Unable to load the manifest for widget ' . $widgetId);
     }
 }