action.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. //Récupération logo application générale
  3. Action::register('skanman_scan_logo',function(&$response){
  4. global $myUser,$_;
  5. User::check_access('skanman','configure');
  6. File::handle_component(array(
  7. 'namespace' => 'skanman', //stockés dans skanman/*.*
  8. 'access' => 'skanman', // crud sur core,
  9. 'size' => '10000000', // taille max
  10. 'storage' => 'skanman/tosend/logo.{png,jpg,jpeg,gif,png}' //chemin complet vers le fichier stocké
  11. ), $response);
  12. });
  13. /** SCANCATEGORY / CATéGORIE DE SCAN **/
  14. //Récuperation d'une liste de catégorie de scan
  15. Action::register('skanman_scan_category_search',function(&$response){
  16. global $_;
  17. User::check_access('skanman','read');
  18. require_once(__DIR__.SLASH.'ScanCategory.class.php');
  19. $response['rows'] = array();
  20. foreach(ScanCategory::loadAll() as $scancategory){
  21. $row = $scancategory->toArray();
  22. $row['editable'] = $row['editable'] == 1;
  23. if($_['export'] == 'true'){
  24. $row['created'] = date('d-m-Y',$row['created']);
  25. $row['updated'] = date('d-m-Y',$row['updated']);
  26. }
  27. $response['rows'][] = $row;
  28. }
  29. /* Mode export */
  30. if($_['export'] == 'true'){
  31. if(empty($response['rows'])) $response['rows'][] = array('Vide'=>'Aucune données');
  32. $fieldsMapping = array();
  33. foreach (ScanCategory::fields(false) as $key => $value)
  34. $fieldsMapping[$value['label']] = $key;
  35. $stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
  36. File::downloadStream($stream,'export-demandes-'.date('d-m-Y').'.xlsx');
  37. exit();
  38. }
  39. });
  40. //Ajout ou modification d'élément catégorie de scan
  41. Action::register('skanman_scan_category_save',function(&$response){
  42. global $_,$conf,$myFirm;
  43. User::check_access('skanman','edit');
  44. require_once(__DIR__.SLASH.'ScanCategory.class.php');
  45. $item = ScanCategory::provide();
  46. $old = clone $item;
  47. $item->slug = slugify($_['label']);
  48. $item->label = $_['label'];
  49. $item->icon = $_['icon'];
  50. $item->sort = $_['sort'];
  51. $item->path = $_['path'];
  52. $item->editable = true;
  53. $item->scope = $_['scope'];
  54. $item->save();
  55. //trigger pour utilisation sur le workflow
  56. if($myFirm->has_plugin('fr.core.workflow')){
  57. Plugin::need('workflow/WorkflowEvent');
  58. WorkflowEvent::trigger('skanman-scanfile-'.($old->id==0?'create':'update'),array('old'=>$old,'current'=>$item));
  59. }
  60. $conf->put('skanman_synchronization_version',time());
  61. $response = $item->toArray();
  62. });
  63. //Récuperation ou edition d'élément catégorie de scan
  64. Action::register('skanman_scan_category_edit',function(&$response){
  65. global $_;
  66. User::check_access('skanman','edit');
  67. require_once(__DIR__.SLASH.'ScanCategory.class.php');
  68. $response = ScanCategory::getById($_['id'],0)->toArray();
  69. });
  70. //Suppression d'élement catégorie de scan
  71. Action::register('skanman_scan_category_delete',function(&$response){
  72. global $_,$conf;
  73. User::check_access('skanman','delete');
  74. require_once(__DIR__.SLASH.'ScanCategory.class.php');
  75. if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Aucun identifiant renseigné");
  76. ScanCategory::deleteById($_['id']);
  77. $conf->put('skanman_synchronization_version',time());
  78. });
  79. /** SCANFILE / FICHIER **/
  80. Action::register('skanman_scanfile_sort_count',function(&$response){
  81. global $_,$conf;
  82. User::check_access('skanman','read');
  83. require_once(__DIR__.SLASH.'Scanfile.class.php');
  84. Plugin::need('document/Element');
  85. $query = 'SELECT count(*) count FROM '.Element::tableName().' WHERE path LIKE ? AND type=?';
  86. $data = array($conf->get('skanman_sort_category').'/%','file');
  87. $elements = Element::staticQuery($query,$data)->fetch();
  88. $response['count'] = $elements['count'];
  89. });
  90. Action::register('skanman_scanfile_sort',function(&$response){
  91. global $_,$conf;
  92. User::check_access('skanman','read');
  93. require_once(__DIR__.SLASH.'Scanfile.class.php');
  94. Plugin::need('document/Element');
  95. $query = 'SELECT main.* FROM '.Element::tableName().' main LEFT JOIN '.Scanfile::tableName().' sf ON sf.element=main.id WHERE main.path LIKE ?';
  96. $data = array($conf->get('skanman_sort_category').'/%');
  97. if(!empty($_['current'])) {
  98. $direction = $_['direction'] == 'next' ? '>':'<';
  99. $query .= ' AND main.id '.$direction.' ?';
  100. $data[] = $_['current'];
  101. }
  102. $query .= ' ORDER BY main.id ASC LIMIT 1';
  103. $file = Element::staticQuery($query,$data,true,0);
  104. if(count($file)<1) return $response;
  105. $file = $file[0];
  106. $scanfile = Scanfile::load(array('element'=>$file->id));
  107. $row = $scanfile->toArray();
  108. $row['id'] = $file->id;
  109. $user = User::byLogin($row['creator']);
  110. $row['extension'] = $file->extension;
  111. $row['path'] = $file->path;
  112. $row['directory'] = dirname($file->path);
  113. $row['label'] = $file->label;
  114. $row['sizeLabel'] = readable_size($file->size);
  115. $row['url'] = 'action.php?action=skanman_scanfile_preview&path='.urlencode(base64_encode($file->path));
  116. switch($file->extension){
  117. case 'jpg':
  118. case 'jpeg':
  119. case 'png':
  120. case 'gif':
  121. case 'bmp':
  122. $row['file'] = '<img src="'.$row['url'].'">';
  123. break;
  124. case 'pdf':
  125. $row['file'] = '<a data-type="pdf" href="'.$row['url'].'"></a>';
  126. break;
  127. default:
  128. $row['file'] = '<a href="'.$row['url'].'">'.getExtIcon($file->extension).'<div>Télécharger</div></a>';
  129. break;
  130. }
  131. $row['icon'] = getExtIcon($row['extension']);
  132. $row['created'] = complete_date($row['created']);
  133. $row['tag'] = explode(',',$row['tag']);
  134. $response = $row;
  135. });
  136. Action::register('skanman_scanfile_preview',function(&$response){
  137. global $myUser,$_,$conf;
  138. User::check_access('skanman','read');
  139. Plugin::need('document/Element');
  140. $isopath = Element::root().base64_decode(rawurldecode($_['path']));
  141. $utf8Path = utf8_encode($isopath);
  142. $osPath = get_OS() === 'WIN' ? $isopath : $utf8Path;
  143. $stream = Element::download($utf8Path);
  144. $name = mt_basename($utf8Path);
  145. $mime = File::mime($osPath);//'application/pdf';
  146. if(ob_get_contents()) ob_end_clean();
  147. header("Content-Type: ".$mime);
  148. header("Content-Length: " . strlen($stream));
  149. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  150. header('Cache-Control: no-store, no-cache, must-revalidate');
  151. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  152. header('Cache-Control: post-check=0, pre-check=0', FALSE);
  153. header('Pragma: no-cache');
  154. echo $stream;
  155. exit();
  156. });
  157. //Récuperation d'une liste de fichier
  158. Action::register('skanman_scanfile_search',function(&$response){
  159. global $_;
  160. User::check_access('skanman','read');
  161. require_once(__DIR__.SLASH.'Scanfile.class.php');
  162. Plugin::need('client/Client');
  163. Plugin::need('document/Element');
  164. $query = 'SELECT main.*, sf.id as scanId,sf.client as scanClient,sf.tag as scanTag,'.Client::joinString('cli').' FROM '.Element::tableName().' main
  165. LEFT JOIN '.Scanfile::tableName().' sf ON sf.element=main.id
  166. LEFT JOIN '.Client::tableName().' cli ON sf.client=cli.id
  167. WHERE main.type=? ';
  168. $data = array('file');
  169. //Recherche simple
  170. if(!empty($_['filters']['keyword'])){
  171. $query .= ' AND main.label LIKE ?';
  172. $data[] = '%'.$_['filters']['keyword'].'%';
  173. }
  174. //Recherche avancée
  175. if(isset($_['filters']['advanced'])){
  176. //convertion octet / Mo
  177. $_['filters']['advanced'] = filter_alteration($_['filters']['advanced'],'main.size',function($filter){
  178. $filter['value'][0] = ($filter['value'][0] * 1000000);
  179. return $filter;
  180. });
  181. filter_secure_query($_['filters']['advanced'],array('main.label','sf.tag','sf.creator','sf.client','main.size','main.extension'),$query,$data);
  182. }
  183. //Tri des colonnes
  184. if(isset($_['sort'])){
  185. sort_secure_query($_['sort'],array('main.label','sf.tag','sf.creator','sf.client','main.size','main.extension'),$query,$data);
  186. }else{
  187. $query.=' ORDER BY main.created DESC ';
  188. }
  189. //Pagination
  190. //Par défaut pour une recherche, 20 items, pour un export 5000 max
  191. $itemPerPage = !empty($_['itemPerPage']) ? $_['itemPerPage'] : 30;
  192. //force le nombre de page max a 50 coté serveur
  193. $itemPerPage = $itemPerPage>50 ? 50 : $itemPerPage;
  194. if($_['export'] == 'true') $itemPerPage = 5000;
  195. $response['pagination'] = Scanfile::paginate($itemPerPage,(!empty($_['page'])?$_['page']:0),$query,$data,'main');
  196. $response['rows'] = array();
  197. foreach(Element::staticQuery($query,$data,true,1) as $element){
  198. $scanfile = new Scanfile();
  199. $scanfile->id = $element->foreign('scanId');
  200. $scanfile->tag = $element->foreign('scanTag');
  201. $scanfile->client = $element->foreign('scanClient');
  202. $scanfile->element = $element->id;
  203. $row = $element->toArray();
  204. $user = User::byLogin($row['creator']);
  205. $row['creator'] = $user->toArray();
  206. $row['creator']['fullname'] = $user->fullname();
  207. $row['creator']['avatar'] = $user->getAvatar();
  208. $row['createdReadable'] = complete_date($row['created']).' '.date('H:i',$row['created']);
  209. $row['tag'] = explode(',',$scanfile->tag);
  210. $row['client'] = $scanfile->client;
  211. $row['label'] = $element->label;
  212. $row['size'] = readable_size($element->size);
  213. $row['extension'] = $element->extension;
  214. $row['icon'] = getExtIcon($element->extension);
  215. $row['directory'] = explode('/',dirname($element->path));
  216. if(!empty($element->foreign('client_join_id'))){
  217. $client = new Client();
  218. $client->id = $element->foreign('client_join_id');
  219. $client->label = $element->foreign('client_join_label');
  220. $row['client'] = $client->toArray(true);
  221. }
  222. $row['url'] = 'action.php?action=document_element_execute&path='.urlencode(base64_encode( $element->path));
  223. if($_['export'] == 'true'){
  224. $row['created'] = date('d-m-Y',$row['created']);
  225. $row['updated'] = date('d-m-Y',$row['updated']);
  226. }
  227. $response['rows'][] = $row;
  228. }
  229. /* Mode export */
  230. if($_['export'] == 'true'){
  231. if(empty($response['rows'])) $response['rows'][] = array('Vide'=>'Aucune données');
  232. $fieldsMapping = array();
  233. foreach (Scanfile::fields(false) as $key => $value)
  234. $fieldsMapping[$value['label']] = $key;
  235. $stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
  236. File::downloadStream($stream,'export-demandes-'.date('d-m-Y').'.xlsx');
  237. exit();
  238. }
  239. });
  240. //Ajout ou modification d'élément fichier
  241. Action::register('skanman_scanfile_save',function(&$response){
  242. global $_;
  243. User::check_access('skanman','edit');
  244. require_once(__DIR__.SLASH.'Scanfile.class.php');
  245. Plugin::need('document/Element');
  246. $element = Element::provide();
  247. if(!$item = Scanfile::load(array('element'=>$element->id))){
  248. $item = new Scanfile();
  249. $item->element = $element->id;
  250. }
  251. //$item->label = $_['label'];
  252. $item->tag = $_['tag'];
  253. if(!empty($_['creator'])) $item->creator = $_['creator'];
  254. if(!empty($_['directory'])){
  255. $element = Element::getById($item->element);
  256. $path = str_replace('..','',$_['directory']);
  257. $path = str_replace('./','/',$path);
  258. $path = Element::root().$path.SLASH.basename($element->path);
  259. Element::move(Element::root().$element->path,$path);
  260. }
  261. $item->client = $_['client'];
  262. $item->save();
  263. $response = $item->toArray();
  264. $response['id'] = $element->id;
  265. });
  266. //Suppression d'élement fichier
  267. Action::register('skanman_scanfile_delete',function(&$response){
  268. global $_;
  269. User::check_access('skanman','delete');
  270. require_once(__DIR__.SLASH.'Scanfile.class.php');
  271. Plugin::need('document/Element');
  272. Plugin::need('client/Client');
  273. if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Aucun identifiant renseigné");
  274. $file = Scanfile::load(array('element'=>$_['id']));
  275. $element = Element::getById($_['id']);
  276. Element::remove(Element::root().$element->path);
  277. Element::deleteById($_['id']);
  278. if($file!= false)Scanfile::deleteById($file->id);
  279. });
  280. //Sauvegarde des configurations de Scanner
  281. Action::register('skanman_setting_save',function(&$response){
  282. global $_,$conf;
  283. User::check_access('skanman','configure');
  284. //Si input file "multiple", possibilité de normaliser le
  285. //tableau $_FILES récupéré avec la fonction => normalize_php_files();
  286. foreach(Configuration::setting('skanman') as $key=>$value){
  287. if(!is_array($value)) continue;
  288. $allowed[] = $key;
  289. }
  290. foreach ($_['fields'] as $key => $value) {
  291. if($key == 'skanman_scan_logo'){
  292. //Ajout upload Logo
  293. if(!empty($_['fields']['skanman_scan_logo'])){
  294. $_['skanman_scan_logo'] = $_['fields']['skanman_scan_logo'];
  295. File::save_component('skanman_scan_logo', 'skanman/tosend/logo.{{extension}}');
  296. }
  297. continue;
  298. }
  299. if(in_array($key, $allowed)) $conf->put($key,$value);
  300. }
  301. $conf->put('skanman_synchronization_version', time());
  302. });
  303. Action::register('skanman_widget_load',function(&$response){
  304. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  305. $widget = DashboardWidget::current();
  306. $widget->title = 'Widget Skanman';
  307. ob_start();
  308. //Décommenter après avoir créé widget.php
  309. //require_once(__DIR__.SLASH.'widget.php');
  310. //$widget->content = ob_get_clean();
  311. $widget->content = 'Widget non développé';
  312. echo json_encode($widget);
  313. });