CVSROOT: /sources/phpgroupware Module name: property Changes by: Sigurd Nes <sigurdne> 07/10/04 14:01:19
Added files: inc/cron : import_from_scanner.php Log message: Start implementing integration with cannon scanner for adding docs to tickets and locations CVSWeb URLs: http://cvs.savannah.gnu.org/viewcvs/property/inc/cron/import_from_scanner.php?cvsroot=phpgroupware&rev=1.1 Patches: Index: import_from_scanner.php =================================================================== RCS file: import_from_scanner.php diff -N import_from_scanner.php --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ import_from_scanner.php 4 Oct 2007 14:01:19 -0000 1.1 @@ -0,0 +1,523 @@ +<?php + /** + * phpGroupWare - property: a Facilities Management System. + * + * @author Sigurd Nes <[EMAIL PROTECTED]> + * @copyright Copyright (C) 2003,2004,2005,2006,2007 Free Software Foundation, Inc. http://www.fsf.org/ + * This file is part of phpGroupWare. + * + * phpGroupWare is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * phpGroupWare is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with phpGroupWare; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * @license http://www.gnu.org/licenses/gpl.html GNU General Public License + * @internal Development of this application was funded by http://www.bergen.kommune.no/bbb_/ekstern/ + * @package property + * @subpackage custom + * @version $Id: import_from_scanner.php,v 1.1 2007/10/04 14:01:19 sigurdne Exp $ + */ + + /** + * Description + * @package property + */ + + class import_from_scanner + { + /* In Admin->Property->Async servises: + * Name: property.custom_functions.index + * Data: function=import_from_scanner,dir=C:/path/to/drawings + */ + + var $dir = '/home/sn5607/test'; + var $suffix = 'pdf'; + var $meta_suffix = 'csv'; + var $delimiter = ','; + var $bypass = False; // bypass location check (only for debugging) + var $default_user_id = 6; + var $default_user_last_name = 'Aspevik'; + var $function_name = 'import_from_scanner'; + var $header = array('type','descr','target','user'); + + function import_from_scanner() + { + $this->currentapp = $GLOBALS['phpgw_info']['flags']['currentapp']; + $this->bocommon = CreateObject($this->currentapp.'.bocommon'); + $this->vfs = CreateObject('phpgwapi.vfs'); + $this->rootdir = $this->vfs->basedir; + $this->fakebase = $this->vfs->fakebase; + $this->db = & $GLOBALS['phpgw']->db; + + } + + function pre_run($data='') + { + $cron = False; + $dry_run = False; + + if(isset($data['enabled']) && $data['enabled']==1) + { + $confirm = True; + $execute = True; + $cron = True; + if($data['suffix']) + { + $this->suffix = $data['suffix']; + } + if($data['dir']) + { + $this->dir = $data['dir']; + } + } + else + { + $confirm = get_var('confirm',array('POST')); + $execute = get_var('execute',array('GET')); + if(get_var('dir',array('GET'))) + { + $this->dir = urldecode (get_var('dir',array('GET'))); + } + if(get_var('suffix',array('GET'))) + { + $this->suffix = get_var('suffix',array('GET')); + } + } + + if(!$execute) + { + $dry_run=True; + } + + if ($confirm) + { + $this->execute($dry_run,$cron); + } + else + { + $this->confirm($execute=False); + } + } + + function confirm($execute='',$done='') + { + $link_data = array + ( + 'menuaction' => $this->currentapp.'.custom_functions.index', + 'function' => $this->function_name, + 'execute' => $execute, + 'dir' => $this->dir, + 'suffix' => $this->suffix, + ); + + if(!$done) + { + if(!$execute) + { + $lang_confirm_msg = 'Ga videre for aa se hva som blir lagt til'; + } + else + { + $lang_confirm_msg = lang('do you want to perform this action'); + } + } + $lang_yes = lang('yes'); + + $GLOBALS['phpgw']->xslttpl->add_file(array('confirm_custom')); + + $msgbox_data = $this->bocommon->msgbox_data($this->receipt); + + $data = array + ( + 'msgbox_data' => $GLOBALS['phpgw']->common->msgbox($msgbox_data), + 'done_action' => $GLOBALS['phpgw']->link('/admin/index.php'), + 'run_action' => $GLOBALS['phpgw']->link('/index.php',$link_data), + 'message' => $this->receipt['message'], + 'lang_confirm_msg' => $lang_confirm_msg, + 'lang_yes' => $lang_yes, + 'lang_yes_statustext' => 'Organisere tegninger i register og pa disk', + 'lang_no_statustext' => 'tilbake', + 'lang_no' => lang('no'), + 'lang_done' => 'Avbryt', + 'lang_done_statustext' => 'tilbake' + ); + + $appname = 'import from scanner'; + $function_msg = 'import files from scanner-drop-catalog'; + $GLOBALS['phpgw_info']['flags']['app_header'] = lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg; + $GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('confirm' => $data)); + $GLOBALS['phpgw']->xslttpl->pp(); + } + + function execute($dry_run='',$cron='') + { + + $file_list = $this->get_files(); + + if($dry_run) + { + _debug_array($file_list); + $this->confirm($execute=True); + + } + else + { + if (isset($file_list) && is_array($file_list)) + { + $this->botts = CreateObject($this->currentapp.'.botts'); + foreach($file_list as $file_entry) + { + $file_entry['user_id'] = $this->get_user_id($file_entry['user']); +_debug_array($file_entry);die(); + if($file_entry['type'] == 'Reklamasjon') + { + + if($file_entry['target'] && $this->find_ticket($file_entry['target'])) + { + + } + else + { + $values['details'] = $file_entry['descr']; + $values['assignedto']= (isset($GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['assigntodefault'])?$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['assigntodefault']:''); + $values['group_id']= (isset($GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['groupdefault'])?$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['groupdefault']:''); + $values['cat_id'] = (isset($GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['tts_category'])?$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['tts_category']:''); + + if($values['location_code'] = $this->get_location_code($file_entry['target'])) + { + $receipt = $this->botts->add($values); + + $to_file = $this->fakebase. SEP . 'fmticket' . SEP . $receipt['id'] . SEP . $file_entry['file_name'] . $this->suffix; + + if($this->botts->vfs->file_exists(array( + 'string' => $to_file, + 'relatives' => Array(RELATIVE_NONE) + ))) + { + $receipt['error'][]=array('msg'=>lang('This file already exists !')); + } + else + { + $this->botts->create_document_dir($receipt['id']); + $this->botts->vfs->override_acl = 1; + + if(!$this->botts->vfs->cp (array ( + 'from' => $this->dir . SEP . $file_entry['file_name'] . $this->suffix, + 'to' => $to_file, + 'relatives' => array (RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)))) + { + $receipt['error'][]=array('msg'=>lang('Failed to upload file !')); + } + $this->botts->vfs->override_acl = 0; + } + } + } + } + } + + $loc1_list = array_keys($loc1_list); + + for ($i=0;$i<count($loc1_list);$i++) + { + // $this->create_loc1_dir($loc1_list[$i]); + } + + for ($i=0;$i<count($file_list);$i++) + { + // $this->copy_files($file_list[$i]); + } + } + +die(); + if(!$cron) + { + $this->confirm($execute=false,$done=True); + } + + $msgbox_data = $this->bocommon->msgbox_data($this->receipt); + + $insert_values= array( + $cron, + date($this->bocommon->datetimeformat), + $this->function_name, + implode(',',(array_keys($msgbox_data))) + ); + + $insert_values = $this->bocommon->validate_db_insert($insert_values); + + $sql = "INSERT INTO fm_cron_log (cron,cron_date,process,message) " + . "VALUES ($insert_values)"; + $this->db->query($sql,__LINE__,__FILE__); + } + } + + function get_files() + { + $dir_handle = @opendir($this->dir); + + $i=0; $myfilearray = ''; + while ($file = @readdir($dir_handle)) + { + if ((strtolower(substr($file, -3, 3)) == $this->meta_suffix) && is_file($this->dir . SEP . $file) ) + { + $myfilearray[$i] = $file; + $i++; + } + } + + @closedir($dir_handle); + @sort($myfilearray); + + for ($i=0;$i<count($myfilearray);$i++) + { + $fname = $myfilearray[$i]; + $file_list[$i]['file_name'] = substr($fname,0, strlen($fname)-strlen($this->meta_suffix)); + + $fp = fopen($this->dir . SEP . $fname,'rb'); + + $row = 1; + while ($data = fgetcsv($fp,8000,$this->delimiter)) + { + if ($row ==2) // Ther first row is headerinfo + { + $num = count($this->header); + + $this->currentrecord = array(); + for ($c=0; $c<$num; $c++ ) + { + $value=$data[$c]; + $name=$this->header[$c]; + + $file_list[$i][$name] = $value; + } + } + $row++; + } + fclose($fp); + } + return $file_list; + } + + function get_type($filename='') + { + $drawing_type=array( + 'p' => 'plan', + 'f' => 'fasade', + 's' => 'snitt' + ); + + for ($i=10;$i<strlen($filename);$i++) + { + $type = $drawing_type[strtolower(substr($filename,$i,1))]; + if($type) + { + return $type; + } + } + } + + function find_ticket($id='') + { + $sql = "SELECT count(*) FROM fm_tts_tickets WHERE id='$id'"; + +//_debug_array($sql); + $this->db->query($sql,__LINE__,__FILE__); + $this->db->next_record(); + if($this->db->f(0)) + { + return True; + } + } + + function get_user_id($account_lastname = '') + { + $account_lastname = $account_lastname?$account_lastname:$this->default_user_last_name; + $sql = "SELECT account_id FROM phpgw_accounts WHERE account_lastname='$account_lastname'"; + +//_debug_array($sql); + $this->db->query($sql,__LINE__,__FILE__); + $this->db->next_record(); + if($this->db->f('account_id')) + { + return $this->db->f('account_id'); + } + else + { + return $this->default_user_id; + } + } + + function get_location_code($target = '') + { + if(strpos($target,'.')) + { + $location = explode('.', $target); + $sql = "SELECT_ location_code FROM fm_location4 WHERE loc1= '{$location[0]}' AND loc4= '{$location[1]}'"; + } + else + { + $location = explode('-', $target); + $type = count($location); + $sql = "SELECT location_code FROM fm_location{$type} WHERE location_code = '{$target}'"; + } + + $this->db->query($sql,__LINE__,__FILE__); + $this->db->next_record(); + return $this->db->f('location_code'); + } + + + function check_building($loc1='',$loc2='') + { + $sql = "SELECT count(*) FROM fm_location2 WHERE loc1= '$loc1' AND loc2= '$loc2'"; + +//_debug_array($sql); + $this->db->query($sql,__LINE__,__FILE__); + $this->db->next_record(); + if($this->db->f(0)) + { + return True; + } + + if($this->bypass) + { + return True; + } + + } + + function create_loc1_dir($loc1='') + { + if(!$this->vfs->file_exists(array( + 'string' => $this->fakebase . SEP . 'document' . SEP . $loc1, + 'relatives' => Array(RELATIVE_NONE) + ))) + { + $this->vfs->override_acl = 1; + + if(!$this->vfs->mkdir (array( + 'string' => $this->fakebase. SEP . 'document' . SEP . $loc1, + 'relatives' => array( + RELATIVE_NONE + ) + ))) + { + $this->receipt['error'][]=array('msg'=>lang('failed to create directory') . ' :'. $this->fakebase. SEP . 'document' . SEP . $loc1); + } + else + { + $this->receipt['message'][]=array('msg'=>lang('directory created') . ' :'. $this->fakebase. SEP . 'document' . SEP . $loc1); + } + $this->vfs->override_acl = 0; + } + +// return $this->receipt; + } + + function copy_files($values) + { + $to_file = $this->fakebase . SEP . 'document' . SEP . $values['loc1'] . SEP . $values['file_name']; + $from_file = $this->dir . SEP . $values['file_name']; + $this->vfs->override_acl = 1; + + +//_debug_array($to_file); + if($this->vfs->file_exists(array( + 'string' => $to_file, + 'relatives' => Array(RELATIVE_NONE) + ))) + { + $this->receipt['error'][]=array('msg'=>lang('File %1 already exists!',$values['file_name'])); + } + else + { + + if(!$this->vfs->cp (array ( + 'from' => $from_file, + 'to' => $to_file, + 'relatives' => array (RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)))) + { + $this->receipt['error'][]=array('msg'=>lang('Failed to copy file !') . $values['file_name']); + } + else + { + $address = $this->get_address($values['loc1'],$values['loc2'],$values['loc3']); + + switch($values['type']) + { + case 'plan': + $values['title'] = $this->db->db_addslashes($values['branch'] . ', ' .$values['type'] . ', etasje: ' . $values['etasje']); + break; + case 'snitt': + $values['title'] = $this->db->db_addslashes($values['branch'] . ', ' . $values['type'] . ' nr: ' . $values['nr']); + break; + case 'fasade': + $values['title'] = $this->db->db_addslashes($values['branch'] . ', ' . $values['type'] . ' nr: ' . $values['nr'] . ' retning: ' . $values['direction']); + break; + } + + $insert_values= array( + $values['file_name'], + $values['title'], + 'public', + $values['category_id'], + time(), + $values['values_date'], + $values['version'], + $values['coordinator'], + $values['status'], + $values['location_code'], + $address, + $values['branch_id'], + $values['vendor_id'], + $this->account, + $values['loc1'], + $values['loc2'], + $values['loc3'], + ); + + $insert_values = $this->bocommon->validate_db_insert($insert_values); + + $sql = "INSERT INTO fm_document (document_name,title,access,category,entry_date,document_date,version,coordinator,status," + . "location_code,address,branch_id,vendor_id,user_id,loc1,loc2,loc3) " + . "VALUES ($insert_values)"; + + $this->db->query($sql,__LINE__,__FILE__); + + unlink($from_file); + + $this->receipt['message'][]=array('msg'=>lang('File %1 copied!',$values['file_name'])); + $this->receipt['message'][]=array('msg'=>lang('File %1 deleted!',$from_file)); + } + } + + $this->vfs->override_acl = 0; +// return $this->receipt; + } + + function get_address($loc1='',$loc2='',$loc3='') + { + if ($loc3) + { + $sql = "SELECT loc3_name as address FROM fm_location3 WHERE loc1='$loc1' AND loc2='$loc2' AND loc3='$loc3'"; + } + else + { + $sql = "SELECT loc2_name as address FROM fm_location2 WHERE loc1='$loc1' AND loc2='$loc2'"; + } + + $this->db->query($sql,__LINE__,__FILE__); + $this->db->next_record(); + return $this->db->f('address'); + } + } +?> _______________________________________________ phpGroupWare-cvs mailing list phpGroupWare-cvs@gnu.org http://lists.gnu.org/mailman/listinfo/phpgroupware-cvs