2006-11-05 10:59:38 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPGWAPI Dragdrop - generates javascript for Walter Zorns dragdrop class
|
|
|
|
*
|
|
|
|
* @link www.egroupware.org
|
|
|
|
* @author Christian Binder <christian.binder@freakmail.de>
|
|
|
|
* @copyright (c) 2006 by Christian Binder <christian.binder@freakmail.de>
|
|
|
|
* @package phpgwapi
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @version $Id: class.dragdrop.inc.php 22783 2006-11-02 13:52:24Z jaytraxx $
|
|
|
|
*/
|
|
|
|
|
2006-12-15 20:39:36 +01:00
|
|
|
require_once(EGW_INCLUDE_ROOT. '/phpgwapi/inc/class.browser.inc.php');
|
|
|
|
|
2006-11-05 10:59:38 +01:00
|
|
|
/**
|
|
|
|
* General object containing the draggables and droppables
|
|
|
|
*
|
|
|
|
* @package phpgwapi
|
|
|
|
* @author Christian Binder <christian.binder@freakmail.de>
|
|
|
|
* @copyright (c) 2006 by Christian Binder <christian.binder@freakmail.de>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
*/
|
|
|
|
class dragdrop
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* draggable Objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $draggables;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* droppable Objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $droppables;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ensures that function setJSCode is only run once
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $setCodeDone = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JavaScript(s) to include which contains the actions while dragging or dropping
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $actionScripts;
|
2006-12-15 20:39:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* enables class for all browsers - use this for testing still not validated browsers
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $browserTestMode = false;
|
2006-11-05 10:59:38 +01:00
|
|
|
|
|
|
|
function dragdrop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adds a Draggable javascript object
|
|
|
|
*
|
|
|
|
* @param string $name unique html id of the object
|
2006-12-15 20:39:36 +01:00
|
|
|
* @param array $values=false optional associative array with values of the object
|
2006-11-05 10:59:38 +01:00
|
|
|
* @param string $dragAction=false ActionScript executed while item is dragged e.g. calendar.myscript.mydrag
|
|
|
|
* @param string $dropAction=false ActionScript executed when item is dropped e.g. calendar.myscript.mydrop
|
|
|
|
* @return boolean true if all actions succeded, false otherwise
|
|
|
|
*/
|
2006-12-15 20:39:36 +01:00
|
|
|
function addDraggable($name,$values = false,$dragAction = false,$dropAction = false)
|
2006-11-05 10:59:38 +01:00
|
|
|
{
|
|
|
|
if(!$this->checkUnique($name)) { return false; }
|
2006-12-15 20:39:36 +01:00
|
|
|
$this->draggables[] = array('name'=>$name,'values'=>$values,'dragAction'=>$this->registerActionScript($dragAction),'dropAction'=>$this->registerActionScript($dropAction));
|
2006-11-05 10:59:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adds a Droppable javascript object
|
|
|
|
*
|
|
|
|
* @param string $name unique html id of the object
|
2006-12-15 20:39:36 +01:00
|
|
|
* @param array $values=false optional associative array with values of the object
|
2006-11-05 10:59:38 +01:00
|
|
|
* @return boolean true if all actions succeded, false otherwise
|
|
|
|
*/
|
2006-12-15 20:39:36 +01:00
|
|
|
function addDroppable($name,$values = false)
|
2006-11-05 10:59:38 +01:00
|
|
|
{
|
|
|
|
if(!$this->checkUnique($name)) { return false; }
|
2006-12-15 20:39:36 +01:00
|
|
|
$this->droppables[] = array('name'=>$name,'values'=>$values);
|
2006-11-05 10:59:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* generates the appropriate JSCode for all defined objects
|
|
|
|
*
|
|
|
|
* @return boolean true if all actions succeed or false if the function was called more than once
|
|
|
|
*/
|
|
|
|
function setJSCode()
|
|
|
|
{
|
2006-12-15 20:39:36 +01:00
|
|
|
// check that dragdrop is enabled by prefs and that we have a supported browser
|
|
|
|
if( !$GLOBALS['egw_info']['user']['preferences']['common']['enable_dragdrop'] ||
|
|
|
|
!$this->validateBrowser()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-11-05 10:59:38 +01:00
|
|
|
// this function can only be run once, so we check that at the beginning
|
2006-12-15 20:39:36 +01:00
|
|
|
if($this->setCodeDone)
|
|
|
|
{
|
|
|
|
error_log('phpgwapi.dragdrop::setJSCode called more than once - aborting');
|
|
|
|
return false;
|
|
|
|
}
|
2006-11-05 10:59:38 +01:00
|
|
|
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= "<!-- BEGIN JavaScript for wz_dragdrop.js -->\n";
|
|
|
|
|
|
|
|
// include wz_dragdrop once
|
|
|
|
if(!$GLOBALS['egw_info']['flags']['wz_dragdrop_included'])
|
|
|
|
{
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= '<script language="JavaScript" type="text/javascript" src="'.$GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/js/wz_dragdrop/wz_dragdrop.js"></script>'."\n";
|
|
|
|
$GLOBALS['egw_info']['flags']['wz_dragdrop_included'] = True;
|
|
|
|
}
|
|
|
|
|
|
|
|
// include actionScripts
|
|
|
|
if(is_array($this->actionScripts))
|
|
|
|
{
|
|
|
|
foreach($this->actionScripts as $i => $actionScript)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= "<script language='JavaScript' type='text/javascript' src='".$actionScript['file']."'></script>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// register all dragdrop elements to wz_dragdrop
|
|
|
|
if(is_array($this->draggables))
|
|
|
|
{
|
|
|
|
foreach($this->draggables as $i=>$element)
|
|
|
|
{
|
|
|
|
$element_names_array[] = "\"".$element['name']."\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_array($this->droppables))
|
|
|
|
{
|
|
|
|
foreach($this->droppables as $i=>$element)
|
|
|
|
{
|
|
|
|
$element_names_array[] = "\"".$element['name']."\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_array($element_names_array))
|
|
|
|
{
|
|
|
|
$element_names=implode(",",$element_names_array);
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= '<script language="JavaScript" type="text/javascript">SET_DHTML(SCROLL,TRANSPARENT,CURSOR_HAND,'.$element_names.');</script>'."\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// set special params for draggable elements
|
|
|
|
if(is_array($this->draggables))
|
|
|
|
{
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= '<script language="JavaScript" type="text/javascript">'."\n";
|
|
|
|
foreach($this->draggables as $i=>$element)
|
|
|
|
{
|
2006-12-15 20:39:36 +01:00
|
|
|
if(is_array($element['values']))
|
|
|
|
{
|
|
|
|
foreach($element['values'] as $val_name=>$val_value)
|
|
|
|
{
|
|
|
|
if($val_value)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= 'dd.elements.'.$element['name'].'.my_'.$val_name.' = "'.$val_value.'";'."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-05 10:59:38 +01:00
|
|
|
if($element['dragAction']) { $GLOBALS['egw_info']['flags']['need_footer'] .= 'dd.elements.'.$element['name'].'.setDragFunc('.$element['dragAction'].');'."\n"; }
|
|
|
|
if($element['dropAction']) { $GLOBALS['egw_info']['flags']['need_footer'] .= 'dd.elements.'.$element['name'].'.setDropFunc('.$element['dropAction'].');'."\n"; }
|
|
|
|
}
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= '</script>'."\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// set special params for droppable elements
|
|
|
|
if(is_array($this->droppables))
|
|
|
|
{
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= '<script language="JavaScript" type="text/javascript">'."\n";
|
|
|
|
foreach($this->droppables as $i=>$element)
|
|
|
|
{
|
2006-12-15 20:39:36 +01:00
|
|
|
if(is_array($element['values']))
|
|
|
|
{
|
|
|
|
foreach($element['values'] as $val_name=>$val_value)
|
|
|
|
{
|
|
|
|
if($val_value)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= 'dd.elements.'.$element['name'].'.my_'.$val_name.' = "'.$val_value.'";'."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-05 10:59:38 +01:00
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= 'dd.elements.'.$element['name'].'.setDraggable(false);'."\n";
|
|
|
|
}
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= '</script>'."\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$GLOBALS['egw_info']['flags']['need_footer'] .= "<!-- END JavaScript for wz_dragdrop.js -->\n";
|
|
|
|
return $this->setCodeDone = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if the given name of an object is unique in all draggable AND droppable objects
|
|
|
|
*
|
|
|
|
* @param string $name unique html id of the object
|
|
|
|
* @return boolean true if $name is unique, otherwise false
|
|
|
|
*/
|
|
|
|
function checkUnique($name)
|
|
|
|
{
|
|
|
|
if(is_array($this->draggables))
|
|
|
|
{
|
|
|
|
foreach($this->draggables as $i=>$element)
|
|
|
|
{
|
|
|
|
if($element['name'] == $name)
|
|
|
|
{
|
|
|
|
error_log("class.dragdrop.inc.php::addDraggable duplicate name for object '".$name."'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_array($this->droppables))
|
|
|
|
{
|
|
|
|
foreach($this->droppables as $i=>$element)
|
|
|
|
{
|
|
|
|
if($element['name'] == $name)
|
|
|
|
{
|
|
|
|
error_log("class.dragdrop.inc.php::addDraggable duplicate name for object '".$name."'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-12-15 20:39:36 +01:00
|
|
|
* checks if the browser is validated to work with the dragdrop class
|
|
|
|
* used to enable/disable this class for the clients browser
|
|
|
|
*
|
|
|
|
* @return boolean true if browser is validated, otherwise false
|
|
|
|
*/
|
|
|
|
function validateBrowser()
|
|
|
|
{
|
|
|
|
$clientBrowser = new browser();
|
|
|
|
|
|
|
|
if($this->browserTestMode)
|
|
|
|
{
|
|
|
|
error_log('dragdrop::validateBrowser, agent: ' . $clientBrowser->get_agent());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(array('MOZILLA') as $id=>$validatedBrowser)
|
|
|
|
{
|
|
|
|
if($this->browserTestMode || $clientBrowser->get_agent() == $validatedBrowser)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-11-05 10:59:38 +01:00
|
|
|
* registers additional javascript file(s) which contain scripts for various object actions
|
|
|
|
* and handles duplicates
|
|
|
|
*
|
|
|
|
* @param string $script script to register, e.g. 'calendar.dragdrop.moveEvent'
|
|
|
|
* @return string $functionname if JavaScript file exists, otherwise false
|
|
|
|
*/
|
|
|
|
function registerActionScript($script)
|
|
|
|
{
|
|
|
|
list($appname,$scriptname,$functionname) = explode('.',$script);
|
|
|
|
$script = $appname.".".$scriptname;
|
|
|
|
$serverFile = EGW_INCLUDE_ROOT.'/'.$appname.'/js/'.$scriptname.'.js';
|
|
|
|
$browserFile = $GLOBALS['egw_info']['server']['webserver_url'].'/'.$appname.'/js/'.$scriptname.'.js';
|
|
|
|
|
|
|
|
// check if file exists otherwise exit
|
|
|
|
if(!file_exists($serverFile))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// check duplicates
|
|
|
|
if(is_array($this->actionScripts))
|
|
|
|
{
|
|
|
|
foreach($this->actionScripts as $i=>$actionScript)
|
|
|
|
{
|
|
|
|
if($actionScript['script'] == $script) { return $functionname; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->actionScripts[] = array('script' => $script,'file' => $browserFile);
|
|
|
|
return $functionname;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|