[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/resources/inc/ -> class.bo_resources.inc.php (source)

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


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7