2005-02-03 17:42:20 +01:00
< ? php
2005-11-11 00:35:55 +01:00
/**
2011-10-14 10:36:12 +02:00
* EGroupware - resources
2005-11-11 00:35:55 +01:00
*
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ package resources
* @ link http :// www . egroupware . org
* @ author Cornelius Weiss < egw @ von - und - zu - weiss . de >
* @ author Lukas Weiss < wnz_gh05t @ users . sourceforge . net >
* @ version $Id $
*/
2005-11-09 22:03:35 +01:00
2016-05-05 22:22:04 +02:00
use EGroupware\Api ;
use EGroupware\Api\Link ;
use EGroupware\Api\Egw ;
use EGroupware\Api\Acl ;
use EGroupware\Api\Vfs ;
2005-11-09 22:03:35 +01:00
/**
* General business object for resources
*
* @ package resources
*/
2011-03-23 16:25:59 +01:00
class resources_bo
2005-02-03 17:42:20 +01:00
{
2016-05-09 12:05:57 +02:00
/**
* Path where the icons are stored ( relative to webserver_url )
*/
const ICON_PATH = '/api/images' ;
2012-12-17 23:14:32 +01:00
const DELETED = 'deleted' ;
2008-10-10 19:04:22 +02:00
const PICTURE_NAME = '.picture.jpg' ;
2005-02-17 09:04:55 +01:00
var $resource_icons = '/resources/templates/default/images/resource_icons/' ;
2005-06-13 14:21:18 +02:00
var $debug = 0 ;
2007-06-25 18:43:32 +02:00
/**
* Instance of resources so object
*
2011-10-13 08:21:46 +02:00
* @ var resources_so
2007-06-25 18:43:32 +02:00
*/
var $so ;
2009-11-05 20:37:28 +01:00
/**
2016-05-05 22:22:04 +02:00
* Instance of resources Acl class
2009-11-05 20:37:28 +01:00
*
2016-05-26 23:32:00 +02:00
* @ var resources_acl_bo
2009-11-05 20:37:28 +01:00
*/
var $acl ;
/**
2016-05-05 22:22:04 +02:00
* Instance of Api\Categories class for resources
2009-11-05 20:37:28 +01:00
*/
var $cats ;
2008-04-20 21:37:12 +02:00
2012-11-13 18:26:31 +01:00
/**
* List of filter options
*/
public static $filter_options = array (
- 1 => 'resources' ,
- 2 => 'accessories' ,
- 3 => 'resources and accessories'
// Accessories of a resource added when resource selected
);
2012-12-17 23:14:32 +01:00
public static $field2label = array (
'res_id' => 'Resource ID' ,
'name' => 'name' ,
'short_description' => 'short description' ,
'cat_id' => 'Category' ,
'quantity' => 'Quantity' ,
'useable' => 'Useable' ,
'location' => 'Location' ,
'storage_info' => 'Storage' ,
'bookable' => 'Bookable' ,
'buyable' => 'Buyable' ,
'prize' => 'Prize' ,
'long_description' => 'Long description' ,
'inventory_number' => 'inventory number' ,
'accessory_of' => 'Accessory of'
);
2014-07-23 14:30:39 +02:00
/**
* Constructor
*
2016-05-05 22:22:04 +02:00
* @ param int $user = null account_id of user to use for Acl , default current user
2014-07-23 14:30:39 +02:00
*/
function __construct ( $user = null )
2005-02-03 17:42:20 +01:00
{
2011-03-23 16:25:59 +01:00
$this -> so = new resources_so ();
2016-05-26 23:32:00 +02:00
$this -> acl = new resources_acl_bo ( $user );
2005-02-16 11:26:22 +01:00
$this -> cats = $this -> acl -> egw_cats ;
2008-04-20 21:37:12 +02:00
$this -> cal_right_transform = array (
2016-05-26 23:32:00 +02:00
resources_acl_bo :: CAL_READ => Acl :: READ ,
resources_acl_bo :: DIRECT_BOOKING => Acl :: READ | Acl :: ADD | Acl :: EDIT | Acl :: DELETE ,
resources_acl_bo :: CAT_ADMIN => Acl :: READ | Acl :: ADD | Acl :: EDIT | Acl :: DELETE ,
2005-06-12 11:54:29 +02:00
);
2005-02-03 17:42:20 +01:00
}
2005-06-10 22:40:57 +02:00
/**
* get rows for resources list
*
2005-06-13 11:18:06 +02:00
* Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-06-10 22:40:57 +02:00
*/
2005-02-03 17:42:20 +01:00
function get_rows ( $query , & $rows , & $readonlys )
{
2013-01-02 18:32:30 +01:00
if ( ! $query [ 'csv_export' ])
{
2016-05-05 22:22:04 +02:00
Api\Cache :: setSession ( 'resources' , 'index_nm' , $query );
2013-01-02 18:32:30 +01:00
}
2009-11-05 20:37:28 +01:00
if ( $query [ 'store_state' ]) // request to store state in session and filter in prefs?
{
2016-05-05 22:22:04 +02:00
Api\Cache :: setSession ( 'resources' , $query [ 'store_state' ], $query );
2009-11-05 20:37:28 +01:00
//echo "<p>".__METHOD__."() query[filter]=$query[filter], prefs[resources][filter]={$GLOBALS['egw_info']['user']['preferences']['resources']['filter']}</p>\n";
if ( $query [ 'filter' ] != $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'resources' ][ 'filter' ])
{
$GLOBALS [ 'egw' ] -> preferences -> add ( 'resources' , 'filter' , $query [ 'filter' ], 'user' );
$GLOBALS [ 'egw' ] -> preferences -> save_repository ();
}
}
2005-06-13 14:21:18 +02:00
if ( $this -> debug ) _debug_array ( $query );
2005-06-29 11:30:08 +02:00
$read_onlys = 'res_id,name,short_description,quantity,useable,bookable,buyable,cat_id,location,storage_info' ;
2008-04-20 21:37:12 +02:00
2012-11-13 18:26:31 +01:00
$filter = array ();
$join = '' ;
$extra_cols = array ();
// Sub-query to get the count of accessories
$acc_join = " LEFT JOIN (SELECT accessory_of AS accessory_id, count(res_id) as acc_count FROM { $this -> so -> table_name } GROUP BY accessory_of) AS acc ON acc.accessory_id = { $this -> so -> table_name } .res_id " ;
switch ( $query [ 'filter2' ])
{
case - 1 :
// Resources only
$filter [ 'accessory_of' ] = - 1 ;
$join = $acc_join ;
$extra_cols [] = 'acc_count' ;
break ;
case - 2 :
// Accessories only
$filter [] = 'accessory_of != -1' ;
break ;
case - 3 :
// All
$join = $acc_join ;
$extra_cols [] = 'acc_count' ;
break ;
2012-12-17 23:14:32 +01:00
case self :: DELETED :
2013-01-02 23:15:26 +01:00
$join = $acc_join ;
$extra_cols [] = 'acc_count' ;
2012-12-17 23:14:32 +01:00
$filter [] = 'deleted IS NOT NULL' ;
break ;
2012-11-13 18:26:31 +01:00
default :
2012-11-14 19:00:02 +01:00
$filter [ 'accessory_of' ] = $query [ 'filter2' ];
2012-11-13 18:26:31 +01:00
}
2012-12-17 23:14:32 +01:00
if ( $query [ 'filter2' ] != self :: DELETED )
{
$filter [ 'deleted' ] = null ;
}
2014-07-23 14:30:39 +02:00
2005-06-13 14:21:18 +02:00
if ( $query [ 'filter' ])
{
2016-05-05 22:22:04 +02:00
if (( $children = $this -> acl -> get_cats ( Acl :: READ , $query [ 'filter' ])))
2009-08-10 21:50:36 +02:00
{
$filter [ 'cat_id' ] = array_keys ( $children );
$filter [ 'cat_id' ][] = $query [ 'filter' ];
}
else
{
$filter [ 'cat_id' ] = $query [ 'filter' ];
}
2005-06-13 14:21:18 +02:00
}
2016-05-05 22:22:04 +02:00
elseif (( $readcats = $this -> acl -> get_cats ( Acl :: READ )))
2005-06-13 14:21:18 +02:00
{
2009-11-06 19:47:31 +01:00
$filter [ 'cat_id' ] = array_keys ( $readcats );
}
2010-07-08 17:01:40 +02:00
// if there is no catfilter -> this means you have no rights, so set the cat filter to null
2011-03-23 16:25:59 +01:00
if ( ! isset ( $filter [ 'cat_id' ]) || empty ( $filter [ 'cat_id' ])) {
$filter [ 'cat_id' ] = NUll ;
}
2010-07-08 17:01:40 +02:00
2009-11-06 19:47:31 +01:00
if ( $query [ 'show_bookable' ])
{
$filter [ 'bookable' ] = true ;
2005-06-13 14:21:18 +02:00
}
2005-02-03 17:42:20 +01:00
$order_by = $query [ 'order' ] ? $query [ 'order' ] . ' ' . $query [ 'sort' ] : '' ;
2005-06-11 14:59:11 +02:00
$start = ( int ) $query [ 'start' ];
2008-04-20 21:37:12 +02:00
2012-01-23 14:54:44 +01:00
foreach ( $filter as $k => $v ) $query [ 'col_filter' ][ $k ] = $v ;
2012-11-13 18:26:31 +01:00
$this -> so -> get_rows ( $query , $rows , $readonlys , $join , false , false , $extra_cols );
2005-06-11 14:59:11 +02:00
$nr = $this -> so -> total ;
2008-04-20 21:37:12 +02:00
2005-09-06 23:45:37 +02:00
// we are called to serve bookable resources (e.g. calendar-dialog)
if ( $query [ 'show_bookable' ])
{
// This is somehow ugly, i know...
2006-01-20 08:10:18 +01:00
foreach (( array ) $rows as $num => $resource )
2005-09-06 23:45:37 +02:00
{
$rows [ $num ][ 'default_qty' ] = 1 ;
}
// we don't need all the following testing
return $nr ;
}
2008-04-20 21:37:12 +02:00
2016-05-05 22:22:04 +02:00
$config = Api\Config :: read ( 'resources' );
2011-06-08 04:27:42 +02:00
foreach ( $rows as $num => & $resource )
2005-02-03 17:42:20 +01:00
{
2016-05-05 22:22:04 +02:00
if ( ! $this -> acl -> is_permitted ( $resource [ 'cat_id' ], Acl :: EDIT ))
2005-02-03 17:42:20 +01:00
{
2005-06-29 11:30:08 +02:00
$readonlys [ " edit[ $resource[res_id] ] " ] = true ;
2005-02-03 17:42:20 +01:00
}
2012-12-17 23:14:32 +01:00
elseif ( $resource [ 'deleted' ])
{
$resource [ 'class' ] .= 'deleted ' ;
}
2016-05-05 22:22:04 +02:00
if ( ! $this -> acl -> is_permitted ( $resource [ 'cat_id' ], Acl :: DELETE ) ||
2012-12-17 23:14:32 +01:00
( $resource [ 'deleted' ] && ! $GLOBALS [ 'egw_info' ][ 'user' ][ 'apps' ][ 'admin' ] && $config [ 'history' ] == 'history' )
)
2005-02-03 17:42:20 +01:00
{
2005-06-29 11:30:08 +02:00
$readonlys [ " delete[ $resource[res_id] ] " ] = true ;
2011-06-08 04:27:42 +02:00
$resource [ 'class' ] .= 'no_delete ' ;
2005-02-03 17:42:20 +01:00
}
2016-05-05 22:22:04 +02:00
if (( ! $this -> acl -> is_permitted ( $resource [ 'cat_id' ], Acl :: ADD )) ||
2012-12-05 00:57:02 +01:00
// Allow new accessory action when viewing accessories of a certain resource
$query [ 'filter2' ] <= 0 && $resource [ 'accessory_of' ] != - 1 )
2005-02-28 16:19:31 +01:00
{
2005-06-29 11:30:08 +02:00
$readonlys [ " new_acc[ $resource[res_id] ] " ] = true ;
2011-06-08 04:27:42 +02:00
$resource [ 'class' ] .= 'no_new_accessory ' ;
2005-02-28 16:19:31 +01:00
}
2007-02-25 21:04:41 +01:00
if ( ! $resource [ 'bookable' ])
2005-02-03 17:42:20 +01:00
{
2005-06-29 11:30:08 +02:00
$readonlys [ " bookable[ $resource[res_id] ] " ] = true ;
2005-10-19 00:07:12 +02:00
$readonlys [ " calendar[ $resource[res_id] ] " ] = true ;
2011-06-08 04:27:42 +02:00
$resource [ 'class' ] .= 'no_book ' ;
$resource [ 'class' ] .= 'no_view_calendar ' ;
2005-02-03 17:42:20 +01:00
}
2016-05-26 23:32:00 +02:00
if ( ! $this -> acl -> is_permitted ( $resource [ 'cat_id' ], resources_acl_bo :: CAL_READ ))
2007-02-25 21:04:41 +01:00
{
$readonlys [ " calendar[ $resource[res_id] ] " ] = true ;
2011-06-08 04:27:42 +02:00
$resource [ 'class' ] .= 'no_view_calendar ' ;
2007-02-25 21:04:41 +01:00
}
2005-02-17 15:42:34 +01:00
if ( ! $resource [ 'buyable' ])
{
2005-06-29 11:30:08 +02:00
$readonlys [ " buyable[ $resource[res_id] ] " ] = true ;
2011-06-08 04:27:42 +02:00
$resource [ 'class' ] .= 'no_buy ' ;
2005-02-17 15:42:34 +01:00
}
2012-11-13 18:26:31 +01:00
$readonlys [ " view_acc[ { $resource [ 'res_id' ] } ] " ] = ( $resource [ 'acc_count' ] == 0 );
2012-11-26 22:04:31 +01:00
$resource [ 'class' ] .= ( $resource [ 'accessory_of' ] ==- 1 ? 'resource ' : 'accessory ' );
2012-11-13 18:26:31 +01:00
if ( $resource [ 'acc_count' ])
2005-02-25 14:18:40 +01:00
{
2012-11-13 18:26:31 +01:00
$resource [ 'class' ] .= 'hasAccessories ' ;
2013-01-02 23:15:26 +01:00
$accessories = $this -> get_acc_list ( $resource [ 'res_id' ], $query [ 'filter2' ] == self :: DELETED );
2012-11-26 22:04:31 +01:00
foreach ( $accessories as $acc_id => $acc_name )
{
2012-12-03 21:20:42 +01:00
$resource [ 'accessories' ][] = array ( 'acc_id' => $acc_id , 'name' => $this -> link_title ( $acc_id ));
2012-11-26 22:04:31 +01:00
}
} elseif ( $resource [ 'accessory_of' ] > 0 ) {
$resource [ 'accessory_of_label' ] = $this -> link_title ( $resource [ 'accessory_of' ]);
2005-02-25 14:18:40 +01:00
}
2012-11-13 18:26:31 +01:00
2012-12-19 19:12:42 +01:00
if ( $resource [ 'deleted' ])
{
$rows [ $num ][ 'picture_thumb' ] = 'deleted' ;
}
else
{
2015-04-08 16:32:13 +02:00
$rows [ $num ][ 'picture_thumb' ] = $this -> get_picture ( $resource , false );
if ( $rows [ $num ][ 'picture_src' ] == 'own_src' )
{
// VFS picture fullsize
$rows [ $num ][ 'picture_original' ] = 'webdav.php/apps/resources/' . $rows [ $num ][ 'res_id' ] . '/.picture.jpg' ;
}
else
{
// cat or generic icon fullsize
$rows [ $num ][ 'picture_original' ] = $this -> get_picture ( $resource , true );
}
2012-12-19 19:12:42 +01:00
}
2005-02-20 19:01:52 +01:00
$rows [ $num ][ 'admin' ] = $this -> acl -> get_cat_admin ( $resource [ 'cat_id' ]);
2005-02-03 17:42:20 +01:00
}
2013-01-02 18:32:30 +01:00
2016-05-05 22:22:04 +02:00
if ( ! Api\Storage\Customfields :: get ( 'resources' ))
2013-01-02 18:32:30 +01:00
{
$rows [ 'no_customfields' ] = true ;
}
2005-02-03 17:42:20 +01:00
return $nr ;
}
2005-06-10 22:40:57 +02:00
/**
* reads a resource exept binary datas
*
2005-06-13 11:18:06 +02:00
* Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-06-29 11:30:08 +02:00
* @ param int $res_id resource id
2005-06-10 22:40:57 +02:00
* @ return array with key => value or false if not found or allowed
*/
2005-06-29 11:30:08 +02:00
function read ( $res_id )
2005-02-03 17:42:20 +01:00
{
2009-10-12 21:16:42 +02:00
if ( ! ( $data = $this -> so -> read ( array ( 'res_id' => $res_id ))))
2005-02-03 17:42:20 +01:00
{
2009-10-12 21:16:42 +02:00
return null ; // not found
}
2016-05-05 22:22:04 +02:00
if ( ! $this -> acl -> is_permitted ( $data [ 'cat_id' ], Acl :: READ ))
2009-10-12 21:16:42 +02:00
{
return false ; // permission denied
2005-02-03 17:42:20 +01:00
}
2009-10-12 21:16:42 +02:00
return $data ;
2005-02-03 17:42:20 +01:00
}
2008-04-20 21:37:12 +02:00
2005-06-10 22:40:57 +02:00
/**
* saves a resource . pictures are saved in vfs
*
2005-06-13 11:18:06 +02:00
* Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-06-10 22:40:57 +02:00
* @ param array $resource array with key => value of all needed datas
* @ return string msg if somthing went wrong ; nothing if all right
*/
2005-02-12 09:25:26 +01:00
function save ( $resource )
2005-02-03 17:42:20 +01:00
{
2016-05-05 22:22:04 +02:00
if ( ! $this -> acl -> is_permitted ( $resource [ 'cat_id' ], Acl :: EDIT ))
2005-02-12 16:49:38 +01:00
{
2011-04-04 21:07:23 +02:00
return lang ( 'You are not permitted to edit this resource!' );
2005-02-12 16:49:38 +01:00
}
2012-11-26 22:04:31 +01:00
$old = array ();
2005-02-24 19:24:26 +01:00
// we need an id to save pictures and make links...
2005-06-29 11:30:08 +02:00
if ( ! $resource [ 'res_id' ])
2005-02-15 17:26:21 +01:00
{
2014-03-18 10:39:30 +01:00
$resource [ 'res_owner' ] = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ];
2016-05-05 22:22:04 +02:00
$resource [ 'res_created' ] = Api\DateTime :: server2user ( time (), 'ts' );
2005-06-29 11:30:08 +02:00
$resource [ 'res_id' ] = $this -> so -> save ( $resource );
2005-02-15 17:26:21 +01:00
}
2012-11-26 22:04:31 +01:00
else
{
2014-03-18 10:39:30 +01:00
$resource [ 'res_modifier' ] = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ];
2016-05-05 22:22:04 +02:00
$resource [ 'res_modified' ] = Api\DateTime :: server2user ( time (), 'ts' );
2012-11-26 22:04:31 +01:00
$old = $this -> read ( $resource [ 'res_id' ]);
}
2005-02-15 17:26:21 +01:00
2005-02-17 10:53:46 +01:00
switch ( $resource [ 'picture_src' ])
2005-02-12 09:25:26 +01:00
{
2005-02-17 10:53:46 +01:00
case 'own_src' :
if ( $resource [ 'own_file' ][ 'size' ] > 0 )
{
2005-06-29 11:30:08 +02:00
$msg = $this -> save_picture ( $resource [ 'own_file' ], $resource [ 'res_id' ]);
2013-07-25 17:01:41 +02:00
unset ( $resource [ 'own_file' ]);
2005-02-17 10:53:46 +01:00
break ;
}
2017-01-19 15:30:12 +01:00
elseif ( @ Vfs :: stat ( '/apps/resources/' . $resource [ 'res_id' ] . '/' . self :: PICTURE_NAME ))
2005-02-17 10:53:46 +01:00
{
break ;
}
$resource [ 'picture_src' ] = 'cat_src' ;
case 'cat_src' :
break ;
case 'gen_src' :
2014-03-18 15:14:00 +01:00
$resource [ 'picture_src' ] = 'gen_src' ;
2005-02-17 10:53:46 +01:00
break ;
default :
if ( $resource [ 'own_file' ][ 'size' ] > 0 )
{
$resource [ 'picture_src' ] = 'own_src' ;
2005-06-29 11:30:08 +02:00
$msg = $this -> save_picture ( $resource [ 'own_file' ], $resource [ 'res_id' ]);
2005-02-17 10:53:46 +01:00
}
else
{
$resource [ 'picture_src' ] = 'cat_src' ;
}
}
// somthing went wrong on saving own picture
if ( $msg )
{
return $msg ;
2005-02-12 09:25:26 +01:00
}
2008-04-20 21:37:12 +02:00
2012-12-17 23:14:32 +01:00
// Check for restore of deleted, restore held links
2017-01-19 15:30:12 +01:00
if ( $old && $old [ 'deleted' ] && ! $resource [ 'deleted' ])
{
Link :: restore ( 'resources' , $resource [ 'res_id' ]);
}
2012-12-17 23:14:32 +01:00
2005-02-17 10:53:46 +01:00
// delete old pictures
if ( $resource [ 'picture_src' ] != 'own_src' )
2005-02-12 09:25:26 +01:00
{
2005-06-29 11:30:08 +02:00
$this -> remove_picture ( $resource [ 'res_id' ]);
2005-02-12 09:25:26 +01:00
}
2005-02-24 19:24:26 +01:00
2012-11-26 22:04:31 +01:00
// Update link title
2016-05-05 22:22:04 +02:00
Link :: notify_update ( 'resources' , $resource [ 'res_id' ], $resource );
2005-02-24 19:24:26 +01:00
// save links
if ( is_array ( $resource [ 'link_to' ][ 'to_id' ]))
{
2016-05-05 22:22:04 +02:00
Link :: link ( 'resources' , $resource [ 'res_id' ], $resource [ 'link_to' ][ 'to_id' ]);
2005-02-24 19:24:26 +01:00
}
2012-11-26 22:04:31 +01:00
if ( $resource [ 'accessory_of' ] != $old [ 'accessory_of' ])
{
2016-05-05 22:22:04 +02:00
Link :: unlink ( 0 , 'resources' , $resource [ 'res_id' ], '' , 'resources' , $old [ 'accessory_of' ]);
2013-01-07 17:21:08 +01:00
// Check for resource changing to accessory - move its accessories to resource
2016-06-29 10:50:20 +02:00
if ( $old [ 'accessory_of' ] == - 1 && ( $accessories = $this -> get_acc_list ( $resource [ 'res_id' ])))
2013-01-07 17:21:08 +01:00
{
foreach ( $accessories as $accessory => $name )
{
2016-05-05 22:22:04 +02:00
Link :: unlink ( 0 , 'resources' , $accessory , '' , 'resources' , $resource [ 'res_id' ]);
2016-06-29 10:50:20 +02:00
if (( $acc = $this -> read ( $accessory )))
{
$acc [ 'accessory_of' ] = - 1 ;
$this -> so -> save ( $acc );
}
2013-01-07 17:21:08 +01:00
}
}
2012-11-26 22:04:31 +01:00
}
2005-02-28 16:19:31 +01:00
if ( $resource [ 'accessory_of' ] != - 1 )
2005-11-03 23:48:30 +01:00
{
2016-05-05 22:22:04 +02:00
Link :: link ( 'resources' , $resource [ 'res_id' ], 'resources' , $resource [ 'accessory_of' ]);
2005-02-28 16:19:31 +01:00
}
2008-04-20 21:37:12 +02:00
2005-11-03 23:48:30 +01:00
if ( ! empty ( $resource [ 'res_id' ]) && $this -> so -> get_value ( " cat_id " , $resource [ 'res_id' ]) != $resource [ 'cat_id' ] && $resource [ 'accessory_of' ] == - 1 )
{
$accessories = $this -> get_acc_list ( $resource [ 'res_id' ]);
foreach ( $accessories as $accessory => $name )
{
2016-06-29 10:50:20 +02:00
if (( $acc = $this -> so -> read ( $accessory )))
{
$acc [ 'cat_id' ] = $resource [ 'cat_id' ];
$this -> so -> data = $acc ;
$this -> so -> save ();
}
2005-11-03 23:48:30 +01:00
}
}
2014-07-23 14:30:39 +02:00
2012-11-27 17:47:12 +01:00
$res_id = $this -> so -> save ( $resource );
2012-12-17 23:14:32 +01:00
// History & notifications
if ( ! is_object ( $this -> tracking ))
{
$this -> tracking = new resources_tracking ();
}
if ( $this -> tracking -> track ( $resource , $old , $this -> user ) === false )
{
return implode ( ', ' , $this -> tracking -> errors );
}
2012-11-27 17:47:12 +01:00
return $res_id ? $res_id : lang ( 'Something went wrong by saving resource' );
2005-02-03 17:42:20 +01:00
}
2005-06-10 22:40:57 +02:00
/**
* deletes resource including pictures and links
*
* @ author Lukas Weiss < wnz_gh05t @ users . sourceforge . net >
2005-06-29 11:30:08 +02:00
* @ param int $res_id id of resource
2016-06-29 10:50:20 +02:00
* @ return string | false string with error or false on success
2005-06-10 22:40:57 +02:00
*/
2005-06-29 11:30:08 +02:00
function delete ( $res_id )
2005-02-03 17:42:20 +01:00
{
2016-05-05 22:22:04 +02:00
if ( ! $this -> acl -> is_permitted ( $this -> so -> get_value ( 'cat_id' , $res_id ), Acl :: DELETE ))
2005-12-04 23:06:02 +01:00
{
2011-04-04 21:07:23 +02:00
return lang ( 'You are not permitted to delete this resource!' );
2005-12-04 23:06:02 +01:00
}
2008-04-20 21:37:12 +02:00
2012-12-31 22:57:14 +01:00
// check if we only mark resources as deleted, or really delete them
2016-05-05 22:22:04 +02:00
$config = Api\Config :: read ( 'resources' );
2016-06-29 10:50:20 +02:00
if ( ! ( $old = $this -> read ( $res_id )))
{
// error is returned at end of function
}
elseif ( $config [ 'history' ] != '' && $old [ 'deleted' ] == null )
2012-12-17 23:14:32 +01:00
{
$old [ 'deleted' ] = time ();
$this -> save ( $old );
2016-05-05 22:22:04 +02:00
Link :: unlink ( 0 , 'resources' , $res_id , '' , '' , '' , true );
2012-12-31 22:57:14 +01:00
$accessories = $this -> get_acc_list ( $res_id );
foreach ( $accessories as $acc_id => $name )
{
2013-01-02 23:15:26 +01:00
// Don't purge already deleted accessories
2016-06-29 10:50:20 +02:00
if (( $acc = $this -> read ( $acc_id )) && ! $acc [ 'deleted' ])
2013-01-02 23:15:26 +01:00
{
$acc [ 'deleted' ] = time ();
$this -> save ( $acc );
2016-05-05 22:22:04 +02:00
Link :: unlink ( 0 , 'resources' , $acc_id , '' , '' , '' , true );
2013-01-02 23:15:26 +01:00
}
2012-12-31 22:57:14 +01:00
}
2012-12-17 23:14:32 +01:00
return false ;
}
elseif ( $this -> so -> delete ( array ( 'res_id' => $res_id )))
2005-10-08 13:06:58 +02:00
{
2013-01-08 18:27:00 +01:00
$accessories = $this -> get_acc_list ( $res_id , true );
2012-11-07 00:17:26 +01:00
foreach ( $accessories as $acc_id => $name )
{
2016-06-29 10:50:20 +02:00
if ( $this -> delete ( $acc_id ) && ( $acc = $this -> read ( $acc_id )))
2012-11-07 00:17:26 +01:00
{
$acc [ 'accessory_of' ] = - 1 ;
$this -> save ( $acc );
}
};
2005-10-08 13:06:58 +02:00
$this -> remove_picture ( $res_id );
2016-05-05 22:22:04 +02:00
Link :: unlink ( 0 , 'resources' , $res_id );
2005-10-08 13:06:58 +02:00
// delete the resource from the calendar
2008-06-12 09:49:30 +02:00
ExecMethod ( 'calendar.calendar_so.deleteaccount' , 'r' . $res_id );
2005-10-08 13:06:58 +02:00
return false ;
}
return lang ( 'Something went wrong by deleting resource' );
2005-02-03 17:42:20 +01:00
}
2008-04-20 21:37:12 +02:00
2005-06-10 22:40:57 +02:00
/**
* gets list of accessories for resource
*
2005-06-13 11:18:06 +02:00
* Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-06-29 11:30:08 +02:00
* @ param int $res_id id of resource
2013-01-02 23:15:26 +01:00
* @ param boolean $deleted Include deleted accessories
2005-06-10 22:40:57 +02:00
* @ return array
*/
2013-01-02 23:15:26 +01:00
function get_acc_list ( $res_id , $deleted = false )
2005-02-24 14:10:57 +01:00
{
2005-06-29 11:30:08 +02:00
if ( $res_id < 1 ){ return ;}
2013-01-02 23:15:26 +01:00
$data = $this -> so -> search ( '' , 'res_id,name,deleted' , '' , '' , '' , '' , '' , $start , array ( 'accessory_of' => $res_id ), '' , $need_full_no_count = true );
2012-02-28 21:24:37 +01:00
$acc_list = array ();
2012-04-13 00:18:22 +02:00
if ( $data ) {
foreach ( $data as $num => $resource )
{
2013-01-02 23:15:26 +01:00
if ( $resource [ 'deleted' ] && ! $deleted ) continue ;
2012-04-13 00:18:22 +02:00
$acc_list [ $resource [ 'res_id' ]] = $resource [ 'name' ];
}
2005-02-24 14:10:57 +01:00
}
return $acc_list ;
}
2008-04-20 21:37:12 +02:00
2016-07-19 22:09:58 +02:00
/**
* Search for resources for calendar to select as participants
2017-01-19 15:30:12 +01:00
*
2016-07-19 22:09:58 +02:00
* Search and options match Link :: query ()
2017-01-19 15:30:12 +01:00
*
2016-07-19 22:09:58 +02:00
* Resources return actual resources as well as categories that match
2017-01-19 15:30:12 +01:00
*
2016-07-19 22:09:58 +02:00
* @ param String $search - Search string
* @ param Array $options - search options
* @ see Link :: query ()
2017-01-19 15:30:12 +01:00
*
2016-07-19 22:09:58 +02:00
* @ return Array List of ID => Title entries matching search string
*/
public static function calendar_search ( $search , $options )
{
2016-09-12 19:42:27 +02:00
$bo = new resources_bo ();
// Resources - call direct to avoid cache
$list = $bo -> link_query ( $search , $options );
2016-07-19 22:09:58 +02:00
// Categories
$cats = $bo -> acl -> get_cats ( Acl :: READ );
foreach ( $cats as $cat_id => $cat )
{
if ( $cat && stripos ( $cat , $search ) !== FALSE )
{
// Get resources for that category
2016-09-28 19:01:10 +02:00
if ( ! $options [ 'exec' ])
{
$resources = $bo -> get_resources_by_category ( $cat_id );
}
else
{
$cat_options = $options ;
$cat_options [ 'cat_id' ] = $cat_id ;
$resources = $bo -> link_query ( '' , $cat_options );
}
2017-01-19 15:30:12 +01:00
2016-07-19 22:09:58 +02:00
// Edit dialog sends exec as an option, don't add categories
if ( count ( $resources ) && ! $options [ 'exec' ])
{
2016-07-20 20:01:07 +02:00
$_resources = array_map (
function ( $id ) { return 'r' . $id ;},
array_keys ( $resources )
);
2016-07-19 22:09:58 +02:00
$list [ 'cat-' . $cat_id ] = array (
2016-08-18 19:42:50 +02:00
'label' => $bo -> acl -> egw_cats -> id2name ( $cat_id ),
2016-07-20 20:01:07 +02:00
'resources' => $_resources ,
2016-07-19 22:09:58 +02:00
);
}
else if ( $resources && $options [ 'exec' ])
{
array_map (
2016-09-12 19:42:27 +02:00
function ( $id , $name ) use ( & $list ) { if ( ! $list [ '' . $id ]) $list [ '' . $id ] = $name ;},
2016-07-19 22:09:58 +02:00
array_keys ( $resources ), $resources
);
}
}
}
return $list ;
}
/**
* Get a list of resources ( ID => name ) matching a single category ID
* @ param int $cat_id
* @ return array ()
*/
public function get_resources_by_category ( $cat_id )
{
$resources = array ();
$filter = array (
'cat_id' => $cat_id ,
//'accessory_of' => '-1'
'deleted' => null
);
$only_keys = 'res_id,name' ;
$data = $this -> so -> search ( array (), $only_keys , $order_by = 'name' , $extra_cols = '' , $wildcard = '%' , $empty , $op = 'OR' , $limit , $filter );
foreach ( $data as $resource )
{
$resources [ $resource [ 'res_id' ]] = $resource [ 'name' ];
}
return $resources ;
}
2005-06-11 14:59:11 +02:00
/**
* returns info about resource for calender
2005-09-06 23:45:37 +02:00
* @ author Cornelius Weiss < egw @ von - und - zu - weiss . de >
2016-07-19 22:09:58 +02:00
* @ param int | array | string $res_id single id , array $num => $res_id or
* 'cat-<cat_id>' for the whole category
2008-04-20 21:37:12 +02:00
* @ return array
2005-06-11 14:59:11 +02:00
*/
2005-06-12 10:01:19 +02:00
function get_calendar_info ( $res_id )
2005-06-11 14:59:11 +02:00
{
2016-07-19 22:09:58 +02:00
//error_log(__METHOD__ . "(".print_r($res_id,true).")");
// Resource category
if ( is_string ( $res_id ) && strpos ( $res_id , 'cat-' ) === 0 )
{
$cat_id = ( int ) substr ( $res_id , 4 );
if ( ! $this -> acl -> is_permitted ( $cat_id , Acl :: READ ))
{
return array ();
}
return array ( array (
'name' => $this -> acl -> get_cat_name ( $cat_id ),
'rights' => $this -> acl -> get_permissions ( $cat_id ),
'resources' => array_map (
function ( $id ) { return 'r' . $id ;},
array_keys ( $this -> get_resources_by_category ( $cat_id ))
)
));
}
2017-01-19 15:30:12 +01:00
2005-06-12 12:05:39 +02:00
if ( ! is_array ( $res_id ) && $res_id < 1 ) return ;
2009-09-29 12:02:09 +02:00
$data = $this -> so -> search ( array ( 'res_id' => $res_id ), self :: TITLE_COLS . ',useable' );
if ( ! is_array ( $data ))
{
2016-07-19 22:09:58 +02:00
//error_log(__METHOD__." No Calendar Data found for Resource with id $res_id");
2009-02-20 13:52:16 +01:00
return array ();
}
2009-09-29 12:02:09 +02:00
foreach ( $data as $num => & $resource )
2005-06-12 10:16:12 +02:00
{
2009-09-29 12:02:09 +02:00
$resource [ 'rights' ] = false ;
2005-06-12 11:54:29 +02:00
foreach ( $this -> cal_right_transform as $res_right => $cal_right )
{
2005-06-12 12:05:39 +02:00
if ( $this -> acl -> is_permitted ( $resource [ 'cat_id' ], $res_right ))
2005-06-12 11:54:29 +02:00
{
2009-09-29 12:02:09 +02:00
$resource [ 'rights' ] = $cal_right ;
2005-06-12 11:54:29 +02:00
}
}
2009-09-29 12:02:09 +02:00
$resource [ 'responsible' ] = $this -> acl -> get_cat_admin ( $resource [ 'cat_id' ]);
// preseed the cache
2016-05-05 22:22:04 +02:00
Link :: set_cache ( 'resources' , $resource [ 'res_id' ], $t = $this -> link_title ( $resource ));
2005-06-12 10:16:12 +02:00
}
2005-06-12 12:05:39 +02:00
return $data ;
2005-06-11 14:59:11 +02:00
}
2008-04-20 21:37:12 +02:00
2005-06-12 10:56:19 +02:00
/**
* returns status for a new calendar entry depending on resources ACL
2005-06-13 11:18:06 +02:00
* @ author Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-07-01 17:01:07 +02:00
* @ param int $res_id single id
2009-10-12 21:16:42 +02:00
* @ return string | boolean false if resource not found , no read rights or not bookable , else A if user has direkt booking rights or U if no dirket booking
2005-06-12 10:56:19 +02:00
*/
function get_calendar_new_status ( $res_id )
{
2009-10-12 21:16:42 +02:00
if ( ! ( $data = $this -> read ( $res_id )) || ! $data [ 'bookable' ])
{
return false ;
}
2016-05-26 23:32:00 +02:00
return $this -> acl -> is_permitted ( $data [ 'cat_id' ], resources_acl_bo :: DIRECT_BOOKING ) ? A : U ;
2005-06-12 10:56:19 +02:00
}
2008-04-20 21:37:12 +02:00
2005-06-10 22:40:57 +02:00
/**
2005-06-13 11:18:06 +02:00
* @ author Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-06-10 22:40:57 +02:00
* query infolog for entries matching $pattern
2008-10-10 12:24:31 +02:00
* @ param string | array $pattern if it 's a string it is the string we will search for as a criteria, if it' s an array we
* will seach for 'search' key in this array to get the string criteria . others keys handled are actually used
* for calendar disponibility .
2009-12-09 00:09:45 +01:00
* @ param array $options Array of options for the search
2005-06-10 22:40:57 +02:00
*
*/
2009-12-09 00:09:45 +01:00
function link_query ( $pattern , Array & $options = array () )
2005-02-25 12:15:27 +01:00
{
2008-10-10 12:24:31 +02:00
if ( is_array ( $pattern ))
{
$criteria = array ( 'name' => $pattern [ 'search' ]
, 'short_description' => $pattern [ 'search' ]);
}
else
{
$criteria = array ( 'name' => $pattern
, 'short_description' => $pattern );
}
$only_keys = 'res_id,name,short_description,bookable,useable' ;
2013-01-14 19:42:16 +01:00
// If no read access to any category, just stop
2016-05-05 22:22:04 +02:00
if ( ! $this -> acl -> get_cats ( Acl :: READ ))
2013-01-14 19:42:16 +01:00
{
$options [ 'total' ] = 0 ;
return array ();
}
2006-06-27 18:30:53 +02:00
$filter = array (
2016-05-05 22:22:04 +02:00
'cat_id' => array_flip (( array ) $this -> acl -> get_cats ( Acl :: READ )),
2006-06-27 18:30:53 +02:00
//'accessory_of' => '-1'
2012-12-19 21:23:40 +01:00
'deleted' => null
2006-06-27 18:30:53 +02:00
);
2009-12-09 00:09:45 +01:00
$limit = false ;
if ( $options [ 'start' ] || $options [ 'num_rows' ]) {
$limit = array ( $options [ 'start' ], $options [ 'num_rows' ]);
}
2016-09-28 19:01:10 +02:00
if ( $options [ 'cat_id' ] && in_array ( $options [ 'cat_id' ], $filter [ 'cat_id' ]))
{
$filter [ 'cat_id' ] = $options [ 'cat_id' ];
}
2012-11-14 19:00:02 +01:00
if ( $options [ 'accessory_of' ])
{
$filter [ 'accessory_of' ] = $options [ 'accessory_of' ];
}
2016-07-19 22:09:58 +02:00
$list = array ();
2011-06-10 16:20:47 +02:00
$data = $this -> so -> search ( $criteria , $only_keys , $order_by = 'name' , $extra_cols = '' , $wildcard = '%' , $empty , $op = 'OR' , $limit , $filter );
2008-10-10 12:24:31 +02:00
// maybe we need to check disponibility of the searched resources in the calendar if $pattern ['exec'] contains some extra args
$show_conflict = False ;
2016-07-19 22:09:58 +02:00
if ( $data && $options [ 'exec' ] && $GLOBALS [ 'egw_info' ][ 'preferences' ][ 'calendar' ][ 'defaultresource_sel' ] !== 'resources' )
2005-02-25 12:15:27 +01:00
{
2008-10-10 12:24:31 +02:00
// we'll use a cache for resources info taken from database
static $res_info_cache = array ();
2015-03-12 17:24:55 +01:00
$cal_info = $options [ 'exec' ];
2008-10-10 12:24:31 +02:00
if ( isset ( $cal_info [ 'start' ]) && isset ( $cal_info [ 'duration' ]))
{
//get a calendar objet for reservations
if ( ( ! isset ( $this -> bocal )) || ! ( is_object ( $this -> bocal )))
{
2016-04-29 20:01:17 +02:00
$this -> bocal = new calendar_bo ();
2008-10-10 12:24:31 +02:00
}
2016-05-05 22:22:04 +02:00
$start = new Api\DateTime ( $cal_info [ 'start' ]);
2015-03-12 17:50:47 +01:00
$startarr = getdate ( $start -> format ( 'ts' ));
2016-04-29 20:01:17 +02:00
if ( isset ( $cal_info [ 'whole_day' ]) && $cal_info [ 'whole_day' ]) {
2008-10-10 12:24:31 +02:00
$startarr [ 'hour' ] = $startarr [ 'minute' ] = 0 ;
2016-05-05 22:22:04 +02:00
$start = new Api\DateTime ( $startarr );
2015-03-12 17:50:47 +01:00
$end = $start -> format ( 'ts' ) + 86399 ;
2008-10-10 12:24:31 +02:00
} else {
2015-03-12 17:50:47 +01:00
$start = $start -> format ( 'ts' );
2008-10-10 12:24:31 +02:00
$end = $start + ( $cal_info [ 'duration' ]);
}
// search events matching our timestamps
$resource_list = array ();
2008-10-10 19:04:22 +02:00
foreach ( $data as $num => $resource )
2008-10-10 12:24:31 +02:00
{
// we only need resources id for the search, but with a 'r' prefix
// now we take this loop to store a new resource array indexed with resource id
// and as we work for calendar we use only bookable resources
if (( isset ( $resource [ 'bookable' ])) && ( $resource [ 'bookable' ])){
$res_info_cache [ $resource [ 'res_id' ]] = $resource ;
$resource_list [] = 'r' . $resource [ 'res_id' ];
}
}
$overlapping_events =& $this -> bocal -> search ( array (
'start' => $start ,
'end' => $end ,
'users' => $resource_list ,
'ignore_acl' => true , // otherwise we get only events readable by the user
'enum_groups' => false , // otherwise group-events would not block time
));
// parse theses overlapping events
foreach ( $overlapping_events as $event )
{
if ( $event [ 'non_blocking' ]) continue ; // ignore non_blocking events
if ( isset ( $cal_info [ 'event_id' ]) && $event [ 'id' ] == $cal_info [ 'event_id' ]) {
continue ; //ignore this event, it's the current edited event, no conflict by def
}
// now we are interested only on resources booked by theses events
if ( isset ( $event [ 'participants' ]) && is_array ( $event [ 'participants' ])){
foreach ( $event [ 'participants' ] as $part_key => $part_detail ){
2008-10-10 19:04:22 +02:00
if ( $part_key { 0 } == 'r' )
2008-10-10 12:24:31 +02:00
{ //now we gatta resource here
//need to check the quantity of this resource
$resource_id = substr ( $part_key , 1 );
// if we do not find this resource in our indexed array it's certainly
// because it was unset, non bookable maybe
if ( ! isset ( $res_info_cache [ $resource_id ])) continue ;
// to detect ressources with default to 1 quantity
if ( ! isset ( $res_info_cache [ $resource_id ][ 'useable' ])) {
$res_info_cache [ $resource_id ][ 'useable' ] = 1 ;
}
2012-12-18 17:35:24 +01:00
// now decrement this quantity useable
2016-05-09 22:27:46 +02:00
$quantity = 1 ;
$this -> bocal -> so -> split_status ( $part_detail , $quantity );
2016-06-29 10:50:20 +02:00
2016-05-09 22:27:46 +02:00
$res_info_cache [ $resource_id ][ 'useable' ] -= $quantity ;
2008-10-10 12:24:31 +02:00
}
}
}
}
}
}
if ( isset ( $res_info_cache )) {
2016-04-29 20:01:17 +02:00
$show_conflict = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'calendar' ][ 'defaultresource_sel' ] === 'resources_conflict' ;
2008-10-10 12:24:31 +02:00
// if we have this array indexed on resource id it means non-bookable resource are removed and we are working for calendar
// so we'll loop on this one and not $data
foreach ( $res_info_cache as $id => $resource ) {
//maybe this resource is reserved
if ( ( $resource [ 'useable' ] < 1 ) )
2008-10-10 19:04:22 +02:00
{
2008-10-10 12:24:31 +02:00
if ( $show_conflict ) {
$list [ $id ] = ' (' . lang ( 'conflict' ) . ') ' . $resource [ 'name' ] . ( $resource [ 'short_description' ] ? ', [' . $resource [ 'short_description' ] . ']' : '' );
}
} else {
$list [ $id ] = $resource [ 'name' ] . ( $resource [ 'short_description' ] ? ', [' . $resource [ 'short_description' ] . ']' : '' );
}
}
} else {
// we are not working for the calendar, we loop on the initial $data
2009-02-20 13:52:16 +01:00
if ( is_array ( $data )) {
foreach ( $data as $num => $resource )
{
$id = $resource [ 'res_id' ];
$list [ $id ] = $resource [ 'name' ] . ( $resource [ 'short_description' ] ? ', [' . $resource [ 'short_description' ] . ']' : '' );
}
} else {
error_log ( __METHOD__ . " No Data found for Resource with id " . $resource [ 'res_id' ]);
2008-10-10 12:24:31 +02:00
}
2005-02-25 09:55:37 +01:00
}
2009-12-09 00:09:45 +01:00
$options [ 'total' ] = $this -> so -> total ;
2005-02-25 12:15:27 +01:00
return $list ;
}
2009-09-29 12:02:09 +02:00
2005-06-10 22:40:57 +02:00
/**
2005-06-29 11:30:08 +02:00
* get title for an infolog entry identified by $res_id
2005-06-10 22:40:57 +02:00
*
2009-09-29 12:02:09 +02:00
* @ author Cornelius Weiss < egw @ von - und - zu - weiss . de >
* @ param int | array $resource
2009-10-12 21:16:42 +02:00
* @ return string | boolean string with title , null if resource does not exist or false if no perms to view it
2005-06-10 22:40:57 +02:00
*/
2005-02-25 12:15:27 +01:00
function link_title ( $resource )
{
2006-06-24 18:12:56 +02:00
if ( ! is_array ( $resource ))
2005-02-25 12:15:27 +01:00
{
2009-10-12 21:16:42 +02:00
if ( ! ( $resource = $this -> read ( array ( 'res_id' => $resource )))) return $resource ;
}
2016-05-05 22:22:04 +02:00
elseif ( ! $this -> acl -> is_permitted ( $resource [ 'cat_id' ], Acl :: READ ))
2009-10-12 21:16:42 +02:00
{
return false ;
2005-02-25 12:15:27 +01:00
}
2006-06-24 18:12:56 +02:00
return $resource [ 'name' ] . ( $resource [ 'short_description' ] ? ', [' . $resource [ 'short_description' ] . ']' : '' );
2005-02-25 12:15:27 +01:00
}
2008-04-20 21:37:12 +02:00
2009-09-29 12:02:09 +02:00
/**
* Columns displayed in title ( or required for ACL )
*
*/
const TITLE_COLS = 'res_id,name,short_description,cat_id' ;
/**
* get title for multiple contacts identified by $ids
*
* Is called as hook to participate in the linking .
*
* @ param array $ids array with resource - id ' s
* @ return array with titles , see link_title
*/
function link_titles ( array $ids )
{
$titles = array ();
2009-12-11 15:29:41 +01:00
if (( $resources =& $this -> so -> search ( array ( 'res_id' => $ids ), self :: TITLE_COLS )))
2009-09-29 12:02:09 +02:00
{
foreach ( $resources as $resource )
{
$titles [ $resource [ 'res_id' ]] = $this -> link_title ( $resource );
}
}
2016-05-05 22:22:04 +02:00
// we assume all not returned contacts are not readable for the user (as we report all deleted contacts to Link)
2009-09-29 12:02:09 +02:00
foreach ( $ids as $id )
{
if ( ! isset ( $titles [ $id ]))
{
$titles [ $id ] = false ;
}
}
return $titles ;
}
2005-06-10 22:40:57 +02:00
/**
2015-04-08 16:32:13 +02:00
* saves a pictures in vfs
2005-06-10 22:40:57 +02:00
*
2005-06-13 11:18:06 +02:00
* Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-06-10 22:40:57 +02:00
* @ param array $file array with key => value
* @ param int $resource_id
* @ return mixed string with msg if somthing went wrong ; nothing if all right
2008-04-20 21:37:12 +02:00
*/
2005-02-16 11:26:22 +01:00
function save_picture ( $file , $resouce_id )
{
switch ( $file [ 'type' ])
{
case 'image/gif' :
$src_img = imagecreatefromgif ( $file [ 'tmp_name' ]);
break ;
case 'image/jpeg' :
case 'image/pjpeg' :
$src_img = imagecreatefromjpeg ( $file [ 'tmp_name' ]);
break ;
case 'image/png' :
case 'image/x-png' :
$src_img = imagecreatefrompng ( $file [ 'tmp_name' ]);
break ;
default :
return lang ( 'Picture type is not supported, sorry!' );
}
2008-04-20 21:37:12 +02:00
2008-10-10 19:04:22 +02:00
$tmp_name = tempnam ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ], 'resources-picture' );
2015-04-08 16:32:13 +02:00
imagejpeg ( $src_img , $tmp_name );
2005-02-16 11:26:22 +01:00
imagedestroy ( $src_img );
2008-04-20 21:37:12 +02:00
2016-05-05 22:22:04 +02:00
Link :: attach_file ( 'resources' , $resouce_id , array (
2008-10-10 19:04:22 +02:00
'tmp_name' => $tmp_name ,
'name' => self :: PICTURE_NAME ,
'type' => 'image/jpeg' ,
));
2005-02-16 11:26:22 +01:00
}
2008-04-20 21:37:12 +02:00
2005-06-10 22:40:57 +02:00
/**
* get resource picture either from vfs or from symlink
2005-06-13 11:18:06 +02:00
* Cornelius Weiss < egw @ von - und - zu - weiss . de >
2011-10-14 10:36:12 +02:00
* @ param int | array $resource res - id or whole resource array
2008-10-10 19:04:22 +02:00
* @ param bool $fullsize false = thumb , true = full pic
2005-06-10 22:40:57 +02:00
* @ return string url of picture
*/
2011-10-14 10:36:12 +02:00
function get_picture ( $resource , $fullsize = false )
2005-02-14 13:03:06 +01:00
{
2011-10-14 10:36:12 +02:00
if ( $resource && ! is_array ( $resource )) $resource = $this -> read ( $resource );
switch ( $resource [ 'picture_src' ])
2005-02-03 17:42:20 +01:00
{
2005-02-16 15:50:17 +01:00
case 'own_src' :
2016-05-05 22:22:04 +02:00
$picture = Link :: vfs_path ( 'resources' , $resource [ 'res_id' ], self :: PICTURE_NAME , true ); // vfs path
2008-10-10 19:04:22 +02:00
if ( $fullsize )
{
2016-05-05 22:22:04 +02:00
$picture = Egw :: link ( Vfs :: download_url ( $picture ));
2008-10-10 19:04:22 +02:00
}
2015-04-08 16:32:13 +02:00
else
{
2016-05-05 22:22:04 +02:00
$picture = Egw :: link ( '/api/thumbnail.php' , array (
2015-04-09 12:48:45 +02:00
'path' => $picture
2015-12-08 01:59:01 +01:00
), false );
2015-04-08 16:32:13 +02:00
}
2005-02-16 15:50:17 +01:00
break ;
2011-10-14 10:36:12 +02:00
2005-02-16 15:50:17 +01:00
case 'cat_src' :
2016-05-05 22:22:04 +02:00
$picture = Api\Categories :: id2name ( $resource [ 'cat_id' ], 'data' );
2005-02-17 10:53:46 +01:00
if ( $picture [ 'icon' ])
{
2016-05-09 12:05:57 +02:00
$picture = ! $fullsize ? $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . self :: ICON_PATH . '/' . $picture [ 'icon' ] : self :: ICON_PATH . '/' . $picture [ 'icon' ];
2005-02-17 10:53:46 +01:00
break ;
}
2011-10-14 10:36:12 +02:00
// fall through
2005-02-17 09:04:55 +01:00
case 'gen_src' :
2005-02-16 15:50:17 +01:00
default :
2012-04-24 08:56:43 +02:00
$src = $resource [ 'picture_src' ];
2015-04-08 16:32:13 +02:00
$picture = ! $fullsize ? $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . $this -> resource_icons : $this -> resource_icons ;
2007-05-09 10:07:06 +02:00
$picture .= strpos ( $src , '.' ) !== false ? $src : 'generic.png' ;
2005-02-03 17:42:20 +01:00
}
2005-02-16 15:50:17 +01:00
return $picture ;
2005-02-03 17:42:20 +01:00
}
2008-04-20 21:37:12 +02:00
2005-06-10 22:40:57 +02:00
/**
* removes picture from vfs
*
2005-06-13 11:18:06 +02:00
* Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-06-29 11:30:08 +02:00
* @ param int $res_id id of resource
2005-06-10 22:40:57 +02:00
* @ return bool succsess or not
*/
2005-06-29 11:30:08 +02:00
function remove_picture ( $res_id )
2005-02-17 10:53:46 +01:00
{
2016-05-05 22:22:04 +02:00
if (( $arr = Link :: delete_attached ( 'resources' , $res_id , self :: PICTURE_NAME )) && is_array ( $arr ))
2008-10-10 19:04:22 +02:00
{
return array_shift ( $arr ); // $arr = array($path => (bool)$ok);
}
return false ;
2005-02-17 10:53:46 +01:00
}
2005-02-03 17:42:20 +01:00
2005-06-10 22:40:57 +02:00
/**
* get_genpicturelist
* gets all pictures from 'generic picutres dir' in selectbox style for eTemplate
*
2005-06-13 11:18:06 +02:00
* Cornelius Weiss < egw @ von - und - zu - weiss . de >
2005-06-10 22:40:57 +02:00
* @ return array directory contens in eTemplates selectbox style
*/
2005-02-17 12:17:19 +01:00
function get_genpicturelist ()
{
$icons [ 'generic.png' ] = lang ( 'gernal resource' );
2005-06-10 22:40:57 +02:00
$dir = dir ( EGW_SERVER_ROOT . $this -> resource_icons );
2005-02-17 12:17:19 +01:00
while ( $file = $dir -> read ())
{
if ( preg_match ( '/\\.(png|gif|jpe?g)$/i' , $file ) && $file != 'generic.png' )
{
$icons [ $file ] = substr ( $file , 0 , strpos ( $file , '.' ));
}
}
$dir -> close ();
return $icons ;
}
}