3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
4 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
6 * This file is a part of Codendi.
8 * Codendi is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Codendi is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
22 require_once $gfcommon.'widget/WidgetLayout.class.php';
23 require_once $gfcommon.'widget/Widget.class.php';
24 require_once $gfcommon.'include/preplugins.php';
29 * Manage layouts for users, groups and homepage
31 class WidgetLayoutManager {
32 const OWNER_TYPE_USER = 'u';
34 * Layout for project home
37 const OWNER_TYPE_GROUP = 'g';
38 const OWNER_TYPE_HOME = 'h';
43 * Display the default layout for the "owner". It my be the home page, the project summary page or /my/ page.
46 * @param char owner_type
48 function displayLayout($owner_id, $owner_type) {
49 $sql = "SELECT * from owner_layouts where owner_id=$1 and owner_type=$2";
50 $res = db_query_params($sql, array($owner_id, $owner_type));
51 if($res && db_numrows($res)<1) {
52 if($owner_type = self::OWNER_TYPE_USER) {
53 $this->createDefaultLayoutForUser($owner_id);
54 $this->displayLayout($owner_id,$owner_type);
56 $this->createDefaultLayoutForProject($owner_id,1);
57 $this->displayLayout($owner_id,$owner_type);
61 FROM layouts AS l INNER JOIN owner_layouts AS o ON(l.id = o.layout_id)
62 WHERE o.owner_type = $1
66 $req = db_query_params($sql, array($owner_type ,$owner_id));
67 if ($data = db_fetch_array($req)) {
68 $readonly = !$this->_currentUserCanUpdateLayout($owner_id, $owner_type);
70 // echo '<p class="customize"><a href="/widgets/widgets.php?owner='. $owner_type.$owner_id .'&layout_id='. $data['id'] .'">'. _("Customize") .'</a></p>';
71 // } else if ($owner_type === self::OWNER_TYPE_GROUP) {
74 $layout = new WidgetLayout($data['id'], $data['name'], $data['description'], $data['scope']);
75 $sql = 'SELECT * FROM layouts_rows WHERE layout_id = $1 ORDER BY rank';
76 $req_rows = db_query_params($sql,array($layout->id));
77 while ($data = db_fetch_array($req_rows)) {
78 $row = new WidgetLayout_Row($data['id'], $data['rank']);
79 $sql = 'SELECT * FROM layouts_rows_columns WHERE layout_row_id = $1';
80 $req_cols = db_query_params($sql,array($row->id));
81 while ($data = db_fetch_array($req_cols)) {
82 $col = new WidgetLayout_Row_Column($data['id'], $data['width']);
83 $sql = "SELECT * FROM layouts_contents WHERE owner_type = $1 AND owner_id = $2 AND column_id = $3 ORDER BY rank";
84 $req_content = db_query_params($sql,array($owner_type, $owner_id, $col->id));
85 while ($data = db_fetch_array($req_content)) {
86 $c = Widget::getInstance($data['name']);
87 if ($c && $c->isAvailable()) {
88 $c->loadContent($data['content_id']);
89 $col->add($c, $data['is_minimized'], $data['display_preferences']);
99 $layout->display($readonly, $owner_id, $owner_type);
105 * _currentUserCanUpdateLayout
107 * @param int owner_id
108 * @param char owner_type
109 * @return boolean true if the user dan uppdate the layout (add/remove widget, collapse, set preferences, ...)
111 function _currentUserCanUpdateLayout($owner_id, $owner_type) {
113 $request =& HTTPRequest::instance();
114 switch ($owner_type) {
115 case self::OWNER_TYPE_USER:
116 if (user_getid() == $owner_id) { //Current user can only update its own /my/ page
120 case self::OWNER_TYPE_GROUP:
121 if (forge_check_perm('project_admin', $owner_id, NULL)) { //Only project admin
125 case self::OWNER_TYPE_HOME:
134 * createDefaultLayoutForUser
136 * Create the first layout for the user and add some initial widgets:
142 * - MyMonitoredForums
143 * - and widgets of plugins if they want to listen to the event default_widgets_for_new_owner
145 * @param int owner_id The id of the newly created user
147 function createDefaultLayoutForUser($owner_id) {
148 $owner_type = self::OWNER_TYPE_USER;
149 $sql = "INSERT INTO owner_layouts(layout_id, is_default, owner_id, owner_type) VALUES (1, 1, $1, $2)";
150 if (db_query_params($sql, array($owner_id, $owner_type))) {
152 $sql = "INSERT INTO layouts_contents(owner_id, owner_type, layout_id, column_id, name, rank) VALUES ";
154 $args[] = "($1, $2, 1, 1, 'myprojects', 0)";
155 $args[] = "($1, $2, 1, 1, 'mybookmarks', 1)";
156 $args[] = "($1, $2, 1, 1, 'mymonitoredforums', 2)";
157 $args[] = "($1, $2, 1, 1, 'mysurveys', 4)";
158 $args[] = "($1, $2, 1, 2, 'myartifacts', 0)";
159 $args[] = "($1, $2, 1, 2, 'mymonitoredfp', 1)";
161 foreach($args as $a) {
162 db_query_params($sql.$a,array($owner_id,$owner_type));
165 /* $em =& EventManager::instance();
167 $em->processEvent('default_widgets_for_new_owner', array('widgets' => &$widgets, 'owner_type' => $owner_type));
168 foreach($widgets as $widget) {
169 $sql .= ",($13, $14, 1, $15, $16, $17)";
176 * createDefaultLayoutForProject
178 * Create the first layout for a new project, based on its parent template.
179 * Add some widgets based also on its parent configuration and on its service configuration.
181 * @param int group_id the id of the newly created project
182 * @param int template_id the id of the project template
184 function createDefaultLayoutForProject($group_id, $template_id) {
185 $pm = ProjectManager::instance();
186 $project = $pm->getProject($group_id);
187 $sql = "INSERT INTO owner_layouts(layout_id, is_default, owner_id, owner_type)
188 SELECT layout_id, is_default, $1, owner_type
190 WHERE owner_type = $2
193 if (db_query_params($sql,array($group_id, self::OWNER_TYPE_GROUP,$template_id))) {
194 $sql = "SELECT layout_id, column_id, name, rank, is_minimized, is_removed, display_preferences, content_id
195 FROM layouts_contents
196 WHERE owner_type = $1
199 if ($req = db_query_params($sql,array( self::OWNER_TYPE_GROUP,$template_id))) {
200 while($data = db_fetch_array($req)) {
201 $w = Widget::getInstance($data['name']);
203 $w->setOwner($template_id, self::OWNER_TYPE_GROUP);
204 if ($w->canBeUsedByProject($project)) {
205 $content_id = $w->cloneContent($w->content_id, $group_id, self::OWNER_TYPE_GROUP);
206 $sql = "INSERT INTO layouts_contents(owner_id, owner_type, content_id, layout_id, column_id, name, rank, is_minimized, is_removed, display_preferences)
207 VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);
209 db_query_params($sql, array($group_id , self::OWNER_TYPE_GROUP , $content_id , $data['layout_id'] , $data['column_id'] , $data['name'] , $data['rank'] , $data['is_minimized'] , $data['is_removed'] , $data['display_preferences'] ));
220 * displayAvailableWidgets - Display all widgets that the user can add to the layout
222 * @param int owner_id
223 * @param char owner_type
224 * @param int layout_id
226 function displayAvailableWidgets($owner_id, $owner_type, $layout_id) {
227 // select already used widgets
228 $used_widgets = array();
230 FROM layouts_contents
231 WHERE owner_type = $1
234 AND content_id = 0 AND column_id <> 0";
235 $res = db_query_params($sql,array($owner_type,$owner_id,$layout_id));
236 while($data = db_fetch_array($res)) {
237 $used_widgets[] = $data['name'];
239 // display contextual toolbar
240 echo '<ul class="widget_toolbar">';
241 $url = "/widgets/widgets.php?owner=".HTTPRequest::instance()->get('owner').
242 "&layout_id=".HTTPRequest::instance()->get('layout_id');
244 $update_layout = (HTTPRequest::instance()->get('update') == 'layout');
245 if ($update_layout) {
246 // customized selected
247 echo '<li><a href="'. $url .'">'. _("Add widgets") .'</a></li>';
248 echo '<li class="current"><a href="'. $url.'&update=layout' .'">'. _("Customize layout") .'</a></li>';
251 // add selected, or default when first displayed
252 echo '<li class="current"><a href="'. $url .'">'. _("Add widgets") .'</a></li>';
253 echo '<li><a href="'. $url.'&update=layout' .'">'. _("Customize layout") .'</a></li>';
257 echo '<form action="/widgets/updatelayout.php?owner='. $owner_type.$owner_id .'&action='. $action .'&layout_id='. $layout_id .'" method="post">';
258 if ($update_layout) {
259 $sql = "SELECT * FROM layouts WHERE scope='S' ORDER BY id ";
260 $req_layouts = db_query_params($sql,array());
263 while ($data = db_fetch_array($req_layouts)) {
264 $checked = $layout_id == $data['id'] ? 'checked="checked"' : '';
265 $is_custom = $is_custom && !$checked;
266 echo '<tr class="layout-manager-chooser '. ($checked ? 'layout-manager-chooser_selected' : '') .'" ><td>';
267 echo '<input type="radio" name="layout_id" value="'. $data['id'] .'" id="layout_'. $data['id'] .'" '. $checked .'/>';
269 echo '<label for="layout_'. $data['id'] .'">';
270 echo html_image('layout/'. strtolower(preg_replace('/(\W+)/', '-', $data['name'])) .'.png');
273 echo '<label for="layout_'. $data['id'] .'"><strong>'. $data['name'] .'</strong><br />';
274 echo $data['description'];
278 /* Custom layout are not available yet */
279 $checked = $is_custom ? 'checked="checked"' : '';
280 echo '<tr class="layout-manager-chooser '. ($checked ? 'layout-manager-chooser_selected' : '') .'"><td>';
281 echo '<input type="radio" name="layout_id" value="-1" id="layout_custom" '. $checked .'/>';
283 echo '<label for="layout_custom">';
284 echo html_image('layout/custom.png', array('style' => 'vertical-align:top;float:left;'));
287 echo '<label for="layout_custom"><strong>'. 'Custom' .'</strong><br />';
288 echo 'Define your own layout:';
290 echo '<table id="layout-manager">
293 <div class="layout-manager-row-add">+</div>';
294 $sql = 'SELECT * FROM layouts_rows WHERE layout_id = $1 ORDER BY rank';
295 $req_rows = db_query_params($sql,array($layout_id));
296 while ($data = db_fetch_array($req_rows)) {
297 echo '<table class="layout-manager-row">
299 <td class="layout-manager-column-add">+</td>';
300 $sql = 'SELECT * FROM layouts_rows_columns WHERE layout_row_id = $1';
301 $req_cols = db_query_params($sql,array($data['id']));
302 while ($data = db_fetch_array($req_cols)) {
303 echo '<td class="layout-manager-column">
304 <div class="layout-manager-column-remove">x</div>
305 <div class="layout-manager-column-width">
306 <input type="text" value="'. $data['width'] .'" size="1" maxlength="3" />%
309 <td class="layout-manager-column-add">+</td>';
313 <div class="layout-manager-row-add">+</div>';
320 echo '<input type="submit" id="save" value="'. _("Submit") .'" />';
322 // display the widget selection form
330 $after .= $this->_displayWidgetsSelectionForm(sprintf(_("%s Widgets"), forge_get_config('forge_name')), Widget::getCodendiWidgets($owner_type), $used_widgets);
334 <td id="widget-content-categ">'. $after .'</td>
342 function updateLayout($owner_id, $owner_type, $layout, $custom_layout) {
344 FROM layouts AS l INNER JOIN owner_layouts AS o ON(l.id = o.layout_id)
345 WHERE o.owner_type = $1
349 $req = db_query_params($sql,array($owner_type,$owner_id));
350 if ($data = db_fetch_array($req)) {
351 if ($this->_currentUserCanUpdateLayout($owner_id, $owner_type)) {
352 $old_scope = $data['scope'];
353 $old_layout_id = $data['id'];
354 $new_layout_id = null;
355 if ($layout == '-1' && is_array($custom_layout)) {
356 //Create a new layout based on the custom layout structure defined by the user
358 foreach($custom_layout as $widths) {
360 $cols = explode(',', $widths);
361 foreach($cols as $col) {
362 if ($width = (int)$col) {
370 //If the structure contains at least one column, create a new layout
372 $sql = "INSERT INTO layouts(name, description, scope)
373 VALUES ('custom', '', 'P')";
374 if ($res = db_query_params($sql,array())) {
375 if ($new_layout_id = db_insertid($res,'layouts', 'id')) {
376 //Create rows & columns
378 foreach($rows as $cols) {
379 $sql = "INSERT INTO layouts_rows(layout_id, rank)
381 if ($res = db_query_params($sql,array($new_layout_id,$rank++))) {
382 if ($row_id = db_insertid($res,'layouts_rows', 'id')) {
383 foreach($cols as $width) {
384 $sql = "INSERT INTO layouts_rows_columns(layout_row_id, width)
386 db_query_params($sql,array($row_id,$width));
395 $new_layout_id = $layout;
398 if ($new_layout_id) {
399 //Retrieve columns of old layout
400 $old = $this->_retrieveStructureOfLayout($old_layout_id);
402 //Retrieve columns of new layout
403 $new = $this->_retrieveStructureOfLayout($new_layout_id);
405 // Switch content from old columns to new columns
406 $last_new_col_id = null;
407 reset($new['columns']);
408 foreach($old['columns'] as $old_col) {
409 if (list(,$new_col) = each($new['columns'])) {
410 $last_new_col_id = $new_col['id'];
412 $sql = "UPDATE layouts_contents
419 db_query_params($sql,array($new_layout_id,$last_new_col_id,$owner_type,$owner_id,$old_layout_id,$old_col['id']));
421 $sql = "UPDATE owner_layouts
423 WHERE owner_type = $2
426 db_query_params($sql,array($new_layout_id,$owner_type,$owner_id,$old_layout_id));
428 //If the old layout is custom remove it
429 if ($old_scope != 'S') {
430 $structure = $this->_retrieveStructureOfLayout($old_layout_id);
431 foreach($structure['rows'] as $row) {
432 $sql = "DELETE FROM layouts_rows
434 db_query_params($sql,array($row['id']));
435 $sql = "DELETE FROM layouts_rows_columns
436 WHERE layout_row_id = $1";
437 db_query_params($sql,array($row['id']));
439 $sql = "DELETE FROM layouts
441 db_query_params($sql,array($old_layout_id));
447 $this->feedback($owner_id, $owner_type);
450 function _retrieveStructureOfLayout($layout_id) {
451 $structure = array('rows' => array(), 'columns' => array());
452 $sql = 'SELECT * FROM layouts_rows WHERE layout_id = $1 ORDER BY rank';
453 $req_rows = db_query_params($sql,array($layout_id));
454 while ($row = db_fetch_array($req_rows)) {
455 $structure['rows'][] = $row;
456 $sql = 'SELECT * FROM layouts_rows_columns WHERE layout_row_id =$1 ORDER BY id';
457 $req_cols = db_query_params($sql,array($row['id']));
458 while ($col = db_fetch_array($req_cols)) {
459 $structure['columns'][] = $col;
466 * _displayWidgetsSelectionForm - displays a widget selection form
470 * @param used_widgets
472 function _displayWidgetsSelectionForm($title, $widgets, $used_widgets) {
473 $hp = Codendi_HTMLPurifier::instance();
474 $additionnal_html = '';
475 if (count($widgets)) {
476 echo '<tr><td colspan="2">';
477 $categs = $this->getCategories($widgets);
478 $widget_rows = array();
479 if (count($categs)) {
480 // display the categories selector in left panel
481 foreach($categs as $c => $ws) {
482 $widget_rows[$c] = '<a class="widget-categ-switcher" href="#widget-categ-'. $c .'"><span>'. str_replace('-',' ', $hp->purify($c, CODENDI_PURIFIER_CONVERT_HTML)) .'</span></a>';
484 uksort($widget_rows, 'strnatcasecmp');
485 echo '<ul id="widget-categories">';
486 foreach($widget_rows as $row) {
487 echo '<li>'. $row .'</li>';
493 foreach($widgets as $widget_name) {
494 if ($widget = Widget::getInstance($widget_name)) {
495 if ($widget->isAvailable()) {
497 $row .= '<td>'. $widget->getTitle() . $widget->getInstallPreferences() .'</td>';
498 $row .= '<td class="align-right">';
499 if ($widget->isUnique() && in_array($widget_name, $used_widgets)) {
500 $row .= '<em>'. _("Already used") .'</em>';
502 $row .= '<input type="submit" name="name['. $widget_name .'][add]" value="'. _("Add") .'" />';
505 $widget_rows[$widget->getTitle()] = $row;
510 foreach($widget_rows as $row) {
511 echo '<tr class="'. (count($widget_rows) ? '' : util_get_alt_row_color($i++)) .'">'. $row .'</tr>';
514 if (count($categs)) {
515 foreach($categs as $c => $ws) {
517 $widget_rows = array();
518 // display widgets of the category
519 foreach($ws as $widget_name => $widget) {
521 $row .= '<div class="widget-preview '. $widget->getPreviewCssClass() .'">';
522 $row .= '<strong>'. $widget->getTitle() .'</strong>';
523 $row .= '<p>'. $widget->getDescription() .'</p>';
524 $row .= $widget->getInstallPreferences();
525 $row .= '</div><div style="text-align:right; border-bottom:1px solid #ddd; padding-bottom:10px; margin-bottom:20px;">';
526 if ($widget->isUnique() && in_array($widget_name, $used_widgets)) {
527 $row .= '<em>'. _("Already used") .'</em>';
529 $row .= '<input type="submit" name="name['. $widget_name .'][add]" value="'. _("Add") .'" />';
532 $widget_rows[$widget->getTitle()] = $row;
534 uksort($widget_rows, 'strnatcasecmp');
535 $additionnal_html .= '<div id="widget-categ-'. $c .'"><h4 class="boxtitle">'. $hp->purify($c, CODENDI_PURIFIER_CONVERT_HTML) .'</h4>';
536 foreach($widget_rows as $row) {
537 $additionnal_html .= $row;
539 $additionnal_html .= '</div>';
543 return $additionnal_html;
547 * getCategories - sort the widgets in their different categories
549 * @param array $widgets
550 * @return array (category => widgets)
552 function getCategories($widgets) {
554 foreach($widgets as $widget_name) {
555 if ($widget = Widget::getInstance($widget_name)) {
556 if ($widget->isAvailable()) {
557 $cs = explode(',', $widget->getCategory());
560 if (!isset($categ[$c])) {
561 $categ[$c] = array();
563 $categ[$c][$widget_name] = $widget;
574 * @param int owner_id
575 * @param char owner_type
576 * @param int layout_id
578 * @param object widget
579 * @param object request
581 function addWidget($owner_id, $owner_type, $layout_id, $name, &$widget, &$request) {
582 //Search for the right column. (The first used)
583 $sql = "SELECT u.column_id AS id
584 FROM layouts_contents AS u
585 LEFT JOIN (SELECT r.rank AS rank, c.id as id
586 FROM layouts_rows AS r INNER JOIN layouts_rows_columns AS c
587 ON (c.layout_row_id = r.id)
588 WHERE r.layout_id = $1) AS col
589 ON (u.column_id = col.id)
590 WHERE u.owner_type = $2
594 ORDER BY col.rank, col.id";
595 $res = db_query_params($sql,array($layout_id,$owner_type,$owner_id,$layout_id));
597 $column_id = db_result($res, 0, 'id');
599 $sql = "SELECT r.rank AS rank, c.id as id
600 FROM layouts_rows AS r
601 INNER JOIN layouts_rows_columns AS c
602 ON (c.layout_row_id = r.id)
603 WHERE r.layout_id = $1
605 $res = db_query_params($sql,array($layout_id));
606 $column_id = db_result($res, 0, 'id');
610 if ($widget->isUnique()) {
611 //unique widgets do not have content_id
614 $content_id = $widget->create($request);
617 //See if it already exists but not used
618 $sql = "SELECT column_id FROM layouts_contents
623 $res = db_query_params($sql,array($owner_type,$owner_id,$layout_id, $name));
625 if (db_numrows($res) && !$widget->isUnique() && db_result($res, 0, 'column_id') == 0) {
627 $sql = "SELECT min(rank) - 1 AS rank FROM layouts_contents WHERE owner_type =$1 AND owner_id = $2 AND layout_id = $3 AND column_id = $4 ";
628 $res = db_query_params($sql,array($owner_type, $owner_id, $layout_id,$column_id));
630 $rank = db_result($res, 0, 'rank');
633 $sql = "UPDATE layouts_contents
634 SET column_id = $1, rank = $2
635 WHERE owner_type = $3
639 $res = db_query_params($sql,array($column_id,$rank,$owner_type, $owner_id,$name, $layout_id));
643 $sql = "INSERT INTO layouts_contents(owner_type, owner_id, layout_id, column_id, name, content_id, rank)
644 SELECT R1.owner_type, R1.owner_id, R1.layout_id, R1.column_id, $1, $2, coalesce(R2.rank, 1) - 1
645 FROM ( SELECT $3::character varying(1) AS owner_type, $4::integer AS owner_id, $5::integer AS layout_id, $6::integer AS column_id ) AS R1
646 LEFT JOIN layouts_contents AS R2 USING ( owner_type, owner_id, layout_id, column_id )
649 $myfile=fopen('/tmp/debug','a');
650 $params = array($name,$content_id,$owner_type,$owner_id,$layout_id,$column_id);
651 fwrite($myfile, $sql." devient:\n");
652 fwrite($myfile, str_replace(array("$1","$2","$3","$4","$5","$6"),$params,$sql));
653 fwrite($myfile, "\n request content=".$request->get('content_id'));
654 db_query_params($sql,array($name,$content_id,$owner_type,$owner_id,$layout_id,$column_id));
657 $this->feedback($owner_id, $owner_type);
660 protected function feedback($owner_id, $owner_type) {
662 if ($owner_type == self::OWNER_TYPE_GROUP) {
663 //retrieve the short name of the project
664 if ($project = ProjectManager::instance()->getProject($owner_id)) {
665 $hp = Codendi_HTMLPurifier::instance();
666 $link = '/projects/'. $hp->purify($project->getUnixName(), CODENDI_PURIFIER_CONVERT_HTML) ;
668 } else if ($owner_type == self::OWNER_TYPE_USER) {
671 $GLOBALS['feedback'] .= vsprintf(_('Your <a href="%s">dashboard</a> has been updated.'), $link);
677 * @param int owner_id
678 * @param char owner_type
679 * @param int layout_id
681 * @param int instance_id
683 function removeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id, &$widget) {
684 $sql = "DELETE FROM layouts_contents WHERE owner_type =$1 AND owner_id = $2 AND layout_id = $3 AND name = $4 AND content_id = $5";
685 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
687 $widget->destroy($instance_id);
694 * @param int owner_id
695 * @param char owner_type
696 * @param int layout_id
698 * @param int instance_id
700 function mimizeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id) {
701 $sql = "UPDATE layouts_contents SET is_minimized = 1 WHERE owner_type = $1 AND owner_id = $2 AND layout_id = $3 AND name = $4 AND content_id = $5";
702 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
709 * @param int owner_id
710 * @param char owner_type
711 * @param int layout_id
713 * @param int instance_id
715 function maximizeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id) {
716 $sql = "UPDATE layouts_contents SET is_minimized = 0 WHERE owner_type =$1 AND owner_id =$2 AND layout_id = $3 AND name = $4 AND content_id = $5";
717 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
722 * displayWidgetPreferences
724 * @param int owner_id
725 * @param char owner_type
726 * @param int layout_id
728 * @param int instance_id
730 function displayWidgetPreferences($owner_id, $owner_type, $layout_id, $name, $instance_id) {
731 $sql = "UPDATE layouts_contents SET display_preferences = 1, is_minimized = 0 WHERE owner_type = $1 AND owner_id = $2 AND layout_id = $3 AND name = $4 AND content_id = $5";
732 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
737 * hideWidgetPreferences
739 * @param int owner_id
740 * @param char owner_type
741 * @param int layout_id
743 * @param int instance_id
745 function hideWidgetPreferences($owner_id, $owner_type, $layout_id, $name, $instance_id) {
746 $sql = "UPDATE layouts_contents SET display_preferences = 0 WHERE owner_type = $1 AND owner_id = $2 AND layout_id = $3 AND name = $4 AND content_id = $5";
747 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
754 * @param int owner_id
755 * @param char owner_type
756 * @param int layout_id
758 * @param int instance_id
760 function reorderLayout($owner_id, $owner_type, $layout_id, &$request) {
761 $keys = array_keys($_REQUEST);
762 foreach($keys as $key) {
763 if (preg_match('`widgetlayout_col_\d+`', $key)) {
765 $split = explode('_', $key);
766 $column_id = (int)$split[count($split)-1];
769 foreach($request->get($key) as $name) {
770 list($name, $id) = explode('-', $name);
771 $names[] = array($id, $name);
774 //Compute differences
775 $originals = array();
776 $sql = "SELECT * FROM layouts_contents WHERE owner_type = $1 AND owner_id = $2 AND column_id = $3 ORDER BY rank";
777 $res = db_query_params($sql,array($owner_type, $owner_id, $column_id));
779 while($data = db_fetch_array($res)) {
780 $originals[] = array($data['content_id'], $data['name']);
783 //delete removed contents
784 $deleted_names = $this->_array_diff_names($originals, $names);
785 if (count($deleted_names)) {
787 foreach($deleted_names as $id => $name) {
793 $_and .= " (name = '".$name[1]."' AND content_id = ". $name[0] .") ";
796 $sql = "UPDATE layouts_contents
798 WHERE owner_type = $1
802 $res = db_query_params($sql,array($owner_type, $owner_id, $column_id));
806 //Insert new contents
807 $added_names = $this->_array_diff_names($names, $originals);
808 if (count($added_names)) {
810 foreach($added_names as $name) {
816 $_and .= " (name = '".$name[1]."' AND content_id = ". $name[0] .") ";
819 //old and new column must be part of the same layout
820 $sql = 'UPDATE layouts_contents
822 WHERE owner_type = $2
826 $res = db_query_params($sql,array($column_id,$owner_type,$owner_id,$layout_id));
834 foreach($names as $name) {
835 $sql = 'UPDATE layouts_contents SET rank = $1 WHERE owner_type =$2 AND owner_id = $3 AND column_id = $4 AND name = $5 AND content_id = $6';
836 db_query_params($sql, array($rank++,$owner_type,$owner_id,$column_id,$name[1],$name[0]));
844 * compute the differences between two arrays
846 function _array_diff_names($tab1, $tab2) {
848 foreach($tab1 as $e1) {
851 while(!$found && list(,$e2) = each($tab2)) {
852 $found = !count(array_diff($e1, $e2));