- Remove previously added hook & base class, turns out there's an established way to do it...

- Use existing link_app type hook to create calendar from infolog
This commit is contained in:
Nathan Gray 2011-12-12 21:51:12 +00:00
parent e75a93713a
commit 2f4d8848c5
5 changed files with 56 additions and 196 deletions

View File

@ -661,62 +661,6 @@ class calendar_hooks
public static function config_validate() {
$GLOBALS['egw_info']['server']['found_validation_hook'] = True;
}
/**
* Convert from a different app entry to this application
*
* @see phpgwapi/inc/class.transmogrify.inc.php
*/
public static function convert($data) {
$mapping['infolog'] = array(
'info_owner' => 'owner',
'info_cat' => 'category',
'info_priority' => 'priority',
'info_access' => 'public',
'info_subject' => 'title',
'info_des' => 'description',
'info_location' => 'location',
'info_startdate' => 'start',
'info_datecompleted' => 'end',
);
// Check for support
if(!$data['from']) return array_keys($mapping); // Get list of supported apps
if(!in_array($data['from'], array_keys($mapping))) return false;
transmogrify::check($data['to'], $data['from'], $data['data'], $mapping[$data['from']]);
foreach($mapping[$data['from']] as $from => $to)
{
$result[$to] = $data['data'][$from];
}
// Add user to participants, or it won't show up
$result['participants'][calendar_so::combine_user('u',$result['owner'])] = calendar_so::combine_status(NO_RESPONSE);
switch($data['from'])
{
case 'infolog':
$result['public'] = $result['public'] != 'private';
// Add responsible as participant
foreach($data['data']['info_responsible'] as $responsible) {
$result['participants'][calendar_so::combine_user('u',$responsible)] = calendar_so::combine_status('U');
}
// Add linked contact as participant
if($data['data']['info_link']['app'] == 'addressbook')
{
$result['participants'][calendar_so::combine_user('c',$data['data']['info_link']['id'])] = calendar_so::combine_status('U');
}
break;
}
if($result)
{
$bo = new calendar_boupdate();
$id = $bo->save($result);
}
return $id ? $id : $result;
}
}
// Not part of the class, since config hooks are still using the old style

View File

@ -1199,10 +1199,60 @@ class calendar_uiforms extends calendar_ui
$event['whole_day'] = !$start['hour'] && !$start['minute'] && $end['hour'] == 23 && $end['minute'] == 59;
$link_to_id = $event['id'];
if (!$add_link && !$event['id'] && isset($_GET['link_app']) && isset($_GET['link_id']) &&
preg_match('/^[a-z_0-9-]+:[:a-z_0-9-]+$/i',$_GET['link_app'].':'.$_GET['link_id'])) // gard against XSS
if (!$add_link && !$event['id'] && isset($_REQUEST['link_app']) && isset($_REQUEST['link_id']))
{
egw_link::link('calendar',$link_to_id,$_GET['link_app'],$_GET['link_id']);
$link_ids = is_array($_REQUEST['link_id']) ? $_REQUEST['link_id'] : array($_REQUEST['link_id']);
foreach(is_array($_REQUEST['link_app']) ? $_REQUEST['link_app'] : array($_REQUEST['link_app']) as $n => $link_app)
{
$link_id = $link_ids[$n];
$app_entry = array();
if(!preg_match('/^[a-z_0-9-]+:[:a-z_0-9-]+$/i',$link_app.':'.$link_id)) // guard against XSS
{
continue;
}
switch($link_app)
{
case 'infolog':
static $infolog_bo;
if(!$infolog_bo) $infolog_bo = new infolog_bo();
$infolog = $app_entry = $infolog_bo->read($link_id);
$event = array_merge($event, array(
'owner' => $infolog['info_owner'],
'category' => $GLOBALS['egw']->categories->check_list(EGW_ACL_READ, $infolog['info_cat']),
'priority' => $infolog['info_priority'] + 1,
'public' => $infolog['info_access'] != 'private',
'title' => $infolog['info_subject'],
'description' => $infolog['info_des'],
'location' => $infolog['info_location'],
'start' => $infolog['info_startdate'],
'end' => $infolog['info_enddate']
));
// Add responsible as participant
foreach($infolog['info_responsible'] as $responsible) {
$event['participants'][calendar_so::combine_user('u',$responsible)] = calendar_so::combine_status('U');
$event['participant_types']['u'][$responsible] = 'U';
}
// Add linked contact as participant
if($infolog['info_link']['app'] == 'addressbook')
{
$event['participants'][calendar_so::combine_user('c',$infolog['info_link']['id'])] = calendar_so::combine_status('U');
$event['participant_types']['c'][$responsible] = 'U';
}
break;
default:
$event['title'] = egw_link::title($link_app,$link_id);
}
// Copy same custom fields
$cal_cfs = config::get_customfields('calendar');
$link_app_cfs = config::get_customfields($link_app);
foreach($cal_cfs as $name => $settings)
{
if($link_app_cfs[$name]) $event['#'.$name] = $app_entry['#'.$name];
}
egw_link::link('calendar',$link_to_id,$link_app,$link_id);
}
}
}
$etpl = new etemplate();

View File

@ -44,7 +44,6 @@ $setup_info['calendar']['hooks']['search_link'] = 'calendar_hooks::search_link';
$setup_info['calendar']['hooks']['config_validate'] = 'calendar_hooks::config_validate';
$setup_info['calendar']['hooks']['timesheet_set'] = 'calendar.calendar_bo.timesheet_set';
$setup_info['calendar']['hooks']['export_limit'] = 'calendar_hooks::getAppExportLimit';
$setup_info['calendar']['hooks']['convert'] = 'calendar_hooks::convert';
/* Dependencies for this app to work */
$setup_info['calendar']['depends'][] = array(

View File

@ -951,7 +951,10 @@ class infolog_ui
'icon' => 'calendar/navbar',
'caption' => 'Calendar',
'group' => $group,
'url' => 'menuaction=calendar.calendar_uiforms.edit&'.
egw_link::get_registry('calendar', 'add_app') . '[]=infolog&'.egw_link::get_registry('calendar','add_id').'[]=$id',
'allowOnMultiple' => false,
'popup' => egw_link::get_registry('calendar', 'add_popup'),
);
}
if ($GLOBALS['egw_info']['user']['apps']['timesheet'])
@ -1103,18 +1106,6 @@ class infolog_ui
}
switch($action)
{
case 'calendar':
$action_msg = lang('copied to %1', lang('calendar'));
$cal_id = transmogrify::convert($entry, 'calendar', 'infolog', $id);
if($cal_id)
{
$success++;
}
else
{
$failed++;
}
break;
case 'close':
$action_msg = lang('closed');
$this->close($id, '', false, $skip_notifications);

View File

@ -1,124 +0,0 @@
<?php
/**
* Base clase for changing or copying one record type (eg Infolog) into another (eg Calendar)
*
* @link http://www.egroupware.org
* @author Nathan Gray
* @package phpgwapi
* @copyright (c) 2011 Nathan Gray
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
/**
* Transmogrify: to completely alter the form of
* For changing an entry from one application (in array form) into another.
*
* Uses hooks so other apps can specify how the conversion process is to happen from other applications,
* so this class is mostly support functions. In the future, it may support customization of the
* field mapping between applications but right now mappings between different fields are provided by the destination
* application.
*
* Hook parameters:
* 'location' => 'convert', // Required by hook system
* 'from' => Original application. If omitted, hook function should return a list of supported applications.
* 'data' => Data from original application. If omitted, return false.
* The hook should return the ID for the newly created entry.
*
* For ease of use & reduction of duplication, hook functions can call transmogrify::check() as a 'parent' function.
*/
class transmogrify
{
/**
* Checks to see if the conversion can happen
*
* @param from String appname
* @param to String appname
*
* @return boolean
*/
public static function isSupported($from, $to)
{
$args = array(
'location' => 'convert'
);
return $GLOBALS['egw']->hooks->hook_exists('convert', $to) > 0 && in_array($from, $GLOBALS['egw']->hooks->single('convert',$to));
}
/**
* Get a list of applications that the given application can convert to
*
* @param from String appname
*
* @return array of appnames
*/
public static function getList($from)
{
return $GLOBALS['egw']->hooks->single('convert', $from);
}
/**
* Convert data from the specified record into another, as best as possible.
*
* @param data Array to be changed
* @param to String appname
* @param from String appname Defaults to current app.
* @param from_id Entry ID. If provided, a link will be created between entries.
*
* @return Array of data as 'to' app structures it, ready to be saved
*/
public static function convert(Array $data, $to, $from = null, $from_id = null)
{
if($from == null) $from = $GLOBALS['egw_info']['flags']['currentapp'];
if(!self::isSupported($from, $to))
{
throw new egw_exception_wrong_parameter("$to does not know how to convert from $from");
}
$result = array();
// Check for hook
// TODO: Maybe add in preference for conversion
if($GLOBALS['egw']->hooks->hook_exists('convert',$to))
{
$args = array(
'location' => 'convert',
'from' => $from,
'to' => $to,
'data' => $data
);
$id = $GLOBALS['egw']->hooks->single($args, $to);
}
else
{
// TODO: Automagic conversion based on data types or names
throw new egw_exception_wrong_parameter("$to does not know how to convert from $from");
}
// Try to link
if($id && $from_id)
{
egw_link::link($from, $from_id, $to, $id);
}
// Try to edit
egw_framework::set_onload("if(egw.open) { egw.open('$id', '$to', 'edit');}");
return $id;
}
/**
* 'Parent' function for hook functions
* Put here to reduce duplication
*/
public static function check($to, $from, Array &$data, Array &$mapping)
{
if(!$from) return; // Get list of supported apps
if(!$data) return false;
// If we have some common stuff, it can go here
}
}
?>