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 (UserManager::instance()->getCurrentUser()->is_super_user==true || user_ismember($request->get('group_id'), 'A')) { //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 echo '<ul class="widget_toolbar">';
240 $url = "/widgets/widgets.php?owner=".HTTPRequest::instance()->get('owner').
241 "&layout_id=".HTTPRequest::instance()->get('layout_id');
243 if ($update_layout = HTTPRequest::instance()->get('update') == 'layout') {
244 echo '<li><a href="'. $url .'">'. _("Add widgets") .'</a></li>';
245 echo '<li class="current"><a href="'. $url.'&update=layout' .'">'. _("Customize layout") .'</a></li>';
248 echo '<li class="current"><a href="'. $url .'">'. _("Add widgets") .'</a></li>';
249 echo '<li><a href="'. $url.'&update=layout' .'">'. _("Customize layout") .'</a></li>';
253 echo '<form action="/widgets/updatelayout.php?owner='. $owner_type.$owner_id .'&action='. $action .'&layout_id='. $layout_id .'" method="post">';
254 if ($update_layout) {
255 $sql = "SELECT * FROM layouts WHERE scope='S' ORDER BY id ";
256 $req_layouts = db_query_params($sql,array());
257 echo '<table cellspacing="0" cellpadding="0">';
259 while ($data = db_fetch_array($req_layouts)) {
260 $checked = $layout_id == $data['id'] ? 'checked="checked"' : '';
261 $is_custom = $is_custom && !$checked;
262 echo '<tr class="layout-manager-chooser '. ($checked ? 'layout-manager-chooser_selected' : '') .'" ><td>';
263 echo '<input type="radio" name="layout_id" value="'. $data['id'] .'" id="layout_'. $data['id'] .'" '. $checked .'/>';
265 echo '<label for="layout_'. $data['id'] .'">';
266 echo html_image('layout/'. strtolower(preg_replace('/(\W+)/', '-', $data['name'])) .'.png');
269 echo '<label for="layout_'. $data['id'] .'"><strong>'. $data['name'] .'</strong><br />';
270 echo $data['description'];
274 /* Custom layout are not available yet */
275 $checked = $is_custom ? 'checked="checked"' : '';
276 echo '<tr class="layout-manager-chooser '. ($checked ? 'layout-manager-chooser_selected' : '') .'"><td>';
277 echo '<input type="radio" name="layout_id" value="-1" id="layout_custom" '. $checked .'/>';
279 echo '<label for="layout_custom">';
280 echo html_image('layout/custom.png', array('style' => 'vertical-align:top;float:left;'));
283 echo '<label for="layout_custom"><strong>'. 'Custom' .'</strong><br />';
284 echo 'Define your own layout:';
286 echo '<table id="layout-manager" cellpadding="0" cellspacing="0">
289 <div class="layout-manager-row-add">+</div>';
290 $sql = 'SELECT * FROM layouts_rows WHERE layout_id = $1 ORDER BY rank';
291 $req_rows = db_query_params($sql,array($layout_id));
292 while ($data = db_fetch_array($req_rows)) {
293 echo '<table class="layout-manager-row" cellspacing="5" cellpadding="2" border="0">
295 <td class="layout-manager-column-add">+</td>';
296 $sql = 'SELECT * FROM layouts_rows_columns WHERE layout_row_id = $1';
297 $req_cols = db_query_params($sql,array($data['id']));
298 while ($data = db_fetch_array($req_cols)) {
299 echo '<td class="layout-manager-column">
300 <div class="layout-manager-column-remove">x</div>
301 <div class="layout-manager-column-width">
302 <input type="text" value="'. $data['width'] .'" size="1" maxlength="3" />%
305 <td class="layout-manager-column-add">+</td>';
309 <div class="layout-manager-row-add">+</div>';
316 echo '<input type="submit" id="save" value="'. _("Submit") .'" />';
318 // display the widget selection form
320 echo '<table cellpadding="0" cellspacing="0">
324 echo '<table cellpadding="2" cellspacing="0">
326 $after .= $this->_displayWidgetsSelectionForm(sprintf(_("%s Widgets"), forge_get_config('forge_name')), Widget::getCodendiWidgets($owner_type), $used_widgets);
330 <td id="widget-content-categ">'. $after .'</td>
338 function updateLayout($owner_id, $owner_type, $layout, $custom_layout) {
340 FROM layouts AS l INNER JOIN owner_layouts AS o ON(l.id = o.layout_id)
341 WHERE o.owner_type = $1
345 $req = db_query_params($sql,array($owner_type,$owner_id));
346 if ($data = db_fetch_array($req)) {
347 if ($this->_currentUserCanUpdateLayout($owner_id, $owner_type)) {
348 $old_scope = $data['scope'];
349 $old_layout_id = $data['id'];
350 $new_layout_id = null;
351 if ($layout == '-1' && is_array($custom_layout)) {
352 //Create a new layout based on the custom layout structure defined by the user
354 foreach($custom_layout as $widths) {
356 $cols = explode(',', $widths);
357 foreach($cols as $col) {
358 if ($width = (int)$col) {
366 //If the structure contains at least one column, create a new layout
368 $sql = "INSERT INTO layouts(name, description, scope)
369 VALUES ('custom', '', 'P')";
370 if ($res = db_query_params($sql,array())) {
371 if ($new_layout_id = db_insertid($res,'layouts', 'id')) {
372 //Create rows & columns
374 foreach($rows as $cols) {
375 $sql = "INSERT INTO layouts_rows(layout_id, rank)
377 if ($res = db_query_params($sql,array($new_layout_id,$rank++))) {
378 if ($row_id = db_insertid($res,'layouts_rows', 'id')) {
379 foreach($cols as $width) {
380 $sql = "INSERT INTO layouts_rows_columns(layout_row_id, width)
382 db_query_params($sql,array($row_id,$width));
391 $new_layout_id = $layout;
394 if ($new_layout_id) {
395 //Retrieve columns of old layout
396 $old = $this->_retrieveStructureOfLayout($old_layout_id);
398 //Retrieve columns of new layout
399 $new = $this->_retrieveStructureOfLayout($new_layout_id);
401 // Switch content from old columns to new columns
402 $last_new_col_id = null;
403 reset($new['columns']);
404 foreach($old['columns'] as $old_col) {
405 if (list(,$new_col) = each($new['columns'])) {
406 $last_new_col_id = $new_col['id'];
408 $sql = "UPDATE layouts_contents
415 db_query_params($sql,array($new_layout_id,$last_new_col_id,$owner_type,$owner_id,$old_layout_id,$old_col['id']));
417 $sql = "UPDATE owner_layouts
419 WHERE owner_type = $2
422 db_query_params($sql,array($new_layout_id,$owner_type,$owner_id,$old_layout_id));
424 //If the old layout is custom remove it
425 if ($old_scope != 'S') {
426 $structure = $this->_retrieveStructureOfLayout($old_layout_id);
427 foreach($structure['rows'] as $row) {
428 $sql = "DELETE FROM layouts_rows
430 db_query_params($sql,array($row['id']));
431 $sql = "DELETE FROM layouts_rows_columns
432 WHERE layout_row_id = $1";
433 db_query_params($sql,array($row['id']));
435 $sql = "DELETE FROM layouts
437 db_query_params($sql,array($old_layout_id));
443 $this->feedback($owner_id, $owner_type);
446 function _retrieveStructureOfLayout($layout_id) {
447 $structure = array('rows' => array(), 'columns' => array());
448 $sql = 'SELECT * FROM layouts_rows WHERE layout_id = $1 ORDER BY rank';
449 $req_rows = db_query_params($sql,array($layout_id));
450 while ($row = db_fetch_array($req_rows)) {
451 $structure['rows'][] = $row;
452 $sql = 'SELECT * FROM layouts_rows_columns WHERE layout_row_id =$1 ORDER BY id';
453 $req_cols = db_query_params($sql,array($row['id']));
454 while ($col = db_fetch_array($req_cols)) {
455 $structure['columns'][] = $col;
462 * _displayWidgetsSelectionForm - displays a widget selection form
466 * @param used_widgets
468 function _displayWidgetsSelectionForm($title, $widgets, $used_widgets) {
469 $hp = Codendi_HTMLPurifier::instance();
470 $additionnal_html = '';
471 if (count($widgets)) {
472 echo '<tr><td colspan="2">';
473 $categs = $this->getCategories($widgets);
474 $widget_rows = array();
475 if (count($categs)) {
476 // display the categories selector in left panel
477 foreach($categs as $c => $ws) {
478 $widget_rows[$c] = '<a class="widget-categ-switcher" href="#widget-categ-'. $c .'"><span>'. str_replace('-',' ', $hp->purify($c, CODENDI_PURIFIER_CONVERT_HTML)) .'</span></a>';
480 uksort($widget_rows, 'strnatcasecmp');
481 echo '<ul id="widget-categories">';
482 foreach($widget_rows as $row) {
483 echo '<li>'. $row .'</li>';
489 foreach($widgets as $widget_name) {
490 if ($widget = Widget::getInstance($widget_name)) {
491 if ($widget->isAvailable()) {
493 $row .= '<td>'. $widget->getTitle() . $widget->getInstallPreferences() .'</td>';
494 $row .= '<td align="right">';
495 if ($widget->isUnique() && in_array($widget_name, $used_widgets)) {
496 $row .= '<em>'. _("Already used") .'</em>';
498 $row .= '<input type="submit" name="name['. $widget_name .'][add]" value="'. _("Add") .'" />';
501 $widget_rows[$widget->getTitle()] = $row;
506 foreach($widget_rows as $row) {
507 echo '<tr class="'. (count($widget_rows) ? '' : util_get_alt_row_color($i++)) .'">'. $row .'</tr>';
510 if (count($categs)) {
511 foreach($categs as $c => $ws) {
513 $widget_rows = array();
514 // display widgets of the category
515 foreach($ws as $widget_name => $widget) {
517 $row .= '<div class="widget-preview '. $widget->getPreviewCssClass() .'">';
518 $row .= '<strong>'. $widget->getTitle() .'</strong>';
519 $row .= '<p>'. $widget->getDescription() .'</p>';
520 $row .= $widget->getInstallPreferences();
521 $row .= '</div><div style="text-align:right; border-bottom:1px solid #ddd; padding-bottom:10px; margin-bottom:20px;">';
522 if ($widget->isUnique() && in_array($widget_name, $used_widgets)) {
523 $row .= '<em>'. _("Already used") .'</em>';
525 $row .= '<input type="submit" name="name['. $widget_name .'][add]" value="'. _("Add") .'" />';
528 $widget_rows[$widget->getTitle()] = $row;
530 uksort($widget_rows, 'strnatcasecmp');
531 $additionnal_html .= '<div id="widget-categ-'. $c .'"><h4 class="boxtitle">'. $hp->purify($c, CODENDI_PURIFIER_CONVERT_HTML) .'</h4>';
532 foreach($widget_rows as $row) {
533 $additionnal_html .= $row;
535 $additionnal_html .= '</div>';
539 return $additionnal_html;
543 * getCategories - sort the widgets in their different categories
545 * @param array $widgets
546 * @return array (category => widgets)
548 function getCategories($widgets) {
550 foreach($widgets as $widget_name) {
551 if ($widget = Widget::getInstance($widget_name)) {
552 if ($widget->isAvailable()) {
553 $cs = explode(',', $widget->getCategory());
556 if (!isset($categ[$c])) {
557 $categ[$c] = array();
559 $categ[$c][$widget_name] = $widget;
570 * @param int owner_id
571 * @param char owner_type
572 * @param int layout_id
574 * @param object widget
575 * @param object request
577 function addWidget($owner_id, $owner_type, $layout_id, $name, &$widget, &$request) {
578 //Search for the right column. (The first used)
579 $sql = "SELECT u.column_id AS id
580 FROM layouts_contents AS u
581 LEFT JOIN (SELECT r.rank AS rank, c.id as id
582 FROM layouts_rows AS r INNER JOIN layouts_rows_columns AS c
583 ON (c.layout_row_id = r.id)
584 WHERE r.layout_id = $1) AS col
585 ON (u.column_id = col.id)
586 WHERE u.owner_type = $2
590 ORDER BY col.rank, col.id";
591 $res = db_query_params($sql,array($layout_id,$owner_type,$owner_id,$layout_id));
593 $column_id = db_result($res, 0, 'id');
595 $sql = "SELECT r.rank AS rank, c.id as id
596 FROM layouts_rows AS r
597 INNER JOIN layouts_rows_columns AS c
598 ON (c.layout_row_id = r.id)
599 WHERE r.layout_id = $1
601 $res = db_query_params($sql,array($layout_id));
602 $column_id = db_result($res, 0, 'id');
606 if ($widget->isUnique()) {
607 //unique widgets do not have content_id
610 $content_id = $widget->create($request);
613 //See if it already exists but not used
614 $sql = "SELECT column_id FROM layouts_contents
619 $res = db_query_params($sql,array($owner_type,$owner_id,$layout_id, $name));
621 if (db_numrows($res) && !$widget->isUnique() && db_result($res, 0, 'column_id') == 0) {
623 $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 ";
624 $res = db_query_params($sql,array($owner_type, $owner_id, $layout_id,$column_id));
626 $rank = db_result($res, 0, 'rank');
629 $sql = "UPDATE layouts_contents
630 SET column_id = $1, rank = $2
631 WHERE owner_type = $3
635 $res = db_query_params($sql,array($column_id,$rank,$owner_type, $owner_id,$name, $layout_id));
639 $sql = "INSERT INTO layouts_contents(owner_type, owner_id, layout_id, column_id, name, content_id, rank)
640 SELECT R1.owner_type, R1.owner_id, R1.layout_id, R1.column_id, $1, $2, coalesce(R2.rank, 1) - 1
641 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
642 LEFT JOIN layouts_contents AS R2 USING ( owner_type, owner_id, layout_id, column_id )
645 $myfile=fopen('/tmp/debug','a');
646 $params = array($name,$content_id,$owner_type,$owner_id,$layout_id,$column_id);
647 fwrite($myfile, $sql." devient:\n");
648 fwrite($myfile, str_replace(array("$1","$2","$3","$4","$5","$6"),$params,$sql));
649 fwrite($myfile, "\n request content=".$request->get('content_id'));
650 db_query_params($sql,array($name,$content_id,$owner_type,$owner_id,$layout_id,$column_id));
653 $this->feedback($owner_id, $owner_type);
656 protected function feedback($owner_id, $owner_type) {
658 if ($owner_type == self::OWNER_TYPE_GROUP) {
659 //retrieve the short name of the project
660 if ($project = ProjectManager::instance()->getProject($owner_id)) {
661 $hp = Codendi_HTMLPurifier::instance();
662 $link = '/projects/'. $hp->purify($project->getUnixName(), CODENDI_PURIFIER_CONVERT_HTML) ;
664 } else if ($owner_type == self::OWNER_TYPE_USER) {
667 $GLOBALS['feedback'] .= vsprintf(_('Your <a href="%s">dashboard</a> has been updated.'), $link);
673 * @param int owner_id
674 * @param char owner_type
675 * @param int layout_id
677 * @param int instance_id
679 function removeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id, &$widget) {
680 $sql = "DELETE FROM layouts_contents WHERE owner_type =$1 AND owner_id = $2 AND layout_id = $3 AND name = $4 AND content_id = $5";
681 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
683 $widget->destroy($instance_id);
690 * @param int owner_id
691 * @param char owner_type
692 * @param int layout_id
694 * @param int instance_id
696 function mimizeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id) {
697 $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";
698 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
705 * @param int owner_id
706 * @param char owner_type
707 * @param int layout_id
709 * @param int instance_id
711 function maximizeWidget($owner_id, $owner_type, $layout_id, $name, $instance_id) {
712 $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";
713 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
718 * displayWidgetPreferences
720 * @param int owner_id
721 * @param char owner_type
722 * @param int layout_id
724 * @param int instance_id
726 function displayWidgetPreferences($owner_id, $owner_type, $layout_id, $name, $instance_id) {
727 $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";
728 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
733 * hideWidgetPreferences
735 * @param int owner_id
736 * @param char owner_type
737 * @param int layout_id
739 * @param int instance_id
741 function hideWidgetPreferences($owner_id, $owner_type, $layout_id, $name, $instance_id) {
742 $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";
743 db_query_params($sql,array($owner_type,$owner_id,$layout_id,$name,$instance_id));
750 * @param int owner_id
751 * @param char owner_type
752 * @param int layout_id
754 * @param int instance_id
756 function reorderLayout($owner_id, $owner_type, $layout_id, &$request) {
757 $keys = array_keys($_REQUEST);
758 foreach($keys as $key) {
759 if (preg_match('`widgetlayout_col_\d+`', $key)) {
761 $split = explode('_', $key);
762 $column_id = (int)$split[count($split)-1];
765 foreach($request->get($key) as $name) {
766 list($name, $id) = explode('-', $name);
767 $names[] = array($id, $name);
770 //Compute differences
771 $originals = array();
772 $sql = "SELECT * FROM layouts_contents WHERE owner_type = $1 AND owner_id = $2 AND column_id = $3 ORDER BY rank";
773 $res = db_query_params($sql,array($owner_type, $owner_id, $column_id));
775 while($data = db_fetch_array($res)) {
776 $originals[] = array($data['content_id'], $data['name']);
779 //delete removed contents
780 $deleted_names = $this->_array_diff_names($originals, $names);
781 if (count($deleted_names)) {
783 foreach($deleted_names as $id => $name) {
789 $_and .= " (name = '".$name[1]."' AND content_id = ". $name[0] .") ";
792 $sql = "UPDATE layouts_contents
794 WHERE owner_type = $1
798 $res = db_query_params($sql,array($owner_type, $owner_id, $column_id));
802 //Insert new contents
803 $added_names = $this->_array_diff_names($names, $originals);
804 if (count($added_names)) {
806 foreach($added_names as $name) {
812 $_and .= " (name = '".$name[1]."' AND content_id = ". $name[0] .") ";
815 //old and new column must be part of the same layout
816 $sql = 'UPDATE layouts_contents
818 WHERE owner_type = $2
822 $res = db_query_params($sql,array($column_id,$owner_type,$owner_id,$layout_id));
830 foreach($names as $name) {
831 $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';
832 db_query_params($sql, array($rank++,$owner_type,$owner_id,$column_id,$name[1],$name[0]));
840 * compute the differences between two arrays
842 function _array_diff_names($tab1, $tab2) {
844 foreach($tab1 as $e1) {
847 while(!$found && list(,$e2) = each($tab2)) {
848 $found = !count(array_diff($e1, $e2));