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';
33 const OWNER_TYPE_GROUP = 'g';
34 const OWNER_TYPE_HOME = 'h';
39 * Display the default layout for the "owner". It my be the home page, the project summary page or /my/ page.
42 * @param char owner_type
44 function displayLayout($owner_id, $owner_type) {
45 $sql = "SELECT * from owner_layouts where owner_id=$1 and owner_type=$2";
46 $res = db_query_params($sql, array($owner_id, $owner_type));
47 if($res && db_numrows($res)<1) {
48 if($owner_type = self::OWNER_TYPE_USER) {
49 $this->createDefaultLayoutForUser($owner_id);
50 $this->displayLayout($owner_id,$owner_type);
52 $this->createDefaultLayoutForProject($owner_id,1);
53 $this->displayLayout($owner_id,$owner_type);
57 FROM layouts AS l INNER JOIN owner_layouts AS o ON(l.id = o.layout_id)
58 WHERE o.owner_type = $1
62 $req = db_query_params($sql, array($owner_type ,$owner_id));
63 if ($data = db_fetch_array($req)) {
64 $readonly = !$this->_currentUserCanUpdateLayout($owner_id, $owner_type);
66 // echo '<p class="customize"><a href="/widgets/widgets.php?owner='. $owner_type.$owner_id .'&layout_id='. $data['id'] .'">'. _("Customize") .'</a></p>';
67 // } else if ($owner_type === self::OWNER_TYPE_GROUP) {
70 $layout = new WidgetLayout($data['id'], $data['name'], $data['description'], $data['scope']);
71 $sql = 'SELECT * FROM layouts_rows WHERE layout_id = $1 ORDER BY rank';
72 $req_rows = db_query_params($sql,array($layout->id));
73 while ($data = db_fetch_array($req_rows)) {
74 $row = new WidgetLayout_Row($data['id'], $data['rank']);
75 $sql = 'SELECT * FROM layouts_rows_columns WHERE layout_row_id = $1';
76 $req_cols = db_query_params($sql,array($row->id));
77 while ($data = db_fetch_array($req_cols)) {
78 $col = new WidgetLayout_Row_Column($data['id'], $data['width']);
79 $sql = "SELECT * FROM layouts_contents WHERE owner_type = $1 AND owner_id = $2 AND column_id = $3 ORDER BY rank";
80 $req_content = db_query_params($sql,array($owner_type, $owner_id, $col->id));
81 while ($data = db_fetch_array($req_content)) {
82 $c = Widget::getInstance($data['name']);
83 if ($c && $c->isAvailable()) {
84 $c->loadContent($data['content_id']);
85 $col->add($c, $data['is_minimized'], $data['display_preferences']);
95 $layout->display($readonly, $owner_id, $owner_type);
101 * _currentUserCanUpdateLayout
103 * @param int owner_id
104 * @param char owner_type
105 * @return boolean true if the user dan uppdate the layout (add/remove widget, collapse, set preferences, ...)
107 function _currentUserCanUpdateLayout($owner_id, $owner_type) {
109 $request =& HTTPRequest::instance();
110 switch ($owner_type) {
111 case self::OWNER_TYPE_USER:
112 if (user_getid() == $owner_id) { //Current user can only update its own /my/ page
116 case self::OWNER_TYPE_GROUP:
117 if (UserManager::instance()->getCurrentUser()->is_super_user==true || user_ismember($request->get('group_id'), 'A')) { //Only project admin
121 case self::OWNER_TYPE_HOME:
130 * createDefaultLayoutForUser
132 * Create the first layout for the user and add some initial widgets:
138 * - MyMonitoredForums
139 * - and widgets of plugins if they want to listen to the event default_widgets_for_new_owner
141 * @param int owner_id The id of the newly created user
143 function createDefaultLayoutForUser($owner_id) {
144 $owner_type = self::OWNER_TYPE_USER;
145 $sql = "INSERT INTO owner_layouts(layout_id, is_default, owner_id, owner_type) VALUES (1, 1, $1, $2)";
146 if (db_query_params($sql, array($owner_id, $owner_type))) {
148 $sql = "INSERT INTO layouts_contents(owner_id, owner_type, layout_id, column_id, name, rank) VALUES ";
150 $args[] = "($1, $2, 1, 1, 'myprojects', 0)";
151 $args[] = "($1, $2, 1, 1, 'mybookmarks', 1)";
152 $args[] = "($1, $2, 1, 1, 'mymonitoredforums', 2)";
153 $args[] = "($1, $2, 1, 1, 'mysurveys', 4)";
154 $args[] = "($1, $2, 1, 2, 'myartifacts', 0)";
155 $args[] = "($1, $2, 1, 2, 'mymonitoredfp', 1)";
157 foreach($args as $a) {
158 db_query_params($sql.$a,array($owner_id,$owner_type));
161 /* $em =& EventManager::instance();
163 $em->processEvent('default_widgets_for_new_owner', array('widgets' => &$widgets, 'owner_type' => $owner_type));
164 foreach($widgets as $widget) {
165 $sql .= ",($13, $14, 1, $15, $16, $17)";
172 * createDefaultLayoutForProject
174 * Create the first layout for a new project, based on its parent template.
175 * Add some widgets based also on its parent configuration and on its service configuration.
177 * @param int group_id the id of the newly created project
178 * @param int template_id the id of the project template
180 function createDefaultLayoutForProject($group_id, $template_id) {
181 $pm = ProjectManager::instance();
182 $project = $pm->getProject($group_id);
183 $sql = "INSERT INTO owner_layouts(layout_id, is_default, owner_id, owner_type)
184 SELECT layout_id, is_default, $1, owner_type
186 WHERE owner_type = $2
189 if (db_query_params($sql,array($group_id, self::OWNER_TYPE_GROUP,$template_id))) {
190 $sql = "SELECT layout_id, column_id, name, rank, is_minimized, is_removed, display_preferences, content_id
191 FROM layouts_contents
192 WHERE owner_type = $1
195 if ($req = db_query_params($sql,array( self::OWNER_TYPE_GROUP,$template_id))) {
196 while($data = db_fetch_array($req)) {
197 $w = Widget::getInstance($data['name']);
199 $w->setOwner($template_id, self::OWNER_TYPE_GROUP);
200 if ($w->canBeUsedByProject($project)) {
201 $content_id = $w->cloneContent($w->content_id, $group_id, self::OWNER_TYPE_GROUP);
202 $sql = "INSERT INTO layouts_contents(owner_id, owner_type, content_id, layout_id, column_id, name, rank, is_minimized, is_removed, display_preferences)
203 VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);
205 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'] ));
216 * displayAvailableWidgets - Display all widget that the user can add to the layout
218 * @param int owner_id
219 * @param char owner_type
220 * @param int layout_id
222 function displayAvailableWidgets($owner_id, $owner_type, $layout_id) {
223 $used_widgets = array();
225 FROM layouts_contents
226 WHERE owner_type = $1
229 AND content_id = 0 AND column_id <> 0";
230 $res = db_query_params($sql,array($owner_type,$owner_id,$layout_id));
231 while($data = db_fetch_array($res)) {
232 $used_widgets[] = $data['name'];
234 echo '<ul class="widget_toolbar">';
235 $url = "/widgets/widgets.php?owner=".HTTPRequest::instance()->get('owner').
236 "&layout_id=".HTTPRequest::instance()->get('layout_id');
238 if ($update_layout = HTTPRequest::instance()->get('update') == 'layout') {
239 echo '<li><a href="'. $url .'">'. _("Add widgets") .'</a></li>';
240 echo '<li class="current"><a href="'. $url.'&update=layout' .'">'. _("Customize layout") .'</a></li>';
243 echo '<li class="current"><a href="'. $url .'">'. _("Add widgets") .'</a></li>';
244 echo '<li><a href="'. $url.'&update=layout' .'">'. _("Customize layout") .'</a></li>';
248 echo '<form action="/widgets/updatelayout.php?owner='. $owner_type.$owner_id .'&action='. $action .'&layout_id='. $layout_id .'" method="post">';
249 if ($update_layout) {
250 $sql = "SELECT * FROM layouts WHERE scope='S' ORDER BY id ";
251 $req_layouts = db_query_params($sql,array());
252 echo '<table cellspacing="0" cellpadding="0">';
254 while ($data = db_fetch_array($req_layouts)) {
255 $checked = $layout_id == $data['id'] ? 'checked="checked"' : '';
256 $is_custom = $is_custom && !$checked;
257 echo '<tr class="layout-manager-chooser '. ($checked ? 'layout-manager-chooser_selected' : '') .'" ><td>';
258 echo '<input type="radio" name="layout_id" value="'. $data['id'] .'" id="layout_'. $data['id'] .'" '. $checked .'/>';
260 echo '<label for="layout_'. $data['id'] .'">';
261 echo html_image('layout/'. strtolower(preg_replace('/(\W+)/', '-', $data['name'])) .'.png');
264 echo '<label for="layout_'. $data['id'] .'"><strong>'. $data['name'] .'</strong><br />';
265 echo $data['description'];
269 /* Custom layout are not available yet */
270 $checked = $is_custom ? 'checked="checked"' : '';
271 echo '<tr class="layout-manager-chooser '. ($checked ? 'layout-manager-chooser_selected' : '') .'"><td>';
272 echo '<input type="radio" name="layout_id" value="-1" id="layout_custom" '. $checked .'/>';
274 echo '<label for="layout_custom">';
275 echo html_image('layout/custom.png', array('style' => 'vertical-align:top;float:left;'));
278 echo '<label for="layout_custom"><strong>'. 'Custom' .'</strong><br />';
279 echo 'Define your own layout:';
281 echo '<table id="layout-manager" cellpadding="0" cellspacing="0">
284 <div class="layout-manager-row-add">+</div>';
285 $sql = 'SELECT * FROM layouts_rows WHERE layout_id = $1 ORDER BY rank';
286 $req_rows = db_query_params($sql,array($layout_id));
287 while ($data = db_fetch_array($req_rows)) {
288 echo '<table class="layout-manager-row" cellspacing="5" cellpadding="2" border="0">
290 <td class="layout-manager-column-add">+</td>';
291 $sql = 'SELECT * FROM layouts_rows_columns WHERE layout_row_id = $1';
292 $req_cols = db_query_params($sql,array($data['id']));
293 while ($data = db_fetch_array($req_cols)) {
294 echo '<td class="layout-manager-column">
295 <div class="layout-manager-column-remove">x</div>
296 <div class="layout-manager-column-width">
297 <input type="text" value="'. $data['width'] .'" size="1" maxlength="3" />%
300 <td class="layout-manager-column-add">+</td>';
304 <div class="layout-manager-row-add">+</div>';
311 echo '<input type="submit" id="save" value="'. _("Submit") .'" />';
314 echo '<table cellpadding="0" cellspacing="0">
318 echo '<table cellpadding="2" cellspacing="0">
320 $after .= $this->_displayWidgetsSelectionForm(sprintf(_("%s Widgets"), forge_get_config('forge_name')), Widget::getCodendiWidgets($owner_type), $used_widgets);
324 <td id="widget-content-categ">'. $after .'</td>
332 function updateLayout($owner_id, $owner_type, $layout, $custom_layout) {
334 FROM layouts AS l INNER JOIN owner_layouts AS o ON(l.id = o.layout_id)
335 WHERE o.owner_type = $1
339 $req = db_query_params($sql,array($owner_type,$owner_id));
340 if ($data = db_fetch_array($req)) {
341 if ($this->_currentUserCanUpdateLayout($owner_id, $owner_type)) {
342 $old_scope = $data['scope'];
343 $old_layout_id = $data['id'];
344 $new_layout_id = null;
345 if ($layout == '-1' && is_array($custom_layout)) {
346 //Create a new layout based on the custom layout structure defined by the user
348 foreach($custom_layout as $widths) {
350 $cols = explode(',', $widths);
351 foreach($cols as $col) {
352 if ($width = (int)$col) {
360 //If the structure contains at least one column, create a new layout
362 $sql = "INSERT INTO layouts(name, description, scope)
363 VALUES ('custom', '', 'P')";
364 if ($res = db_query_params($sql,array())) {
365 if ($new_layout_id = db_insertid($res,'layouts', 'id')) {
366 //Create rows & columns
368 foreach($rows as $cols) {
369 $sql = "INSERT INTO layouts_rows(layout_id, rank)
371 if ($res = db_query_params($sql,array($new_layout_id,$rank++))) {
372 if ($row_id = db_insertid($res,'layouts_rows', 'id')) {
373 foreach($cols as $width) {
374 $sql = "INSERT INTO layouts_rows_columns(layout_row_id, width)
376 db_query_params($sql,array($row_id,$width));
385 $new_layout_id = $layout;
388 if ($new_layout_id) {
389 //Retrieve columns of old layout
390 $old = $this->_retrieveStructureOfLayout($old_layout_id);
392 //Retrieve columns of new layout
393 $new = $this->_retrieveStructureOfLayout($new_layout_id);
395 // Switch content from old columns to new columns
396 $last_new_col_id = null;
397 reset($new['columns']);
398 foreach($old['columns'] as $old_col) {
399 if (list(,$new_col) = each($new['columns'])) {
400 $last_new_col_id = $new_col['id'];
402 $sql = "UPDATE layouts_contents
409 db_query_params($sql,array($new_layout_id,$last_new_col_id,$owner_type,$owner_id,$old_layout_id,$old_col['id']));
411 $sql = "UPDATE owner_layouts
413 WHERE owner_type = $2
416 db_query_params($sql,array($new_layout_id,$owner_type,$owner_id,$old_layout_id));
418 //If the old layout is custom remove it
419 if ($old_scope != 'S') {
420 $structure = $this->_retrieveStructureOfLayout($old_layout_id);
421 foreach($structure['rows'] as $row) {
422 $sql = "DELETE FROM layouts_rows
424 db_query_params($sql,array($row['id']));
425 $sql = "DELETE FROM layouts_rows_columns
426 WHERE layout_row_id = $1";
427 db_query_params($sql,array($row['id']));
429 $sql = "DELETE FROM layouts
431 db_query_params($sql,array($old_layout_id));
437 $this->feedback($owner_id, $owner_type);
440 function _retrieveStructureOfLayout($layout_id) {
441 $structure = array('rows' => array(), 'columns' => array());
442 $sql = 'SELECT * FROM layouts_rows WHERE layout_id = $1 ORDER BY rank';
443 $req_rows = db_query_params($sql,array($layout_id));
444 while ($row = db_fetch_array($req_rows)) {
445 $structure['rows'][] = $row;
446 $sql = 'SELECT * FROM layouts_rows_columns WHERE layout_row_id =$1 ORDER BY id';
447 $req_cols = db_query_params($sql,array($row['id']));
448 while ($col = db_fetch_array($req_cols)) {
449 $structure['columns'][] = $col;
456 * _displayWidgetsSelectionForm
460 * @param used_widgets
462 function _displayWidgetsSelectionForm($title, $widgets, $used_widgets) {
463 $hp = Codendi_HTMLPurifier::instance();
464 $additionnal_html = '';
465 if (count($widgets)) {
466 echo '<tr><td colspan="2">';
467 $categs = $this->getCategories($widgets);
468 $widget_rows = array();
469 if (count($categs)) {
470 foreach($categs as $c => $ws) {
471 $widget_rows[$c] = '<a class="widget-categ-switcher" href="#widget-categ-'. $c .'"><span>'. str_replace('-',' ', $hp->purify($c, CODENDI_PURIFIER_CONVERT_HTML)) .'</span></a>';
473 uksort($widget_rows, 'strnatcasecmp');
474 echo '<ul id="widget-categories">';
475 foreach($widget_rows as $row) {
476 echo '<li>'. $row .'</li>';
482 foreach($widgets as $widget_name) {
483 if ($widget = Widget::getInstance($widget_name)) {
484 if ($widget->isAvailable()) {
486 $row .= '<td>'. $widget->getTitle() . $widget->getInstallPreferences() .'</td>';
487 $row .= '<td align="right">';
488 if ($widget->isUnique() && in_array($widget_name, $used_widgets)) {
489 $row .= '<em>'. _("Already used") .'</em>';
491 $row .= '<input type="submit" name="name['. $widget_name .'][add]" value="'. _("Add") .'" />';
494 $widget_rows[$widget->getTitle()] = $row;
499 foreach($widget_rows as $row) {
500 echo '<tr class="'. (count($widget_rows) ? '' : util_get_alt_row_color($i++)) .'">'. $row .'</tr>';
503 if (count($categs)) {
504 foreach($categs as $c => $ws) {
506 $widget_rows = array();
507 foreach($ws as $widget_name => $widget) {
509 $row .= '<div class="widget-preview '. $widget->getPreviewCssClass() .'">';
510 $row .= '<strong>'. $widget->getTitle() .'</strong>';
511 $row .= '<p>'. $widget->getDescription() .'</p>';
512 $row .= $widget->getInstallPreferences();
513 $row .= '</div><div style="text-align:right; border-bottom:1px solid #ddd; padding-bottom:10px; margin-bottom:20px;">';
514 if ($widget->isUnique() && in_array($widget_name, $used_widgets)) {
515 $row .= '<em>'. _("Already used") .'</em>';
517 $row .= '<input type="submit" name="name['. $widget_name .'][add]" value="'. _("Add") .'" />';
520 $widget_rows[$widget->getTitle()] = $row;
522 uksort($widget_rows, 'strnatcasecmp');
523 $additionnal_html .= '<div id="widget-categ-'. $c .'"><h4 class="boxtitle">'. $hp->purify($c, CODENDI_PURIFIER_CONVERT_HTML) .'</h4>';
524 foreach($widget_rows as $row) {
525 $additionnal_html .= $row;
527 $additionnal_html .= '</div>';
531 return $additionnal_html;
533 function getCategories($widgets) {
535 foreach($widgets as $widget_name) {
536 if ($widget = Widget::getInstance($widget_name)) {
537 if ($widget->isAvailable()) {
538 $cs = explode(',', $widget->getCategory());
541 if (!isset($categ[$c])) {
542 $categ[$c] = array();
544 $categ[$c][$widget_name] = $widget;
555 * @param int owner_id
556 * @param char owner_type
557 * @param int layout_id
559 * @param object widget
560 * @param object request
562 function addWidget($owner_id, $owner_type, $layout_id, $name, &$widget, &$request) {
563 //Search for the right column. (The first used)
564 $sql = "SELECT u.column_id AS id
565 FROM layouts_contents AS u
566 LEFT JOIN (SELECT r.rank AS rank, c.id as id
567 FROM layouts_rows AS r INNER JOIN layouts_rows_columns AS c
568 ON (c.layout_row_id = r.id)
569 WHERE r.layout_id = $1) AS col
570 ON (u.column_id = col.id)
571 WHERE u.owner_type = $2
575 ORDER BY col.rank, col.id";
576 $res = db_query_params($sql,array($layout_id,$owner_type,$owner_id,$layout_id));
578 $column_id = db_result($res, 0, 'id');
580 $sql = "SELECT r.rank AS rank, c.id as id
581 FROM layouts_rows AS r
582 INNER JOIN layouts_rows_columns AS c
583 ON (c.layout_row_id = r.id)
584 WHERE r.layout_id = $1
586 $res = db_query_params($sql,array($layout_id));
587 $column_id = db_result($res, 0, 'id');
591 if ($widget->isUnique()) {
592 //unique widgets do not have content_id
595 $content_id = $widget->create($request);
598 //See if it already exists but not used
599 $sql = "SELECT column_id FROM layouts_contents
604 $res = db_query_params($sql,array($owner_type,$owner_id,$layout_id, $name));
606 if (db_numrows($res) && !$widget->isUnique() && db_result($res, 0, 'column_id') == 0) {
608 $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 ";
609 $res = db_query_params($sql,array($owner_type, $owner_id, $layout_id,$column_id));
611 $rank = db_result($res, 0, 'rank');
614 $sql = "UPDATE layouts_contents
615 SET column_id = $1, rank = $2
616 WHERE owner_type = $3
620 $res = db_query_params($sql,array($column_id,$rank,$owner_type, $owner_id,$name, $layout_id));
624 $sql = "INSERT INTO layouts_contents(owner_type, owner_id, layout_id, column_id, name, content_id, rank)
625 SELECT R1.owner_type, R1.owner_id, R1.layout_id, R1.column_id, $1, $2, coalesce(R2.rank, 1) - 1
626 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
627 LEFT JOIN layouts_contents AS R2 USING ( owner_type, owner_id, layout_id, column_id )
630 $myfile=fopen('/tmp/debug','a');
631 $params = array($name,$content_id,$owner_type,$owner_id,$layout_id,$column_id);
632 fwrite($myfile, $sql." devient:\n");
633 fwrite($myfile, str_replace(array("$1","$2","$3","$4","$5","$6"),$params,$sql));
634 fwrite($myfile, "\n request content=".$request->get('content_id'));
635 db_query_params($sql,array($name,$content_id,$owner_type,$owner_id,$layout_id,$column_id));
638 $this->feedback($owner_id, $owner_type);
641 protected function feedback($owner_id, $owner_type) {
643 if ($owner_type == self::OWNER_TYPE_GROUP) {
644 //retrieve the short name of the project
645 if ($project = ProjectManager::instance()->getProject($owner_id)) {
646 $hp = Codendi_HTMLPurifier::instance();
647 $link = '/projects/'. $hp->purify($project->getUnixName(), CODENDI_PURIFIER_CONVERT_HTML) ;
649 } else if ($owner_type == self::OWNER_TYPE_USER) {
652 $GLOBALS['feedback'] .= vsprintf(_('Your <a href="%s">dashboard</a> has been updated.'), $link);
658 * @param int owner_id
659 * @param char owner_type
660 * @param int layout_id
662 * @param int instance_id
664 function removeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id, &$widget) {
665 $sql = "DELETE FROM layouts_contents WHERE owner_type =$1 AND owner_id = $2 AND layout_id = $3 AND name = $4 AND content_id = $5";
666 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
668 $widget->destroy($instance_id);
675 * @param int owner_id
676 * @param char owner_type
677 * @param int layout_id
679 * @param int instance_id
681 function mimizeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id) {
682 $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";
683 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
690 * @param int owner_id
691 * @param char owner_type
692 * @param int layout_id
694 * @param int instance_id
696 function maximizeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id) {
697 $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";
698 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
703 * displayWidgetPreferences
705 * @param int owner_id
706 * @param char owner_type
707 * @param int layout_id
709 * @param int instance_id
711 function displayWidgetPreferences($owner_id, $owner_type, $layout_id, $name, $instance_id) {
712 $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";
713 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
718 * hideWidgetPreferences
720 * @param int owner_id
721 * @param char owner_type
722 * @param int layout_id
724 * @param int instance_id
726 function hideWidgetPreferences($owner_id, $owner_type, $layout_id, $name, $instance_id) {
727 $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";
728 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
735 * @param int owner_id
736 * @param char owner_type
737 * @param int layout_id
739 * @param int instance_id
741 function reorderLayout($owner_id, $owner_type, $layout_id, &$request) {
742 $keys = array_keys($_REQUEST);
743 foreach($keys as $key) {
744 if (preg_match('`widgetlayout_col_\d+`', $key)) {
746 $split = explode('_', $key);
747 $column_id = (int)$split[count($split)-1];
750 foreach($request->get($key) as $name) {
751 list($name, $id) = explode('-', $name);
752 $names[] = array($id, $name);
755 //Compute differences
756 $originals = array();
757 $sql = "SELECT * FROM layouts_contents WHERE owner_type = $1 AND owner_id = $2 AND column_id = $3 ORDER BY rank";
758 $res = db_query_params($sql,array($owner_type, $owner_id, $column_id));
760 while($data = db_fetch_array($res)) {
761 $originals[] = array($data['content_id'], $data['name']);
764 //delete removed contents
765 $deleted_names = $this->_array_diff_names($originals, $names);
766 if (count($deleted_names)) {
768 foreach($deleted_names as $id => $name) {
774 $_and .= " (name = '".$name[1]."' AND content_id = ". $name[0] .") ";
777 $sql = "UPDATE layouts_contents
779 WHERE owner_type = $1
783 $res = db_query_params($sql,array($owner_type, $owner_id, $column_id));
787 //Insert new contents
788 $added_names = $this->_array_diff_names($names, $originals);
789 if (count($added_names)) {
791 foreach($added_names as $name) {
797 $_and .= " (name = '".$name[1]."' AND content_id = ". $name[0] .") ";
800 //old and new column must be part of the same layout
801 $sql = 'UPDATE layouts_contents
803 WHERE owner_type = $2
807 $res = db_query_params($sql,array($column_id,$owner_type,$owner_id,$layout_id));
815 foreach($names as $name) {
816 $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';
817 db_query_params($sql, array($rank++,$owner_type,$owner_id,$column_id,$name[1],$name[0]));
825 * compute the differences between two arrays
827 function _array_diff_names($tab1, $tab2) {
829 foreach($tab1 as $e1) {
832 while(!$found && list(,$e2) = each($tab2)) {
833 $found = !count(array_diff($e1, $e2));