2006-10-04 22:23:44 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2011-08-25 21:52:51 +02:00
|
|
|
* EGroupware eTemplate Extension - Contact Widget
|
2006-10-04 22:23:44 +02:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage extensions
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* eTemplate Extension: Contact widget
|
|
|
|
*
|
2011-08-25 21:52:51 +02:00
|
|
|
* This widget can be used to fetch fields of a contact specified by contact-id
|
2006-10-04 22:23:44 +02:00
|
|
|
*/
|
2014-09-30 23:37:45 +02:00
|
|
|
class contact_widget extends etemplate_widget_entry
|
2006-10-04 22:23:44 +02:00
|
|
|
{
|
2011-08-25 21:52:51 +02:00
|
|
|
/**
|
2006-10-04 22:23:44 +02:00
|
|
|
* exported methods of this class
|
2011-08-25 21:52:51 +02:00
|
|
|
*
|
2006-10-04 22:23:44 +02:00
|
|
|
* @var array $public_functions
|
2011-08-25 21:52:51 +02:00
|
|
|
* @deprecated only used for old etemplate
|
2006-10-04 22:23:44 +02:00
|
|
|
*/
|
2011-08-25 21:52:51 +02:00
|
|
|
public $public_functions = array(
|
2006-10-04 22:23:44 +02:00
|
|
|
'pre_process' => True,
|
|
|
|
);
|
|
|
|
/**
|
|
|
|
* availible extensions and there names for the editor
|
|
|
|
*
|
2011-08-25 21:52:51 +02:00
|
|
|
* @var string|array $human_name
|
|
|
|
* @deprecated only used for old etemplate
|
2006-10-04 22:23:44 +02:00
|
|
|
*/
|
2011-08-25 21:52:51 +02:00
|
|
|
public $human_name = array(
|
2007-09-13 15:50:48 +02:00
|
|
|
'contact-value' => 'Contact',
|
|
|
|
'contact-account' => 'Account contactdata',
|
|
|
|
'contact-template' => 'Account template',
|
|
|
|
'contact-fields' => 'Contact fields',
|
2006-10-04 22:23:44 +02:00
|
|
|
);
|
|
|
|
/**
|
|
|
|
* Instance of the contacts class
|
2011-08-25 21:52:51 +02:00
|
|
|
*
|
2006-10-04 22:23:44 +02:00
|
|
|
* @var contacts
|
|
|
|
*/
|
2011-08-25 21:52:51 +02:00
|
|
|
private $contacts;
|
|
|
|
|
2006-10-04 22:23:44 +02:00
|
|
|
/**
|
2011-08-25 21:52:51 +02:00
|
|
|
* Array with a transformation description, based on attributes to modify.
|
|
|
|
*
|
|
|
|
* Exampels:
|
|
|
|
*
|
|
|
|
* * 'type' => array('some' => 'other')
|
|
|
|
* if 'type' attribute equals 'some' replace it with 'other'
|
|
|
|
*
|
|
|
|
* * 'type' => array('some' => array('type' => 'other', 'options' => 'otheroption')
|
|
|
|
* same as above, but additonally set 'options' attr to 'otheroption'
|
|
|
|
*
|
|
|
|
* --> leaf element is the action, if previous filters are matched:
|
|
|
|
* - if leaf is scalar, it just replaces the previous filter value
|
|
|
|
* - if leaf is an array, it contains assignments for (multiple) attributes: attr => value pairs
|
|
|
|
*
|
|
|
|
* * 'type' => array(
|
|
|
|
* 'some' => array(...),
|
|
|
|
* 'other' => array(...),
|
|
|
|
* '__default__' => array(...),
|
|
|
|
* )
|
|
|
|
* it's possible to have a list of filters with actions to run, plus a '__default__' which matches all not explicitly named values
|
|
|
|
*
|
|
|
|
* * 'value' => array('__callback__' => 'app.class.method' || 'class::method' || 'method')
|
|
|
|
* run value through a *serverside* callback, eg. reading an entry based on it's given id
|
|
|
|
*
|
|
|
|
* * 'value' => array('__js__' => 'function(value) { return value+5; }')
|
|
|
|
* run value through a *clientside* callback running in the context of the widget
|
|
|
|
*
|
|
|
|
* * 'name' => '@name[@options]'
|
|
|
|
* replace value of 'name' attribute with itself (@name) plus value of options in square brackets
|
|
|
|
*
|
|
|
|
* --> attribute name prefixed with @ sign means value of given attribute
|
2006-10-04 22:23:44 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-25 21:52:51 +02:00
|
|
|
protected static $transformation = array(
|
|
|
|
'type' => array(
|
|
|
|
'contact-fields' => array( // contact-fields widget
|
|
|
|
'sel_options' => array('__callback__' => 'get_contact_fields'),
|
|
|
|
'type' => 'select',
|
|
|
|
'no_lang' => true,
|
|
|
|
'options' => 'None',
|
|
|
|
),
|
|
|
|
'contact-template' => array(
|
|
|
|
'type' => 'template',
|
|
|
|
'options' => '',
|
2014-09-30 23:37:45 +02:00
|
|
|
'template' => array('__callback__' => 'parse_template'),
|
2011-08-25 21:52:51 +02:00
|
|
|
),
|
|
|
|
'__default__' => array(
|
|
|
|
'options' => array(
|
|
|
|
'bday' => array('type' => 'date', 'options' => 'Y-m-d'),
|
|
|
|
'owner' => array('type' => 'select-account', 'options' => ''),
|
|
|
|
'modifier' => array('type' => 'select-account', 'options' => ''),
|
|
|
|
'creator' => array('type' => 'select-account', 'options' => ''),
|
|
|
|
'modifed' => array('type' => 'date-time', 'options' => ''),
|
|
|
|
'created' => array('type' => 'date-time', 'options' => ''),
|
|
|
|
'cat_id' => array('type' => 'select-cat', 'options' => ''),
|
|
|
|
'__default__' => array('type' => 'label', 'options' => ''),
|
|
|
|
),
|
|
|
|
'no_lang' => 1,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2006-10-04 22:23:44 +02:00
|
|
|
/**
|
|
|
|
* Constructor of the extension
|
|
|
|
*
|
2011-08-27 11:03:32 +02:00
|
|
|
* @param string $xml or 'html' for old etemplate
|
2006-10-04 22:23:44 +02:00
|
|
|
*/
|
2013-11-01 22:19:08 +01:00
|
|
|
public function __construct($xml)
|
2011-08-25 21:52:51 +02:00
|
|
|
{
|
2011-08-27 11:03:32 +02:00
|
|
|
if (is_a($xml, 'XMLReader') || $xml != '' && $xml != 'html')
|
2011-08-25 21:52:51 +02:00
|
|
|
{
|
|
|
|
parent::__construct($xml);
|
|
|
|
}
|
|
|
|
$this->contacts = $GLOBALS['egw']->contacts;
|
|
|
|
}
|
|
|
|
|
2014-09-30 23:37:45 +02:00
|
|
|
/**
|
|
|
|
* Legacy support for putting the template name in 'label' param
|
|
|
|
* @param string $label
|
|
|
|
* @param array $attrs
|
|
|
|
*/
|
|
|
|
public function parse_template($template, &$attrs)
|
|
|
|
{
|
|
|
|
return sprintf($template ? $template : $attrs['label'], $attrs['value']);
|
|
|
|
}
|
|
|
|
|
2011-08-25 21:52:51 +02:00
|
|
|
/**
|
|
|
|
* Get all contact-fields
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_contact_fields()
|
|
|
|
{
|
|
|
|
translation::add_app('addressbook');
|
|
|
|
$this->contacts->__construct();
|
|
|
|
$options = $this->contacts->contact_fields;
|
|
|
|
foreach($this->contacts->customfields as $name => $data)
|
|
|
|
{
|
|
|
|
$options['#'.$name] = $data['label'];
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2014-09-30 23:37:45 +02:00
|
|
|
public function get_entry($value, array $attrs)
|
|
|
|
{
|
|
|
|
return $this->get_contact($value, $attrs);
|
|
|
|
}
|
2011-08-25 21:52:51 +02:00
|
|
|
/**
|
|
|
|
* Get contact data, if $value not already contains them
|
|
|
|
*
|
|
|
|
* @param int|string|array $value
|
|
|
|
* @param array $attrs
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_contact($value, array $attrs)
|
2006-10-04 22:23:44 +02:00
|
|
|
{
|
2014-09-30 23:37:45 +02:00
|
|
|
$field = $attrs['field'] ? $attrs['field'] : '';
|
|
|
|
if (is_array($value) && !(array_key_exists('app',$value) && array_key_exists('id', $value))) return $value;
|
2011-08-25 21:52:51 +02:00
|
|
|
|
2014-09-30 23:37:45 +02:00
|
|
|
if(is_array($value) && array_key_exists('app', $value) && array_key_exists('id', $value)) $value = $value['id'];
|
2011-08-25 21:52:51 +02:00
|
|
|
switch($attrs['type'])
|
|
|
|
{
|
|
|
|
case 'contact-account':
|
|
|
|
case 'contact-template':
|
|
|
|
if (substr($value,0,8) != 'account:')
|
|
|
|
{
|
|
|
|
$value = 'account:'.($attrs['name'] != 'account:' ? $value : $GLOBALS['egw_info']['user']['account_id']);
|
|
|
|
}
|
|
|
|
// fall-throught
|
|
|
|
case 'contact-value':
|
|
|
|
default:
|
|
|
|
if (substr($value,0,12) == 'addressbook:') $value = substr($value,12); // link-entry syntax
|
|
|
|
if (!($contact = $this->contacts->read($value)))
|
|
|
|
{
|
|
|
|
$contact = array();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
unset($contact['jpegphoto']); // makes no sense to return binary image
|
2008-03-25 17:42:23 +01:00
|
|
|
|
2011-08-25 21:52:51 +02:00
|
|
|
//error_log(__METHOD__."('$value') returning ".array2string($contact));
|
|
|
|
return $contact;
|
2006-10-04 22:23:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pre-processing of the extension
|
|
|
|
*
|
|
|
|
* This function is called before the extension gets rendered
|
|
|
|
*
|
|
|
|
* @param string $name form-name of the control
|
|
|
|
* @param mixed &$value value / existing content, can be modified
|
2011-08-25 21:52:51 +02:00
|
|
|
* @param array &$cell array with the widget, can be modified for ui-independent widgets
|
2006-10-04 22:23:44 +02:00
|
|
|
* @param array &$readonlys names of widgets as key, to be made readonly
|
|
|
|
* @param mixed &$extension_data data the extension can store persisten between pre- and post-process
|
|
|
|
* @param etemplate &$tmpl reference to the template we belong too
|
|
|
|
* @return boolean true if extra label is allowed, false otherwise
|
|
|
|
*/
|
2011-08-25 21:52:51 +02:00
|
|
|
function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl)
|
2006-10-04 22:23:44 +02:00
|
|
|
{
|
2007-09-13 15:50:48 +02:00
|
|
|
//echo "<p>contact_widget::pre_process('$name','$value',".print_r($cell,true).",...)</p>\n";
|
2008-03-05 19:38:46 +01:00
|
|
|
switch($type = $cell['type'])
|
2006-10-04 22:23:44 +02:00
|
|
|
{
|
|
|
|
case 'contact-fields':
|
2011-08-25 21:52:51 +02:00
|
|
|
$cell['sel_options'] = $this->get_contact_fields();
|
2006-10-04 22:23:44 +02:00
|
|
|
$cell['type'] = 'select';
|
|
|
|
$cell['no_lang'] = 1;
|
2007-09-06 13:14:12 +02:00
|
|
|
$cell['size'] = 'None';
|
2006-10-04 22:23:44 +02:00
|
|
|
break;
|
|
|
|
|
2006-10-26 12:57:47 +02:00
|
|
|
case 'contact-account':
|
2007-09-13 15:50:48 +02:00
|
|
|
case 'contact-template':
|
|
|
|
if (substr($value,0,8) != 'account:')
|
|
|
|
{
|
|
|
|
$value = 'account:'.($cell['name'] != 'account:' ? $value : $GLOBALS['egw_info']['user']['account_id']);
|
|
|
|
}
|
2011-08-25 21:52:51 +02:00
|
|
|
echo "<p>$name: $value</p>\n";
|
2006-10-26 12:57:47 +02:00
|
|
|
// fall-throught
|
2006-10-04 22:23:44 +02:00
|
|
|
case 'contact-value':
|
|
|
|
default:
|
2006-10-05 10:05:25 +02:00
|
|
|
if (substr($value,0,12) == 'addressbook:') $value = substr($value,12); // link-entry syntax
|
2011-08-25 21:52:51 +02:00
|
|
|
if (!$value || !$cell['size'] || (!is_array($this->contact) ||
|
2006-10-26 13:09:34 +02:00
|
|
|
!($this->contact['id'] == $value || 'account:'.$this->contact['account_id'] == $value)) &&
|
2006-10-04 22:23:44 +02:00
|
|
|
!($this->contact = $this->contacts->read($value)))
|
|
|
|
{
|
|
|
|
$cell = $tmpl->empty_cell();
|
2006-10-19 20:19:11 +02:00
|
|
|
$value = '';
|
2006-10-04 22:23:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-08-20 09:51:49 +02:00
|
|
|
$type = $cell['size'];
|
2006-10-04 22:23:44 +02:00
|
|
|
$cell['size'] = '';
|
2011-08-25 21:52:51 +02:00
|
|
|
|
2007-09-13 15:50:48 +02:00
|
|
|
if ($cell['type'] == 'contact-template')
|
|
|
|
{
|
|
|
|
$name = $this->contact[$type];
|
|
|
|
$cell['type'] = 'template';
|
|
|
|
if (($prefix = $cell['label'])) $name = strpos($prefix,'%s') !== false ? str_replace('%s',$name,$prefix) : $prefix.$name;
|
|
|
|
$cell['obj'] = new etemplate($name,$tmpl->as_array());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$value = $this->contact[$type];
|
2006-10-04 22:23:44 +02:00
|
|
|
$cell['no_lang'] = 1;
|
|
|
|
$cell['readonly'] = true;
|
2011-08-25 21:52:51 +02:00
|
|
|
|
2007-08-20 09:51:49 +02:00
|
|
|
switch($type)
|
2006-10-04 22:23:44 +02:00
|
|
|
{
|
|
|
|
// ToDo: pseudo types like address-label
|
2011-08-25 21:52:51 +02:00
|
|
|
|
2007-08-20 09:51:49 +02:00
|
|
|
case 'bday':
|
|
|
|
$cell['type'] = 'date';
|
|
|
|
$cell['size'] = 'Y-m-d';
|
|
|
|
break;
|
2011-08-25 21:52:51 +02:00
|
|
|
|
2007-08-20 09:51:49 +02:00
|
|
|
case 'owner':
|
|
|
|
case 'modifier':
|
|
|
|
case 'creator':
|
|
|
|
$cell['type'] = 'select-account';
|
|
|
|
break;
|
2011-08-25 21:52:51 +02:00
|
|
|
|
2007-08-20 09:51:49 +02:00
|
|
|
case 'modified':
|
|
|
|
case 'created':
|
|
|
|
$cell['type'] = 'date-time';
|
|
|
|
break;
|
2011-08-25 21:52:51 +02:00
|
|
|
|
2007-08-20 09:51:49 +02:00
|
|
|
case 'cat_id':
|
|
|
|
$cell['type'] = 'select-cat';
|
|
|
|
break;
|
2006-10-04 22:23:44 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
$cell['type'] = 'label';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-03-05 19:38:46 +01:00
|
|
|
$cell['id'] = ($cell['id'] ? $cell['id'] : $cell['name'])."[$type]";
|
|
|
|
|
2006-10-04 22:23:44 +02:00
|
|
|
return True; // extra label ok
|
2014-05-07 11:00:59 +02:00
|
|
|
}
|
2006-10-04 22:23:44 +02:00
|
|
|
}
|
2011-08-25 21:52:51 +02:00
|
|
|
// register widgets for etemplate2
|
|
|
|
etemplate_widget::registerWidget('contact_widget',array('contact-value', 'contact-account', 'contact-template', 'contact-fields'));
|