PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/exports/class/export.class.php

https://github.com/asterix14/dolibarr
PHP | 485 lines | 292 code | 65 blank | 128 comment | 50 complexity | f19285d702784cc45c24f9352876f8bc MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/exports/class/export.class.php
  20. * \ingroup export
  21. * \brief File of class to manage exports
  22. */
  23. /**
  24. * \class Export
  25. * \brief Class to manage exports
  26. */
  27. class Export
  28. {
  29. var $db;
  30. var $array_export_code=array(); // Tableau de "idmodule_numlot"
  31. var $array_export_module=array(); // Tableau de "nom de modules"
  32. var $array_export_label=array(); // Tableau de "libelle de lots"
  33. var $array_export_sql=array(); // Tableau des "requetes sql"
  34. var $array_export_fields=array(); // Tableau des listes de champ+libelle a exporter
  35. //var $array_export_alias=array(); // Tableau des listes de champ+alias a exporter
  36. var $array_export_special=array(); // Tableau des operations speciales sur champ
  37. // To store export modules
  38. var $hexa;
  39. var $datatoexport;
  40. var $model_name;
  41. var $sqlusedforexport;
  42. /**
  43. * Constructor
  44. *
  45. * @param DoliDB $DB Database handler
  46. */
  47. function Export($DB)
  48. {
  49. $this->db=$DB;
  50. }
  51. /**
  52. * Load an exportable dataset
  53. *
  54. * @param User $user Object user making export
  55. * @param string $filter Load a particular dataset only
  56. * @return int <0 if KO, >0 if OK
  57. */
  58. function load_arrays($user,$filter='')
  59. {
  60. global $langs,$conf,$mysoc;
  61. dol_syslog("Export::load_arrays user=".$user->id." filter=".$filter);
  62. $var=true;
  63. $i=0;
  64. foreach ($conf->file->dol_document_root as $type => $dirroot)
  65. {
  66. $modulesdir[] = $dirroot . "/core/modules/";
  67. if ($type == 'alt')
  68. {
  69. $handle=@opendir($dirroot);
  70. if (is_resource($handle))
  71. {
  72. while (($file = readdir($handle))!==false)
  73. {
  74. if (is_dir($dirroot.'/'.$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && $file != 'includes')
  75. {
  76. if (is_dir($dirroot . '/' . $file . '/core/modules/'))
  77. {
  78. $modulesdir[] = $dirroot . '/' . $file . '/core/modules/';
  79. }
  80. }
  81. }
  82. closedir($handle);
  83. }
  84. }
  85. }
  86. foreach($modulesdir as $dir)
  87. {
  88. // Search available exports
  89. $handle=@opendir($dir);
  90. if (is_resource($handle))
  91. {
  92. // Search module files
  93. while (($file = readdir($handle))!==false)
  94. {
  95. if (is_readable($dir.$file) && preg_match("/^(mod.*)\.class\.php$/i",$file,$reg))
  96. {
  97. $modulename=$reg[1];
  98. // Defined if module is enabled
  99. $enabled=true;
  100. $part=strtolower(preg_replace('/^mod/i','',$modulename));
  101. if (empty($conf->$part->enabled)) $enabled=false;
  102. if ($enabled)
  103. {
  104. // Chargement de la classe
  105. $file = $dir.$modulename.".class.php";
  106. $classname = $modulename;
  107. require_once($file);
  108. $module = new $classname($this->db);
  109. if (is_array($module->export_code))
  110. {
  111. foreach($module->export_code as $r => $value)
  112. {
  113. //print $i.'-'.$filter.'-'.$modulename.'-'.join(',',$module->export_code).'<br>';
  114. if ($filter && ($filter != $module->export_code[$r])) continue;
  115. // Test if condition to show are ok
  116. if (! empty($module->export_enabled[$r]) && ! verifCond($module->export_enabled[$r])) continue;
  117. // Test if permissions are ok
  118. $bool=true;
  119. foreach($module->export_permission[$r] as $val)
  120. {
  121. $perm=$val;
  122. //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
  123. if ($perm[2])
  124. {
  125. $bool=$user->rights->$perm[0]->$perm[1]->$perm[2];
  126. }
  127. else
  128. {
  129. $bool=$user->rights->$perm[0]->$perm[1];
  130. }
  131. if ($perm[0]=='user' && $user->admin) $bool=true;
  132. if (! $bool) break;
  133. }
  134. //print $bool." $perm[0]"."<br>";
  135. // Permissions ok
  136. // if ($bool)
  137. // {
  138. // Charge fichier lang en rapport
  139. $langtoload=$module->getLangFilesArray();
  140. if (is_array($langtoload))
  141. {
  142. foreach($langtoload as $key)
  143. {
  144. $langs->load($key);
  145. }
  146. }
  147. // Module
  148. $this->array_export_module[$i]=$module;
  149. // Permission
  150. $this->array_export_perms[$i]=$bool;
  151. // Icon
  152. $this->array_export_icon[$i]=(isset($module->export_icon[$r])?$module->export_icon[$r]:$module->picto);
  153. // Code du dataset export
  154. $this->array_export_code[$i]=$module->export_code[$r];
  155. // Libelle du dataset export
  156. $this->array_export_label[$i]=$module->getExportDatasetLabel($r);
  157. // Tableau des champ a exporter (cle=champ, valeur=libelle)
  158. $this->array_export_fields[$i]=$module->export_fields_array[$r];
  159. // Tableau des entites a exporter (cle=champ, valeur=entite)
  160. $this->array_export_entities[$i]=$module->export_entities_array[$r];
  161. // Tableau des operations speciales sur champ
  162. $this->array_export_special[$i]=$module->export_special_array[$r];
  163. // Requete sql du dataset
  164. $this->array_export_sql_start[$i]=$module->export_sql_start[$r];
  165. $this->array_export_sql_end[$i]=$module->export_sql_end[$r];
  166. //$this->array_export_sql[$i]=$module->export_sql[$r];
  167. dol_syslog("Export loaded for module ".$modulename." with index ".$i.", dataset=".$module->export_code[$r].", nb of fields=".count($module->export_fields_code[$r]));
  168. $i++;
  169. // }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. closedir($handle);
  176. }
  177. }
  178. return 1;
  179. }
  180. /**
  181. * Build the sql export request.
  182. * Arrays this->array_export_xxx are already loaded for required datatoexport
  183. *
  184. * @param int $indice Indice of export
  185. * @param array $array_selected Filter on array of fields to export
  186. * @return string SQL String. Example "select s.rowid as r_rowid, s.status as s_status from ..."
  187. */
  188. function build_sql($indice,$array_selected)
  189. {
  190. // Build the sql request
  191. $sql=$this->array_export_sql_start[$indice];
  192. $i=0;
  193. //print_r($array_selected);
  194. foreach ($this->array_export_fields[$indice] as $key => $value)
  195. {
  196. if (! array_key_exists($key, $array_selected)) continue; // Field not selected
  197. if ($i > 0) $sql.=', ';
  198. else $i++;
  199. $newfield=$key.' as '.str_replace(array('.', '-'),'_',$key);;
  200. $sql.=$newfield;
  201. }
  202. $sql.=$this->array_export_sql_end[$indice];
  203. return $sql;
  204. }
  205. /**
  206. * Build export file.
  207. * File is built into directory $conf->export->dir_temp.'/'.$user->id
  208. * Arrays this->array_export_xxx are already loaded for required datatoexport
  209. *
  210. * @param User $user User that export
  211. * @param string $model Export format
  212. * @param string $datatoexport Name of dataset to export
  213. * @param array $array_selected Filter on array of fields to export
  214. * @param string $sqlquery If set, transmit a sql query instead of building it from arrays
  215. * @return int <0 if KO, >0 if OK
  216. */
  217. function build_file($user, $model, $datatoexport, $array_selected, $sqlquery = '')
  218. {
  219. global $conf,$langs;
  220. $indice=0;
  221. asort($array_selected);
  222. dol_syslog("Export::build_file $model, $datatoexport, $array_selected");
  223. // Check parameters or context properties
  224. if (! is_array($this->array_export_fields[$indice]))
  225. {
  226. $this->error="ErrorBadParameter";
  227. return -1;
  228. }
  229. // Creation de la classe d'export du model ExportXXX
  230. $dir = DOL_DOCUMENT_ROOT . "/core/modules/export/";
  231. $file = "export_".$model.".modules.php";
  232. $classname = "Export".$model;
  233. require_once($dir.$file);
  234. $objmodel = new $classname($db);
  235. if ($sqlquery) $sql = $sqlquery;
  236. else $sql=$this->build_sql($indice,$array_selected);
  237. // Run the sql
  238. $this->sqlusedforexport=$sql;
  239. dol_syslog("Export::build_file sql=".$sql);
  240. $resql = $this->db->query($sql);
  241. if ($resql)
  242. {
  243. //$this->array_export_label[$indice]
  244. $filename="export_".$datatoexport;
  245. $filename.='.'.$objmodel->getDriverExtension();
  246. $dirname=$conf->export->dir_temp.'/'.$user->id;
  247. $outputlangs=$langs; // Lang for output
  248. // Open file
  249. dol_mkdir($dirname);
  250. $result=$objmodel->open_file($dirname."/".$filename, $outputlangs);
  251. if ($result >= 0)
  252. {
  253. // Genere en-tete
  254. $objmodel->write_header($outputlangs);
  255. // Genere ligne de titre
  256. $objmodel->write_title($this->array_export_fields[$indice],$array_selected,$outputlangs);
  257. while ($objp = $this->db->fetch_object($resql))
  258. {
  259. $var=!$var;
  260. // Process special operations
  261. if (! empty($this->array_export_special[$indice]))
  262. {
  263. foreach ($this->array_export_special[$indice] as $key => $value)
  264. {
  265. if (! array_key_exists($key, $array_selected)) continue; // Field not selected
  266. // Operation NULLIFNEG
  267. if ($this->array_export_special[$indice][$key]=='NULLIFNEG')
  268. {
  269. //$alias=$this->array_export_alias[$indice][$key];
  270. $alias=str_replace(array('.', '-'),'_',$key);
  271. if ($objp->$alias < 0) $objp->$alias='';
  272. }
  273. // Operation ZEROIFNEG
  274. if ($this->array_export_special[$indice][$key]=='ZEROIFNEG')
  275. {
  276. //$alias=$this->array_export_alias[$indice][$key];
  277. $alias=str_replace(array('.', '-'),'_',$key);
  278. if ($objp->$alias < 0) $objp->$alias='0';
  279. }
  280. }
  281. }
  282. // end of special operation processing
  283. $objmodel->write_record($array_selected,$objp,$outputlangs);
  284. }
  285. // Genere en-tete
  286. $objmodel->write_footer($outputlangs);
  287. // Close file
  288. $objmodel->close_file();
  289. return 1;
  290. }
  291. else
  292. {
  293. $this->error=$objmodel->error;
  294. dol_syslog("Export::build_file Error: ".$this->error, LOG_ERR);
  295. return -1;
  296. }
  297. }
  298. else
  299. {
  300. $this->error=$this->db->error()." - sql=".$sql;
  301. dol_syslog("Export::build_file Error: ".$this->error, LOG_ERR);
  302. return -1;
  303. }
  304. }
  305. /**
  306. * Save an export model in database
  307. *
  308. * @param User $user Object user that save
  309. * @return int <0 if KO, >0 if OK
  310. */
  311. function create($user)
  312. {
  313. global $conf;
  314. dol_syslog("Export.class.php::create");
  315. $this->db->begin();
  316. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'export_model (';
  317. $sql.= 'label, type, field)';
  318. $sql.= " VALUES ('".$this->model_name."', '".$this->datatoexport."', '".$this->hexa."')";
  319. dol_syslog("Export::create sql=".$sql, LOG_DEBUG);
  320. $resql=$this->db->query($sql);
  321. if ($resql)
  322. {
  323. $this->db->commit();
  324. return 1;
  325. }
  326. else
  327. {
  328. $this->error=$this->db->lasterror();
  329. $this->errno=$this->db->lasterrno();
  330. dol_syslog("Export::create error ".$this->error, LOG_ERR);
  331. $this->db->rollback();
  332. return -1;
  333. }
  334. }
  335. /**
  336. * Load an export profil from database
  337. *
  338. * @param int $id Id of profil to load
  339. * @return int <0 if KO, >0 if OK
  340. */
  341. function fetch($id)
  342. {
  343. $sql = 'SELECT em.rowid, em.field, em.label, em.type';
  344. $sql.= ' FROM '.MAIN_DB_PREFIX.'export_model as em';
  345. $sql.= ' WHERE em.rowid = '.$id;
  346. dol_syslog("Export::fetch sql=".$sql, LOG_DEBUG);
  347. $result = $this->db->query($sql);
  348. if ($result)
  349. {
  350. $obj = $this->db->fetch_object($result);
  351. if ($obj)
  352. {
  353. $this->id = $obj->rowid;
  354. $this->hexa = $obj->field;
  355. $this->model_name = $obj->label;
  356. $this->datatoexport = $obj->type;
  357. return 1;
  358. }
  359. else
  360. {
  361. $this->error="Model not found";
  362. return -2;
  363. }
  364. }
  365. else
  366. {
  367. dol_print_error($this->db);
  368. return -3;
  369. }
  370. }
  371. /**
  372. * Delete object in database
  373. *
  374. * @param User $user User that delete
  375. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  376. * @return int <0 if KO, >0 if OK
  377. */
  378. function delete($user, $notrigger=0)
  379. {
  380. global $conf, $langs;
  381. $error=0;
  382. $sql = "DELETE FROM ".MAIN_DB_PREFIX."export_model";
  383. $sql.= " WHERE rowid=".$this->id;
  384. $this->db->begin();
  385. dol_syslog(get_class($this)."::delete sql=".$sql);
  386. $resql = $this->db->query($sql);
  387. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  388. if (! $error)
  389. {
  390. if (! $notrigger)
  391. {
  392. // Uncomment this and change MYOBJECT to your own tag if you
  393. // want this action call a trigger.
  394. //// Call triggers
  395. //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  396. //$interface=new Interfaces($this->db);
  397. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  398. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  399. //// End call triggers
  400. }
  401. }
  402. // Commit or rollback
  403. if ($error)
  404. {
  405. foreach($this->errors as $errmsg)
  406. {
  407. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  408. $this->error.=($this->error?', '.$errmsg:$errmsg);
  409. }
  410. $this->db->rollback();
  411. return -1*$error;
  412. }
  413. else
  414. {
  415. $this->db->commit();
  416. return 1;
  417. }
  418. }
  419. }
  420. ?>