Move requested gadget in layout

This wish/proposal has been added in CVS by Jonathan Hernandez, thanks for the idea and the code ;-)

Original proposal

Ok.

Currently, Jaws allways draws (render) the requested gadget in the main section at first. If we define in the layout another gadget, it’s displayed after the requested gadget (main gadget). We can define the order of the additional gadgets in the main section, but not the requested gadget.

For example, if we need to display the banner gadget before any other requested gadget, simplly we can’t.

So, I wrote some changes in order to achieve this behavior.

Let’s start with index.php. Here arrange some things and call up a new function display_gadgets(). This function displays all the gadgets inclusive the requested one, in the order stablished in the layout structure.

<?PHP
/*
 * index.php : index page for jaws
 *
 * Author: Jonathan Hernandez  <nomeescriban@yomero.org.mx>
 *
 * <c> 2002, 2003 JaWs
 */
 
require_once ('./include/config.php');
$GLOBALS["app"]->init();
$gadget = $_REQUEST["gadget"];
if ($gadget)
{
	// Check for valid gadget identificator
 
	if(ereg('[^A-Za-z0-9_-]', $gadget)){
		jaws_error ("Invalid gadget name",__file__,__line__);
	}
 
	// Check if gadget is enabled
        if ($GLOBALS["app"]->registry->get_value("/gadgets/".$gadget."/enabled") != "true") {
		jaws_error ("Gadget is not enabled",__file__,__line__);
	}
 
	// Check if file exists
	$filepath = $GLOBALS["path"]."gadgets/".$gadget.".php";
	if (!file_exists($filepath)) {
		jaws_error ("Gadget file doesn't exists",__file__,__line__);
	}
	require_once $filepath;
	$go_gadget = new $_REQUEST["gadget"]();
	$go_gadget->action = $_REQUEST["action"];
	$standalone_mode = $go_gadget->is_standalone($go_gadget->action);
	if ($standalone_mode)
	{
		print $go_gadget->execute();
	} else {
		if ($go_gadget->is_normal($go_gadget->action))
		{
			$GLOBALS["app"]->display_gadgets(); //jfgarcia 28/jul/2004 18:55
			$GLOBALS["app"]->show();
		} else {
			jaws_error ("Invalid operation: You can't execute requested action",__file__,__line__);
		}
	}
} else {
	$GLOBALS["app"]->display_gadgets(); //jfgarcia 28/jul/2004 18:55
	$GLOBALS["app"]->show();
}
 
 
?>
 

In application.php we implement the new function display_gadgets().
Here, we must note that the requested gadget is named requested in the layout.

	function display_gadgets()
	{
 
		if ($_REQUEST["gadget"])
		{
			$requested_gadget = $_REQUEST["gadget"];
			$am_i_index = false;
		} else {
			$requested_gadget = $GLOBALS["app"]->registry->get_value("/config/main_gadget");
			$am_i_index = true;
		}
 
		$sql = "select layout.id, gadget, action, display_when, layout_section.name as section, layout_section.width
			from layout, layout_section
			where layout.layout_section_id = layout_section.id
			order by layout_section.name, layout.position";
                $rs = $this->db->query($sql);
		while ($row = $rs->next()) {
			if (
			($row["display_when"] == "*")||
			(!$am_i_index && in_array($requested_gadget, split(",",$row["display_when"])))||
			($am_i_index && in_array('index', split(",",$row["display_when"])))||
			($am_i_index && ($row["display_when"] == 'index'))
			)
			{
				$this->display_gadget($row["gadget"],$row["action"],$row["section"]);
			}
			if ("requested"==$row["gadget"]){
				$this->display_gadget($requested_gadget,$_REQUEST["action"],"main");
			}
		}
	}

Now, it’s the turn of controlpanel.php. Here the function layout() was modified to handle the new possibilities for the layout.

Caution: Large section follows.

	function layout()
	{
		$tpl = new template($GLOBALS["path"]."templates/controlpanel/");
                $tpl->load("admin.layout.html");
                $tpl->set_block("layout");
                $tpl->set_variable("title","<a href=\"admin.php\">".string_translate("control panel")."</a> &gt; ".string_translate("layout"));
                $tpl->set_variable("icon",check_image("images/controlpanel/layout.png"));
		// layoutmode
		$tpl->set_variable("previous_mode",$GLOBALS["app"]->registry->get_value("/config/layoutmode"));
		$tpl->set_variable("checked".$GLOBALS["app"]->registry->get_value("/config/layoutmode"),'checked="checked"');
		$mode = array();
		$mode[1] = "(1,2,3,4,5)";
		$mode[2] = "(1,2,3,5)";
		$mode[3] = "(1,3,4,5)";
		$mode[4] = "(1,3,5)";
		$sql1 = "select id, name, width from layout_section where id in ".$mode[$GLOBALS["app"]->registry->get_value("/config/layoutmode")]." order by id";
		$rs1 = $GLOBALS["app"]->db->query($sql1);
                while ($row1 = $rs1->fetch_row()) {
                        $section_empty = true;
			$section = $row1["name"];
                        $tpl->set_block("layout/".$row1["name"]);
                        $previous_path="";
			$sql2 = "select id, gadget, action, display_when from layout where layout_section_id = ".$row1["id"]." order by position";
			$rs2 = $GLOBALS["app"]->db->query($sql2);
			$add_url = "admin.php?gadget=controlpanel&amp;action=edit_layout_element&amp;mode=new&amp;section=".$row1["id"];
			$tpl->set_variable ("title",string_translate($row1["name"]));
			$tpl->set_variable ("add",'<a href="'.$add_url.'"><img border="0" src="'.check_image("images/stock/new.png").'" alt="" /> '.string_translate("add gadget").'</a>');
			while ($row2 = $rs2->fetch_row($rs2)){
                                if ($GLOBALS["app"]->registry->get_value("/gadgets/".$row2["gadget"]."/enabled") == 'true') {
                                        $section_empty = false;
					$controls = '';
                                        $tpl->set_block("layout/".$row1["name"]."/item");
					$edit_url = "admin.php?gadget=controlpanel&amp;action=edit_layout_element&amp;id=".$row2["id"];
					$delete_url = "javascript: if(confirm('".string_translate("Do you want to remove this gadget?")."')) { window.location='admin.php?gadget=controlpanel&amp;action=delete_layout_element&amp;id=".$row2["id"]."'}";
                                        $move_url = "admin.php?gadget=controlpanel&amp;action=move_layout_element&amp;id=".$row2["id"]."&amp;section=".$row1["id"]."&amp;direction=";
					$moveup_url = $move_url."up";
					$movedown_url = $move_url."down";
 
					// TODO: Make move left/right work, also up/down to other sections
					$moveleft_url = $move_url."left";
					$moveright_url = $move_url."right";
 
					//if (($row1["name"] == "bar2") || ($row1["name"] == "main"))
					//	$controls .= '<a href="'.$moveleft_url.'"><img border="0" src="'.check_image("images/stock/left.png").'"></a> ';
					$controls .= '<a href="'.$moveup_url.'"><img border="0" src="'.check_image("images/stock/up.png").'" alt="" /></a> ';
					$controls .= '<a href="'.$movedown_url.'"><img border="0" src="'.check_image("images/stock/down.png").'" alt="" /></a> ';
					//if (($row1["name"] == "bar1") || ($row1["name"] == "main"))
					//	$controls .= '<a href="'.$moveright_url.'"><img border="0" src="'.check_image("images/stock/right.png").'"></a> ';
					$controls .= '<a href="'.$edit_url.'"><img border="0" src="'.check_image("images/stock/edit.png").'" alt="" /></a> ';
					$controls .= '<a href="'.$delete_url.'"><img border="0" src="'.check_image("images/stock/delete.png").'" alt="" /></a> ';
                                        $tpl->set_variable("controls",$controls);
					require_once ("gadgets/".$row2["gadget"].".php");
					$g = new $row2["gadget"]();
                                        $tpl->set_variable("title",string_translate($row2["gadget"])." &gt; ".string_translate($row2["action"]));
                                        $tpl->set_variable("description",string_translate($g->valid_action[$row2["action"]]->description));
					unset($g);
					// display when:
					$dw = string_translate ("display in").": <i>";
					if ($row2["display_when"] == "*") {
						$dw .= string_translate("always");
					} else {
						$dw .= $row2["display_when"];
					}
					$dw .= "</i>";
					$tpl->set_variable("display_when",$dw);
					$tpl->parse_block("layout/".$row1["name"]."/item");
				} else {
					if ("requested"==$row2["gadget"]){ // jfgarcia 28/jul/2004 18:21
						$section_empty = false;
						$controls = '';
						$tpl->set_block("layout/".$row1["name"]."/item");
						$move_url = "admin.php?gadget=controlpanel&amp;action=move_layout_element&amp;id=".$row2["id"]."&amp;section=".$row1["id"]."&amp;direction=";
						$moveup_url = $move_url."up";
						$movedown_url = $move_url."down";
						$controls .= '<a href="'.$moveup_url.'"><img border="0" src="'.check_image("images/stock/up.png").'" alt="" /></a> ';
						$controls .= '<a href="'.$movedown_url.'"><img border="0" src="'.check_image("images/stock/down.png").'" alt="" /></a> ';
						$tpl->set_variable("controls",$controls);
						$tpl->set_variable("title",string_translate("requested gadget"));
						$tpl->set_variable("description",string_translate("here goes the requested gadget"));
						$tpl->set_variable("display_when",string_translate("display when: requested"));
						$tpl->parse_block("layout/".$row1["name"]."/item");
					}
				}
			}
			if ($section_empty) {
				$tpl->set_block("layout/".$row1["name"]."/message");
				$tpl->set_variable("message",string_translate("section is empty"));
				$tpl->parse_block("layout/".$row1["name"]."/message");
			}
			$tpl->parse_block("layout/".$row1["name"]);
		}
		$tpl->parse_block("layout");
		return $tpl->get();
 
	}

And finally (I think), we must insert a record in the layout table

INSERT INTO layout (layout_section_id,gadget,action,display_when,position) VALUES (3,"requested","display","requested",1)

Easy, ¿no?.

¿The result?

Look at this screenshots: *Control Panel *In action

Thanks, thanks. Stay sit down, please.

¿What? ¿The size of this post?, well this is the only way I found to send this code to the main developers, so…

NOTE from Raúl Murciano: I think that main developers will sooner read your great code if you send it to the developers mailing list ;-) . They can’t continously review the wiki looking for new contributions.


Oh, thanks a lot Raúl. I’d try several times to suscribe to that mailing list, but no success. The “Welcome” mail never arrived, only the “confirmation” one. So today I try with another email address and everything is all right now.

I’ll try to contact the developers by that media. Thanks for read and for your comments.

*jfgarcia

 
  /var/www/wiki/htdocs/data/jaws/main_gadget_relocator.txt · Last modified: 2007/11/02 16:27