source: contrib/resources/inc/class.bo_resources.inc.php @ 3524

Revision 3524, 16.7 KB checked in by afernandes, 13 years ago (diff)

Ticket #1416 - Disponibilizado módulo de recursos para a comunidade

  • Property svn:executable set to *
Line 
1<?php
2/**
3 * eGroupWare - resources
4 *
5 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
6 * @package resources
7 * @link http://www.egroupware.org
8 * @author Cornelius Weiss <phpgw@von-und-zu-weiss.de>
9 * @author Lukas Weiss <wnz_gh05t@users.sourceforge.net>
10 * @version $Id: class.bo_resources.inc.php 24207 2007-06-25 16:44:20Z ralfbecker $
11 */
12
13
14/**
15 * General business object for resources
16 *
17 * @package resources
18 */
19class bo_resources
20{
21        var $vfs_basedir = '/resources/';
22        var $pictures_dir = '/resources/pictures/';
23        var $thumbs_dir = '/resources/pictures/thumbs/';
24        var $resource_icons = '/resources/templates/default/images/resource_icons/';
25        var $debug = 0;
26        /**
27         * Instance of resources so object
28         *
29         * @var so_resources
30         */
31        var $so;
32       
33        function bo_resources()
34        {
35                $this->so =& CreateObject('resources.so_resources');
36                $this->acl =& CreateObject('resources.bo_acl');
37                $this->cats = $this->acl->phpgw_cats;
38                $this->vfs =& CreateObject('phpgwapi.vfs');
39                $this->link =& CreateObject('phpgwapi.bolink');
40                $this->conf =& CreateObject('phpgwapi.config');
41                $this->conf->read_repository();
42               
43                $this->cal_right_transform = array(     
44                        phpgw_ACL_CALREAD       => phpgw_ACL_READ,
45                        phpgw_ACL_DIRECT_BOOKING        => phpgw_ACL_READ | phpgw_ACL_ADD | phpgw_ACL_EDIT | phpgw_ACL_DELETE,
46                        phpgw_ACL_CAT_ADMIN     => phpgw_ACL_READ | phpgw_ACL_ADD | phpgw_ACL_EDIT | phpgw_ACL_DELETE,
47                );
48        }
49
50        /**
51         * get rows for resources list
52         *
53         * Cornelius Weiss <phpgw@von-und-zu-weiss.de>
54         */
55        function get_rows($query,&$rows,&$readonlys)
56        {
57                if ($this->debug) _debug_array($query);
58                $query['search'] = $query['search'] ? $query['search'] : '*';
59                $criteria = array('name' => $query['search'], 'short_description' => $query['search'], 'inventory_number' => $query['search']);
60                $read_onlys = 'res_id,name,short_description,quantity,useable,bookable,buyable,cat_id,location,storage_info';
61               
62                $accessory_of = $query['view_accs_of'] ? $query['view_accs_of'] : -1;
63                $filter = array('accessory_of' => $accessory_of);
64                if ($query['filter'])
65                {
66                        $filter = $filter + array('cat_id' => $query['filter']);
67                }
68                else
69                {
70                        $readcats = array_flip((array)$this->acl->get_cats(phpgw_ACL_READ));
71                        if($readcats) $filter = $filter + array('cat_id' => $readcats);
72                }
73                if($query['show_bookable']) $filter = $filter + array('bookable' => true);
74                $order_by = $query['order'] ? $query['order'].' '. $query['sort'] : '';
75                $start = (int)$query['start'];
76               
77                $rows = $this->so->search($criteria,$read_onlys,$order_by,'','%',$empty=False,$op='OR',$start,$filter,$join='',$need_full_no_count=false);
78                $nr = $this->so->total;
79               
80                // we are called to serve bookable resources (e.g. calendar-dialog)
81                if($query['show_bookable'])
82                {
83                        // This is somehow ugly, i know...
84                        foreach((array)$rows as $num => $resource)
85                        {
86                                $rows[$num]['default_qty'] = 1;
87                        }
88                        // we don't need all the following testing
89                        return $nr;
90                }
91               
92                foreach((array)$rows as $num => $resource)
93                {
94                        if (!$this->acl->is_permitted($resource['cat_id'],phpgw_ACL_EDIT))
95                        {
96                                $readonlys["edit[$resource[res_id]]"] = true;
97                        }
98                        if (!$this->acl->is_permitted($resource['cat_id'],phpgw_ACL_DELETE))
99                        {
100                                $readonlys["delete[$resource[res_id]]"] = true;
101                        }
102                        if ((!$this->acl->is_permitted($resource['cat_id'],phpgw_ACL_ADD)) || $accessory_of != -1)
103                        {
104                                $readonlys["new_acc[$resource[res_id]]"] = true;
105                        }
106                        if (!$resource['bookable'])
107                        {
108                                $readonlys["bookable[$resource[res_id]]"] = true;
109                                $readonlys["calendar[$resource[res_id]]"] = true;
110                        }
111                        if(!$this->acl->is_permitted($resource['cat_id'],phpgw_ACL_CALREAD))
112                        {
113                                $readonlys["calendar[$resource[res_id]]"] = true;
114                        }
115                        if (!$resource['buyable'])
116                        {
117                                $readonlys["buyable[$resource[res_id]]"] = true;
118                        }
119                        $readonlys["view_acc[$resource[res_id]]"] = true;
120                        $links = $this->link->get_links('resources',$resource['res_id']);
121                        if(count($links) != 0 && $accessory_of == -1)
122                        {
123                                foreach ($links as $link_num => $link)
124                                {
125                                        if($link['app'] == 'resources')
126                                        {
127                                                if($this->so->get_value('accessory_of',$link['res_id']) != -1)
128                                                {
129                                                        $readonlys["view_acc[$resource[res_id]]"] = false;
130                                                }
131                                        }
132                                }
133                        }
134                        $rows[$num]['picture_thumb'] = $this->get_picture($resource['res_id']);
135                        $rows[$num]['admin'] = $this->acl->get_cat_admin($resource['cat_id']);
136                }
137                return $nr;
138        }
139
140        /**
141         * reads a resource exept binary datas
142         *
143         * Cornelius Weiss <phpgw@von-und-zu-weiss.de>
144         * @param int $res_id resource id
145         * @return array with key => value or false if not found or allowed
146         */
147        function read($res_id)
148        {
149                if(!$this->acl->is_permitted($this->so->get_value('cat_id',$res_id),phpgw_ACL_READ))
150                {
151                        echo lang('You are not permitted to get information about this resource!') . '<br>';
152                        echo lang('Notify your administrator to correct this situation') . '<br>';
153                        return false;
154                }
155                return $this->so->read(array('res_id' => $res_id));
156        }
157       
158        /**
159         * saves a resource. pictures are saved in vfs
160         *
161         * Cornelius Weiss <phpgw@von-und-zu-weiss.de>
162         * @param array $resource array with key => value of all needed datas
163         * @return string msg if somthing went wrong; nothing if all right
164         */
165        function save($resource)
166        {
167                if(!$this->acl->is_permitted($resource['cat_id'],phpgw_ACL_EDIT))
168                {
169                        return lang('You are not permitted to edit this reource!');
170                }
171               
172                // we need an id to save pictures and make links...
173                if(!$resource['res_id'])
174                {
175                        $resource['res_id'] = $this->so->save($resource);
176                }
177
178                switch ($resource['picture_src'])
179                {
180                        case 'own_src':
181                                $vfs_data = array('string' => $this->pictures_dir.$resource['res_id'].'.jpg','relatives' => array(RELATIVE_ROOT));
182                                if($resource['own_file']['size'] > 0)
183                                {
184                                        $msg = $this->save_picture($resource['own_file'],$resource['res_id']);
185                                        break;
186                                }
187                                elseif($this->vfs->file_exists($vfs_data))
188                                {
189                                        break;
190                                }
191                                $resource['picture_src'] = 'cat_src';
192                        case 'cat_src':
193                                break;
194                        case 'gen_src':
195                                $resource['picture_src'] = $resource['gen_src_list'];
196                                break;
197                        default:
198                                if($resource['own_file']['size'] > 0)
199                                {
200                                        $resource['picture_src'] = 'own_src';
201                                        $msg = $this->save_picture($resource['own_file'],$resource['res_id']);
202                                }
203                                else
204                                {
205                                        $resource['picture_src'] = 'cat_src';
206                                }
207                }
208                // somthing went wrong on saving own picture
209                if($msg)
210                {
211                        return $msg;
212                }
213               
214                // delete old pictures
215                if($resource['picture_src'] != 'own_src')
216                {
217                        $this->remove_picture($resource['res_id']);
218                }
219
220                // save links
221                if(is_array($resource['link_to']['to_id']))
222                {
223                        $this->link->link('resources',$resource['res_id'],$resource['link_to']['to_id']);
224                }
225                if($resource['accessory_of'] != -1)
226                {
227                        $this->link->link('resources',$resource['res_id'],'resources',$resource['accessory_of']);
228                }
229               
230                if(!empty($resource['res_id']) && $this->so->get_value("cat_id",$resource['res_id']) != $resource['cat_id'] && $resource['accessory_of'] == -1)
231                {
232                        $accessories = $this->get_acc_list($resource['res_id']);
233                        foreach($accessories as $accessory => $name)
234                        {
235                                $acc = $this->so->read($accessory);
236                                $acc['cat_id'] = $resource['cat_id'];
237                                $this->so->data = $acc;
238                                $this->so->save();
239                        }
240                }
241               
242                return $this->so->save($resource) ? false : lang('Something went wrong by saving resource');
243        }
244
245        /**
246         * deletes resource including pictures and links
247         *
248         * @author Lukas Weiss <wnz_gh05t@users.sourceforge.net>
249         * @param int $res_id id of resource
250         */
251        function delete($res_id)
252        {
253                if(!$this->acl->is_permitted($this->so->get_value('cat_id',$res_id),phpgw_ACL_DELETE))
254                {
255                        return lang('You are not permitted to delete this reource!');
256                }
257               
258                if ($this->so->delete(array('res_id'=>$res_id)))
259                {
260                        $this->remove_picture($res_id);
261                        $this->link->unlink(0,'resources',$res_id);
262                        // delete the resource from the calendar
263                        ExecMethod('calendar.socal.change_delete_user','r'.$res_id);
264                        return false;
265                }
266                return lang('Something went wrong by deleting resource');
267        }
268       
269        /**
270         * gets list of accessories for resource
271         *
272         * Cornelius Weiss <phpgw@von-und-zu-weiss.de>
273         * @param int $res_id id of resource
274         * @return array
275         */
276        function get_acc_list($res_id)
277        {
278                if($res_id < 1){return;}
279                $data = $this->so->search('','res_id,name','','','','','',$start,array('accessory_of' => $res_id),'',$need_full_no_count=true);
280                foreach($data as $num => $resource)
281                {
282                        $acc_list[$resource['res_id']] = $resource['name'];
283                }
284                return $acc_list;
285        }
286       
287        /**
288         * returns info about resource for calender
289         * @author Cornelius Weiss<phpgw@von-und-zu-weiss.de>
290         * @param int/array $res_id single id or array $num => $res_id
291         * @return array
292         */
293        function get_calendar_info($res_id)
294        {
295                //echo "<p>bo_resources::get_calendar_info(".print_r($res_id,true).")</p>\n";
296                if(!is_array($res_id) && $res_id < 1) return;
297
298                $data = $this->so->search(array('res_id' => $res_id),'res_id,cat_id,name,useable');
299               
300                foreach($data as $num => $resource)
301                {
302                        $data[$num]['rights'] = false;
303                        foreach($this->cal_right_transform as $res_right => $cal_right)
304                        {
305                                if($this->acl->is_permitted($resource['cat_id'],$res_right))
306                                {
307                                        $data[$num]['rights'] = $cal_right;
308                                }
309                        }
310                        $data[$num]['responsible'] = $this->acl->get_cat_admin($resource['cat_id']);
311                }
312                return $data;
313        }
314       
315        /**
316         * returns status for a new calendar entry depending on resources ACL
317         * @author Cornelius Weiss <phpgw@von-und-zu-weiss.de>
318         * @param int $res_id single id
319         * @return array
320         */
321        function get_calendar_new_status($res_id)
322        {
323                $data = $this->so->search(array('res_id' => $res_id),'res_id,cat_id,bookable');
324                if($data[0]['bookable'] == 0) return 'x';
325                return $this->acl->is_permitted($data[0]['cat_id'],phpgw_ACL_DIRECT_BOOKING) ? A : U;
326        }
327       
328        /**
329         * @author Cornelius Weiss <phpgw@von-und-zu-weiss.de>
330         * query infolog for entries matching $pattern
331         *
332         */
333        function link_query( $pattern )
334        {
335                $criteria = array('name' => $pattern, 'short_description' => $pattern);
336                $only_keys = 'res_id,name,short_description';
337                $filter = array(
338                        'cat_id' => array_flip((array)$this->acl->get_cats(phpgw_ACL_READ)),
339                        //'accessory_of' => '-1'
340                );
341                $data = $this->so->search($criteria,$only_keys,$order_by='',$extra_cols='',$wildcard='%',$empty,$op='OR',false,$filter);
342                foreach($data as $num => $resource)
343                {
344                        $list[$resource['res_id']] = $resource['name']. ($resource['short_description'] ? ', ['.$resource['short_description'].']':'');
345                }
346                return $list;
347        }
348               
349        /**
350         * @author Cornelius Weiss <phpgw@von-und-zu-weiss.de>
351         * get title for an infolog entry identified by $res_id
352         *
353         * @return string/boolean string with title, null if resource does not exist or false if no perms to view it
354         */
355        function link_title( $resource )
356        {
357                if (!is_array($resource))
358                {
359                        if (!($resource  = $this->so->read(array('res_id' => $resource)))) return null;
360                }
361                if(!$this->acl->is_permitted($resource['cat_id'],phpgw_ACL_READ)) return false;
362
363                return $resource['name']. ($resource['short_description'] ? ', ['.$resource['short_description'].']':'');
364        }
365       
366        /**
367         * resizes and saves an pictures in vfs
368         *
369         * Cornelius Weiss <phpgw@von-und-zu-weiss.de>
370         * @param array $file array with key => value
371         * @param int $resource_id
372         * @return mixed string with msg if somthing went wrong; nothing if all right
373         * TODO make thumb an picture sizes choosable by preferences
374         */     
375        function save_picture($file,$resouce_id)
376        {
377                // test upload dir
378                $vfs_data = array('string'=>$this->vfs_basedir,'relatives'=>array(RELATIVE_ROOT));
379                if (!($this->vfs->file_exists($vfs_data)))
380                {
381                        $this->vfs->override_acl = 1;
382                        $this->vfs->mkdir($vfs_data);
383                        $vfs_data['string'] = $this->pictures_dir;
384                        $this->vfs->mkdir($vfs_data);
385                        $vfs_data['string'] = $this->thumbs_dir;
386                        $this->vfs->mkdir($vfs_data);
387                        $this->vfs->override_acl = 0;
388                }
389               
390                switch($file['type'])
391                {
392                        case 'image/gif':
393                                $src_img = imagecreatefromgif($file['tmp_name']);
394                                break;
395                        case 'image/jpeg':
396                        case 'image/pjpeg':
397                                $src_img = imagecreatefromjpeg($file['tmp_name']);
398                                break;
399                        case 'image/png':
400                        case 'image/x-png':
401                                $src_img = imagecreatefrompng($file['tmp_name']);
402                                break;
403                        default:
404                                return lang('Picture type is not supported, sorry!');
405                }
406               
407                $src_img_size = getimagesize($file['tmp_name']);
408                $dst_img_size = array( 0 => 320, 1 => 240);
409                $thumb_size = array( 0 => 64, 1 => 48);
410               
411                $tmp_dir = $GLOBALS['phpgw_info']['server']['temp_dir'].'/';
412                if($src_img_size[0] > 64 || $src_img_size[1] > 48)
413                {
414                        $f = $thumb_size[0] / $src_img_size[0];
415                        $f = $thumb_size[1] / $src_img_size[1] < $f ? $thumb_size[1] / $src_img_size[1] : $f;
416                        $dst_img = imagecreatetruecolor($src_img_size[0] * $f, $src_img_size[1] * $f);
417                        imagecopyresized($dst_img,$src_img,0,0,0,0,$src_img_size[0] * $f,$src_img_size[1] * $f,$src_img_size[0],$src_img_size[1]);
418                        imagejpeg($dst_img,$tmp_dir.$resouce_id.'.thumb.jpg');
419                        if($src_img_size[0] > $dst_img_size[0] || $src_img_size[1] > $dst_img_size[1])
420                        {
421                                $f = $dst_img_size[0] / $src_img_size[0];
422                                $f = $dst_img_size[1] / $src_img_size[1] < $f ? $dst_img_size[1] / $src_img_size[1] : $f;
423                                $dst_img = imagecreatetruecolor($src_img_size[0] * $f, $src_img_size[1] * $f);
424                                imagecopyresized($dst_img,$src_img,0,0,0,0,$src_img_size[0] * $f,$src_img_size[1] * $f,$src_img_size[0],$src_img_size[1]);
425                                imagejpeg($dst_img,$tmp_dir.$resouce_id.'.jpg');
426                        }
427                        else
428                        {
429                                imagejpeg($src_img,$tmp_dir.$resouce_id.'.jpg');
430                        }
431                        imagedestroy($dst_img);
432                }
433                else
434                {
435                                imagejpeg($src_img,$tmp_dir.$resouce_id.'.jpg');
436                                imagejpeg($src_img,$tmp_dir.$resouce_id.'.thumb.jpg');
437                }
438                imagedestroy($src_img);
439                       
440                $this->vfs->override_acl = 1;
441                $this->vfs->cp(array(
442                        'from' => $tmp_dir.$resouce_id.'.jpg',
443                        'to'   => $this->pictures_dir.$resouce_id.'.jpg',
444                        'relatives' => array(RELATIVE_NONE|VFS_REAL,RELATIVE_ROOT)
445                ));
446                $this->vfs->set_attributes(array(
447                        'string' => $this->pictures_dir.$resouce_id.'.jpg',
448                        'relatives' => array (RELATIVE_ROOT),
449                        'attributes' => array (
450                                'mime_type' => 'image/jpeg',
451                                'comment' => 'picture of resource no.'.$resouce_id,
452                                'app' => $GLOBALS['phpgw_info']['flags']['currentapp']
453                )));
454                $this->vfs->cp(array(
455                        'from' => $tmp_dir.$resouce_id.'.thumb.jpg',
456                        'to'   => $this->thumbs_dir.$resouce_id.'.jpg',
457                        'relatives' => array(RELATIVE_NONE|VFS_REAL,RELATIVE_ROOT)
458                        ));
459                $this->vfs->set_attributes(array(
460                        'string' => $this->thumbs_dir.$resouce_id.'.jpg',
461                        'relatives' => array (RELATIVE_ROOT),
462                        'attributes' => array (
463                                'mime_type' => 'image/jpeg',
464                                'comment' => 'thumbnail of resource no.'.$resouce_id,
465                                'app' => $GLOBALS['phpgw_info']['flags']['currentapp']
466                )));
467                $this->vfs->override_acl = 0;
468                return;
469        }
470       
471        /**
472         * get resource picture either from vfs or from symlink
473         * Cornelius Weiss <phpgw@von-und-zu-weiss.de>
474         * @param int $res_id id of resource
475         * @param bool $size false = thumb, true = full pic
476         * @return string url of picture
477         */
478        function get_picture($res_id=0,$size=false)
479        {
480                if ($res_id > 0)
481                {
482                        $src = $this->so->get_value('picture_src',$res_id);
483                }
484                switch($src)
485                {
486                        case 'own_src':
487                                $picture = $this->conf->config_data['dont_use_vfs'] ? $GLOBALS['phpgw_info']['server']['webserver_url'] : 'vfs:';
488                                $picture .= $size ? $this->pictures_dir.$res_id.'.jpg' : $this->thumbs_dir.$res_id.'.jpg';
489                                break;
490                        case 'cat_src':
491                                list($picture) = $this->cats->return_single($this->so->get_value('cat_id',$res_id));
492                                $picture = unserialize($picture['data']);
493                                if($picture['icon'])
494                                {
495                                        $picture = $GLOBALS['phpgw_info']['server']['webserver_url'].'/phpgwapi/images/'.$picture['icon'];
496                                        break;
497                                }
498                        case 'gen_src':
499                        default :
500                                $picture = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->resource_icons;
501                                $picture .= strpos($src,'.') !== false ? $src : 'generic.png';
502                }
503                return $picture;
504        }
505       
506        /**
507         * remove_picture
508         * removes picture from vfs
509         *
510         * Cornelius Weiss <phpgw@von-und-zu-weiss.de>
511         * @param int $res_id id of resource
512         * @return bool succsess or not
513         */
514        function remove_picture($res_id)
515        {
516                $vfs_data = array('string' => $this->pictures_dir.$res_id.'.jpg','relatives' => array(RELATIVE_ROOT));
517                $this->vfs->override_acl = 1;
518                if($this->vfs->file_exists($vfs_data))
519                {
520                        $this->vfs->rm($vfs_data);
521                        $vfs_data['string'] = $this->thumbs_dir.$res_id.'.jpg';
522                        $this->vfs->rm($vfs_data);
523                }
524                $this->vfs->override_acl = 0;
525        }
526
527        /**
528         * get_genpicturelist
529         * gets all pictures from 'generic picutres dir' in selectbox style for eTemplate
530         *
531         * Cornelius Weiss <phpgw@von-und-zu-weiss.de>
532         * @return array directory contens in eTemplates selectbox style
533         */
534        function get_genpicturelist()
535        {
536                $icons['generic.png'] = lang('gernal resource');
537                $dir = dir(phpgw_SERVER_ROOT.$this->resource_icons);
538                while($file = $dir->read())
539                {
540                        if (preg_match('/\\.(png|gif|jpe?g)$/i',$file) && $file != 'generic.png')
541                        {
542                                $icons[$file] = substr($file,0,strpos($file,'.'));
543                        }
544                }
545                $dir->close();
546                return $icons;
547        }
548}
Note: See TracBrowser for help on using the repository browser.