2016-02-18 20:45:37 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware - eTemplate serverside of owner list widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright 2016 Nathan Gray
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
2016-05-01 19:47:59 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
use EGroupware\Api\Link;
|
|
|
|
use EGroupware\Api\Framework;
|
|
|
|
use EGroupware\Api\Etemplate;
|
|
|
|
|
2016-02-18 20:45:37 +01:00
|
|
|
/**
|
|
|
|
* eTemplate tag list widget
|
|
|
|
*
|
|
|
|
* The naming convention is <appname>_<subtype>_etemplate_widget
|
|
|
|
*/
|
2016-05-01 19:47:59 +02:00
|
|
|
class calendar_owner_etemplate_widget extends Etemplate\Widget\Taglist
|
2016-02-18 20:45:37 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure all the needed select options are there
|
|
|
|
*
|
|
|
|
* @param string $cname
|
|
|
|
* @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
|
|
|
*/
|
|
|
|
public function beforeSendToClient($cname, array $expand=null)
|
|
|
|
{
|
|
|
|
|
2016-05-01 19:47:59 +02:00
|
|
|
Framework::includeJS('.','et2_widget_owner','calendar');
|
2016-05-24 23:08:48 +02:00
|
|
|
Framework::includeCSS('calendar','calendar');
|
2016-05-01 11:57:48 +02:00
|
|
|
|
2016-02-18 20:45:37 +01:00
|
|
|
$bo = new calendar_bo();
|
|
|
|
|
|
|
|
$form_name = self::form_name($cname, $this->id, $expand);
|
|
|
|
|
|
|
|
$value =& self::get_array(self::$request->content, $form_name);
|
2016-02-18 21:54:26 +01:00
|
|
|
if(!is_array($value)) $value = array();
|
2016-02-18 20:45:37 +01:00
|
|
|
if (!is_array(self::$request->sel_options[$form_name]))
|
|
|
|
{
|
|
|
|
self::$request->sel_options[$form_name] = array();
|
|
|
|
}
|
|
|
|
$sel_options =& self::$request->sel_options[$form_name];
|
|
|
|
|
|
|
|
// Get user accounts, formatted nicely for grouping and matching
|
|
|
|
// the ajax call calendar_uiforms->ajax_owner() - users first
|
|
|
|
$accounts = array();
|
|
|
|
$list = array('accounts', 'owngroups');
|
|
|
|
foreach($list as $type)
|
|
|
|
{
|
|
|
|
$account_options = array('account_type' => $type);
|
2016-05-01 19:47:59 +02:00
|
|
|
$accounts_type = Api\Accounts::link_query('',$account_options);
|
2016-03-10 18:43:02 +01:00
|
|
|
if($type == 'accounts')
|
|
|
|
{
|
|
|
|
$accounts_type = array_intersect_key($accounts_type, $GLOBALS['egw']->acl->get_grants('calendar'));
|
|
|
|
}
|
|
|
|
$accounts += $accounts_type;
|
2016-02-18 20:45:37 +01:00
|
|
|
}
|
|
|
|
$sel_options += array_map(
|
|
|
|
function($account_id, $account_name) {
|
|
|
|
return array(
|
|
|
|
'value' => ''.$account_id,
|
|
|
|
'label' => $account_name,
|
2016-05-01 11:57:48 +02:00
|
|
|
'app' => lang('api-accounts')
|
2016-02-18 20:45:37 +01:00
|
|
|
);
|
|
|
|
},
|
|
|
|
array_keys($accounts), $accounts
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Add external owners that a select account widget will not find
|
|
|
|
foreach($value as &$owner)
|
|
|
|
{
|
2016-05-02 21:22:52 +02:00
|
|
|
$label = self::get_owner_label($owner);
|
2016-02-18 20:45:37 +01:00
|
|
|
if(!is_numeric($owner))
|
|
|
|
{
|
|
|
|
$resource = $bo->resources[substr($owner, 0,1)];
|
|
|
|
}
|
|
|
|
else if (!in_array($owner, array_keys($accounts)))
|
|
|
|
{
|
2016-05-01 11:57:48 +02:00
|
|
|
$resource = array('app'=> 'api-accounts');
|
2016-02-18 20:45:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$sel_options[] = array('value' => $owner, 'label' => $label, 'app' => lang($resource['app']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:54:26 +01:00
|
|
|
/**
|
|
|
|
* Validate input
|
|
|
|
*
|
|
|
|
* @param string $cname current namespace
|
|
|
|
* @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
|
|
|
* @param array $content
|
|
|
|
* @param array &$validated=array() validated content
|
|
|
|
*/
|
|
|
|
public function validate($cname, array $expand, array $content, &$validated=array())
|
|
|
|
{
|
|
|
|
$form_name = self::form_name($cname, $this->id, $expand);
|
|
|
|
|
|
|
|
if (!$this->is_readonly($cname, $form_name))
|
|
|
|
{
|
|
|
|
$value = $value_in =& self::get_array($content, $form_name);
|
|
|
|
if(!is_array($value))
|
|
|
|
{
|
|
|
|
$value = Array($value);
|
|
|
|
}
|
2016-05-01 11:57:48 +02:00
|
|
|
|
2016-02-18 21:54:26 +01:00
|
|
|
$valid =& self::get_array($validated, $form_name, true);
|
2016-05-01 19:47:59 +02:00
|
|
|
if (true) $valid = $value;
|
2016-02-18 21:54:26 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 20:45:37 +01:00
|
|
|
/**
|
|
|
|
* Handle ajax searches for owner across all supported resources
|
|
|
|
*
|
|
|
|
* @return Array List of matching results
|
|
|
|
*/
|
2016-05-02 21:22:52 +02:00
|
|
|
public static function ajax_owner($id = null)
|
2016-02-18 20:45:37 +01:00
|
|
|
{
|
2016-05-02 21:22:52 +02:00
|
|
|
// Handle a request for a single ID
|
|
|
|
if($id)
|
|
|
|
{
|
|
|
|
$label = self::get_owner_label($id);
|
|
|
|
Api\Json\Response::get()->data($label);
|
|
|
|
return $label;
|
|
|
|
}
|
2016-02-18 20:45:37 +01:00
|
|
|
|
2016-05-03 23:03:51 +02:00
|
|
|
$bo = new calendar_bo();
|
2016-02-18 20:45:37 +01:00
|
|
|
$query = $_REQUEST['query'];
|
|
|
|
// Arbitrarily limited to 50 / resource
|
2016-04-29 20:01:17 +02:00
|
|
|
$options = array('start' => 0, 'num_rows' => 50) +
|
|
|
|
array_diff_key($_REQUEST, array_flip(array('menuaction','query')));
|
2016-03-04 16:57:13 +01:00
|
|
|
$results = array();
|
2016-02-18 20:45:37 +01:00
|
|
|
|
|
|
|
$resources = array_merge(array('' => $bo->resources['']),$bo->resources);
|
|
|
|
foreach($resources as $type => $data)
|
|
|
|
{
|
|
|
|
$mapped = array();
|
|
|
|
$_results = array();
|
|
|
|
|
2016-05-01 19:47:59 +02:00
|
|
|
// Handle Api\Accounts seperately
|
2016-02-18 20:45:37 +01:00
|
|
|
if($type == '')
|
|
|
|
{
|
|
|
|
$list = array('accounts', 'owngroups');
|
|
|
|
foreach($list as $a_type)
|
|
|
|
{
|
|
|
|
$account_options = $options + array('account_type' => $a_type);
|
2016-05-01 19:47:59 +02:00
|
|
|
$_results += Api\Accounts::link_query($query,$account_options);
|
2016-02-18 20:45:37 +01:00
|
|
|
}
|
2016-06-23 15:49:44 +02:00
|
|
|
if (!$options['exec']['nocheckgrants']) $_results = array_intersect_key($_results, $GLOBALS['egw']->acl->get_grants('calendar'));
|
2016-02-18 20:45:37 +01:00
|
|
|
}
|
2016-05-01 19:47:59 +02:00
|
|
|
else if ($data['app'] && Link::get_registry($data['app'], 'query'))
|
2016-02-18 20:45:37 +01:00
|
|
|
{
|
2016-05-01 19:47:59 +02:00
|
|
|
$_results = Link::query($data['app'], $query,$options);
|
2016-02-18 20:45:37 +01:00
|
|
|
}
|
|
|
|
if(!$_results) continue;
|
|
|
|
|
2016-05-01 19:47:59 +02:00
|
|
|
foreach(array_unique($_results) as $id => $title)
|
2016-02-18 20:45:37 +01:00
|
|
|
{
|
|
|
|
if($id && $title)
|
|
|
|
{
|
|
|
|
// Magicsuggest uses id, not value.
|
2016-03-04 16:57:13 +01:00
|
|
|
$value = array(
|
2016-02-18 20:45:37 +01:00
|
|
|
'id' => $type.$id,
|
|
|
|
'value'=> $type.$id,
|
|
|
|
'label' => $title,
|
|
|
|
'app' => lang($data['app'])
|
2016-03-04 16:57:13 +01:00
|
|
|
);
|
2016-02-18 20:45:37 +01:00
|
|
|
if(is_array($value['label']))
|
|
|
|
{
|
|
|
|
$value = array_merge($value, $value['label']);
|
|
|
|
}
|
|
|
|
$mapped[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(count($mapped))
|
|
|
|
{
|
|
|
|
$results = array_merge($results, $mapped);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// switch regular JSON response handling off
|
2016-05-01 19:47:59 +02:00
|
|
|
Api\Json\Request::isJSONRequest(false);
|
2016-02-18 20:45:37 +01:00
|
|
|
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
echo json_encode($results);
|
2016-05-01 19:47:59 +02:00
|
|
|
exit();
|
2016-02-18 20:45:37 +01:00
|
|
|
}
|
2016-05-02 21:22:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get just the label for a single owner
|
|
|
|
* @param string $id
|
|
|
|
*/
|
2016-05-16 22:32:03 +02:00
|
|
|
public static function get_owner_label($id)
|
2016-05-02 21:22:52 +02:00
|
|
|
{
|
|
|
|
static $bo;
|
|
|
|
if(!$bo) $bo = new calendar_bo();
|
|
|
|
|
|
|
|
$id = ''.$id;
|
|
|
|
if(!is_numeric($id))
|
|
|
|
{
|
|
|
|
$resource = $bo->resources[substr($id, 0,1)];
|
|
|
|
$label = Link::title($resource['app'], substr($id,1));
|
2016-06-23 15:49:44 +02:00
|
|
|
|
2016-05-02 21:22:52 +02:00
|
|
|
// Could not get via link, try via resources info
|
|
|
|
if($label === false)
|
|
|
|
{
|
|
|
|
$info = ExecMethod($resource['info'], substr($id,1));
|
|
|
|
$label = $info[0]['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$label = Link::title('api-accounts',$id);
|
|
|
|
}
|
|
|
|
return $label;
|
|
|
|
}
|
2016-02-18 20:45:37 +01:00
|
|
|
}
|