'skanman', //stockés dans skanman/*.*
'access' => 'skanman', // crud sur core,
'size' => '10000000', // taille max
'storage' => 'skanman/tosend/logo.{png,jpg,jpeg,gif,png}' //chemin complet vers le fichier stocké
), $response);
});
/** SCANCATEGORY / CATéGORIE DE SCAN **/
//Récuperation d'une liste de catégorie de scan
Action::register('skanman_scan_category_search',function(&$response){
global $_;
User::check_access('skanman','read');
require_once(__DIR__.SLASH.'ScanCategory.class.php');
$response['rows'] = array();
foreach(ScanCategory::loadAll() as $scancategory){
$row = $scancategory->toArray();
$row['editable'] = $row['editable'] == 1;
if($_['export'] == 'true'){
$row['created'] = date('d-m-Y',$row['created']);
$row['updated'] = date('d-m-Y',$row['updated']);
}
$response['rows'][] = $row;
}
/* Mode export */
if($_['export'] == 'true'){
if(empty($response['rows'])) $response['rows'][] = array('Vide'=>'Aucune données');
$fieldsMapping = array();
foreach (ScanCategory::fields(false) as $key => $value)
$fieldsMapping[$value['label']] = $key;
$stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
File::downloadStream($stream,'export-demandes-'.date('d-m-Y').'.xlsx');
exit();
}
});
//Ajout ou modification d'élément catégorie de scan
Action::register('skanman_scan_category_save',function(&$response){
global $_,$conf,$myFirm;
User::check_access('skanman','edit');
require_once(__DIR__.SLASH.'ScanCategory.class.php');
$item = ScanCategory::provide();
$old = clone $item;
$item->slug = slugify($_['label']);
$item->label = $_['label'];
$item->icon = $_['icon'];
$item->sort = $_['sort'];
$item->path = $_['path'];
$item->editable = true;
$item->scope = $_['scope'];
$item->save();
//trigger pour utilisation sur le workflow
if($myFirm->has_plugin('fr.core.workflow')){
Plugin::need('workflow/WorkflowEvent');
WorkflowEvent::trigger('skanman-scanfile-'.($old->id==0?'create':'update'),array('old'=>$old,'current'=>$item));
}
$conf->put('skanman_synchronization_version',time());
$response = $item->toArray();
});
//Récuperation ou edition d'élément catégorie de scan
Action::register('skanman_scan_category_edit',function(&$response){
global $_;
User::check_access('skanman','edit');
require_once(__DIR__.SLASH.'ScanCategory.class.php');
$response = ScanCategory::getById($_['id'],0)->toArray();
});
//Suppression d'élement catégorie de scan
Action::register('skanman_scan_category_delete',function(&$response){
global $_,$conf;
User::check_access('skanman','delete');
require_once(__DIR__.SLASH.'ScanCategory.class.php');
if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Aucun identifiant renseigné");
ScanCategory::deleteById($_['id']);
$conf->put('skanman_synchronization_version',time());
});
/** SCANFILE / FICHIER **/
Action::register('skanman_scanfile_sort_count',function(&$response){
global $_,$conf;
User::check_access('skanman','read');
require_once(__DIR__.SLASH.'Scanfile.class.php');
Plugin::need('document/Element');
$query = 'SELECT count(*) count FROM '.Element::tableName().' WHERE path LIKE ? AND type=?';
$data = array($conf->get('skanman_sort_category').'/%','file');
$elements = Element::staticQuery($query,$data)->fetch();
$response['count'] = $elements['count'];
});
Action::register('skanman_scanfile_sort',function(&$response){
global $_,$conf;
User::check_access('skanman','read');
require_once(__DIR__.SLASH.'Scanfile.class.php');
Plugin::need('document/Element');
$query = 'SELECT main.* FROM '.Element::tableName().' main LEFT JOIN '.Scanfile::tableName().' sf ON sf.element=main.id WHERE main.path LIKE ?';
$data = array($conf->get('skanman_sort_category').'/%');
if(!empty($_['current'])) {
$direction = $_['direction'] == 'next' ? '>':'<';
$query .= ' AND main.id '.$direction.' ?';
$data[] = $_['current'];
}
$query .= ' ORDER BY main.id ASC LIMIT 1';
$file = Element::staticQuery($query,$data,true,0);
if(count($file)<1) return $response;
$file = $file[0];
$scanfile = Scanfile::load(array('element'=>$file->id));
$row = $scanfile->toArray();
$row['id'] = $file->id;
$user = User::byLogin($row['creator']);
$row['extension'] = $file->extension;
$row['path'] = $file->path;
$row['directory'] = dirname($file->path);
$row['label'] = $file->label;
$row['sizeLabel'] = readable_size($file->size);
$row['url'] = 'action.php?action=skanman_scanfile_preview&path='.urlencode(base64_encode($file->path));
switch($file->extension){
case 'jpg':
case 'jpeg':
case 'png':
case 'gif':
case 'bmp':
$row['file'] = '
';
break;
case 'pdf':
$row['file'] = '';
break;
default:
$row['file'] = ''.getExtIcon($file->extension).'Télécharger
';
break;
}
$row['icon'] = getExtIcon($row['extension']);
$row['created'] = complete_date($row['created']);
$row['tag'] = explode(',',$row['tag']);
$response = $row;
});
Action::register('skanman_scanfile_preview',function(&$response){
global $myUser,$_,$conf;
User::check_access('skanman','read');
Plugin::need('document/Element');
$isopath = Element::root().base64_decode(rawurldecode($_['path']));
$utf8Path = utf8_encode($isopath);
$osPath = get_OS() === 'WIN' ? $isopath : $utf8Path;
$stream = Element::download($utf8Path);
$name = mt_basename($utf8Path);
$mime = File::mime($osPath);//'application/pdf';
if(ob_get_contents()) ob_end_clean();
header("Content-Type: ".$mime);
header("Content-Length: " . strlen($stream));
header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
echo $stream;
exit();
});
//Récuperation d'une liste de fichier
Action::register('skanman_scanfile_search',function(&$response){
global $_;
User::check_access('skanman','read');
require_once(__DIR__.SLASH.'Scanfile.class.php');
Plugin::need('client/Client');
Plugin::need('document/Element');
$query = 'SELECT main.*, sf.id as scanId,sf.client as scanClient,sf.tag as scanTag,'.Client::joinString('cli').' FROM '.Element::tableName().' main
LEFT JOIN '.Scanfile::tableName().' sf ON sf.element=main.id
LEFT JOIN '.Client::tableName().' cli ON sf.client=cli.id
WHERE main.type=? ';
$data = array('file');
//Recherche simple
if(!empty($_['filters']['keyword'])){
$query .= ' AND main.label LIKE ?';
$data[] = '%'.$_['filters']['keyword'].'%';
}
//Recherche avancée
if(isset($_['filters']['advanced'])){
//convertion octet / Mo
$_['filters']['advanced'] = filter_alteration($_['filters']['advanced'],'main.size',function($filter){
$filter['value'][0] = ($filter['value'][0] * 1000000);
return $filter;
});
filter_secure_query($_['filters']['advanced'],array('main.label','sf.tag','sf.creator','sf.client','main.size','main.extension'),$query,$data);
}
//Tri des colonnes
if(isset($_['sort'])){
sort_secure_query($_['sort'],array('main.label','sf.tag','sf.creator','sf.client','main.size','main.extension'),$query,$data);
}else{
$query.=' ORDER BY main.created DESC ';
}
//Pagination
//Par défaut pour une recherche, 20 items, pour un export 5000 max
$itemPerPage = !empty($_['itemPerPage']) ? $_['itemPerPage'] : 30;
//force le nombre de page max a 50 coté serveur
$itemPerPage = $itemPerPage>50 ? 50 : $itemPerPage;
if($_['export'] == 'true') $itemPerPage = 5000;
$response['pagination'] = Scanfile::paginate($itemPerPage,(!empty($_['page'])?$_['page']:0),$query,$data,'main');
$response['rows'] = array();
foreach(Element::staticQuery($query,$data,true,1) as $element){
$scanfile = new Scanfile();
$scanfile->id = $element->foreign('scanId');
$scanfile->tag = $element->foreign('scanTag');
$scanfile->client = $element->foreign('scanClient');
$scanfile->element = $element->id;
$row = $element->toArray();
$user = User::byLogin($row['creator']);
$row['creator'] = $user->toArray();
$row['creator']['fullname'] = $user->fullname();
$row['creator']['avatar'] = $user->getAvatar();
$row['createdReadable'] = complete_date($row['created']).' '.date('H:i',$row['created']);
$row['tag'] = explode(',',$scanfile->tag);
$row['client'] = $scanfile->client;
$row['label'] = $element->label;
$row['size'] = readable_size($element->size);
$row['extension'] = $element->extension;
$row['icon'] = getExtIcon($element->extension);
$row['directory'] = explode('/',dirname($element->path));
if(!empty($element->foreign('client_join_id'))){
$client = new Client();
$client->id = $element->foreign('client_join_id');
$client->label = $element->foreign('client_join_label');
$row['client'] = $client->toArray(true);
}
$row['url'] = 'action.php?action=document_element_execute&path='.urlencode(base64_encode( $element->path));
if($_['export'] == 'true'){
$row['created'] = date('d-m-Y',$row['created']);
$row['updated'] = date('d-m-Y',$row['updated']);
}
$response['rows'][] = $row;
}
/* Mode export */
if($_['export'] == 'true'){
if(empty($response['rows'])) $response['rows'][] = array('Vide'=>'Aucune données');
$fieldsMapping = array();
foreach (Scanfile::fields(false) as $key => $value)
$fieldsMapping[$value['label']] = $key;
$stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
File::downloadStream($stream,'export-demandes-'.date('d-m-Y').'.xlsx');
exit();
}
});
//Ajout ou modification d'élément fichier
Action::register('skanman_scanfile_save',function(&$response){
global $_;
User::check_access('skanman','edit');
require_once(__DIR__.SLASH.'Scanfile.class.php');
Plugin::need('document/Element');
$element = Element::provide();
if(!$item = Scanfile::load(array('element'=>$element->id))){
$item = new Scanfile();
$item->element = $element->id;
}
//$item->label = $_['label'];
$item->tag = $_['tag'];
if(!empty($_['creator'])) $item->creator = $_['creator'];
if(!empty($_['directory'])){
$element = Element::getById($item->element);
$path = str_replace('..','',$_['directory']);
$path = str_replace('./','/',$path);
$path = Element::root().$path.SLASH.basename($element->path);
Element::move(Element::root().$element->path,$path);
}
$item->client = $_['client'];
$item->save();
$response = $item->toArray();
$response['id'] = $element->id;
});
//Suppression d'élement fichier
Action::register('skanman_scanfile_delete',function(&$response){
global $_;
User::check_access('skanman','delete');
require_once(__DIR__.SLASH.'Scanfile.class.php');
Plugin::need('document/Element');
Plugin::need('client/Client');
if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Aucun identifiant renseigné");
$file = Scanfile::load(array('element'=>$_['id']));
$element = Element::getById($_['id']);
Element::remove(Element::root().$element->path);
Element::deleteById($_['id']);
if($file!= false)Scanfile::deleteById($file->id);
});
//Sauvegarde des configurations de Scanner
Action::register('skanman_setting_save',function(&$response){
global $_,$conf;
User::check_access('skanman','configure');
//Si input file "multiple", possibilité de normaliser le
//tableau $_FILES récupéré avec la fonction => normalize_php_files();
foreach(Configuration::setting('skanman') as $key=>$value){
if(!is_array($value)) continue;
$allowed[] = $key;
}
foreach ($_['fields'] as $key => $value) {
if($key == 'skanman_scan_logo'){
//Ajout upload Logo
if(!empty($_['fields']['skanman_scan_logo'])){
$_['skanman_scan_logo'] = $_['fields']['skanman_scan_logo'];
File::save_component('skanman_scan_logo', 'skanman/tosend/logo.{{extension}}');
}
continue;
}
if(in_array($key, $allowed)) $conf->put($key,$value);
}
$conf->put('skanman_synchronization_version', time());
});
Action::register('skanman_widget_load',function(&$response){
require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
$widget = DashboardWidget::current();
$widget->title = 'Widget Skanman';
ob_start();
//Décommenter après avoir créé widget.php
//require_once(__DIR__.SLASH.'widget.php');
//$widget->content = ob_get_clean();
$widget->content = 'Widget non développé';
echo json_encode($widget);
});