ExportModel.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Define a exportmodel.
  4. * @author Valentin MORREEL
  5. * @category Plugin
  6. * @license copyright
  7. */
  8. class ExportModel extends Entity{
  9. public $id,$label,$description,$plugin,$dataset,$slug,$filename,$privacy,$export_format;
  10. protected $TABLE_NAME = 'export_model';
  11. public $fields =
  12. array(
  13. 'id' => 'key',
  14. 'label' => 'string',
  15. 'description' => 'longstring',
  16. 'plugin' => 'string',
  17. 'dataset' => 'string',
  18. 'slug' => 'string',
  19. 'filename' => 'string',
  20. 'privacy' => 'string',
  21. 'export_format' => 'string'
  22. );
  23. const PRIVACY_PRIVATE = 'private';
  24. const PRIVACY_PUBLIC = 'public';
  25. public function privacy(){
  26. return $this->privacy==self::PRIVACY_PRIVATE?'Privé':'Public';
  27. }
  28. //Renvoie les infos des documents placés en dropzone
  29. public function documents(){
  30. $documents = array();
  31. foreach(glob(__ROOT__.FILE_PATH.'export'.SLASH.'documents'.SLASH.$this->plugin.SLASH.$this->id.SLASH.'*.*') as $file){
  32. if(is_dir($file)) continue;
  33. $filenameDisk = $file;
  34. // $filenameDisk = get_OS() === 'WIN' ? html_decode_utf8($file) : $file;
  35. if(get_OS() === 'WIN') $file = utf8_encode($file);
  36. $ext = getExt($file);
  37. $documents[] = array(
  38. 'path' => 'export'.SLASH.'documents'.SLASH.$this->plugin.SLASH.$this->id.SLASH.basename($file),
  39. 'url' => 'action.php?action=export_model_download_document&path='.$this->plugin.SLASH.$this->id.SLASH.rawurlencode(basename($file)),
  40. 'name' => basename($file),
  41. 'ext' => $ext,
  42. 'icon' => getExtIcon($ext),
  43. 'lastModification' => ' - '.date('d/m/Y', filemtime($filenameDisk))
  44. );
  45. }
  46. return $documents;
  47. }
  48. public static function get_standard_dataset($parameters){
  49. global $myUser, $myFirm;
  50. setlocale (LC_TIME, 'fr_FR.utf8','fra');
  51. //Common DS
  52. $data['programme'] = array('type'=>'object','value'=>array(
  53. 'date' => array(
  54. 'label'=>'Date courante',
  55. 'type'=>'object',
  56. 'value' => array(
  57. 'compact' => array('label'=>'Date courante (format "dd/mm/YYYY")','value'=>date('d/m/Y')),
  58. 'littéral' => array('label'=>'Date courante (format "01 Janvier 1970")','value'=>strftime('%d %B %Y'))
  59. )
  60. ),
  61. 'heure' => array('label'=>'Heure courante (format hh:mm)','value'=> date('H:i'))
  62. ));
  63. $data['utilisateur'] = array('type'=>'object','value'=>array(
  64. 'identifiant' => array('label'=>'Identifiant de l\'utilisateur courant','value'=>$myUser->login),
  65. 'prénom' => array('label'=>'Prénom de l\'utilisateur courant','value'=>$myUser->firstname),
  66. 'nom' => array('label'=>'Nom de l\'utilisateur courant','value'=>$myUser->name),
  67. 'fonction' => array('label'=>'Fonction occupée par l\'utilisateur courant','value'=>$myUser->function),
  68. 'email' => array('label'=>'Adresse e-mail de l\'utilisateur courant','value'=>$myUser->mail),
  69. 'photo' => array(
  70. 'label'=>'Photo de profil de l\'utilisateur courant',
  71. 'type'=>'image',
  72. 'value' => file_get_contents($myUser->getAvatar(true))
  73. )
  74. ));
  75. $data['établissement'] = array('type'=>'object','value'=>array(
  76. 'logo' => array(
  77. 'label'=>'Le logo de l\'établissement courant',
  78. 'type'=>'image',
  79. 'value' => file_get_contents($myFirm->logo('path')),
  80. ),
  81. 'libellé' => array('label'=>'Le libellé de l\'établissement courant','value'=>$myFirm->label),
  82. 'téléphone' => array('label'=>'Le n° de téléphone de l\'établissement courant','value'=>$myFirm->phone),
  83. 'fax' => array('label'=>'Le n° de fax de l\'établissement courant','value'=>$myFirm->fax),
  84. 'email' => array('label'=>'L\'adresse mail de l\'établissement courant','value'=>$myFirm->mail),
  85. 'siret' => array('label'=>'Le n° de SIRET de l\'établissement courant','value'=>$myFirm->siret),
  86. ));
  87. $data['établissement']['value']['adresse'] = array('type'=>'object','value'=>array(
  88. 'rue' => array('label'=>'La rue de l\'établissement courant','value'=>$myFirm->street),
  89. 'complément' => array('label'=>'Le complément d\'adresse de l\'établissement courant','value'=>$myFirm->street2),
  90. 'ville' => array('label'=>'La ville de l\'établissement courant','value'=>$myFirm->city),
  91. 'cp' => array('label'=>'Le code postal de l\'établissement courant','value'=>$myFirm->zipcode),
  92. ));
  93. return $data;
  94. }
  95. //Renvoie les différents type de templates pris en compte
  96. public static function templates($key=null){
  97. $templates = array();
  98. foreach (glob(__DIR__.SLASH.'template'.SLASH.'*.class.php') as $templatePath) {
  99. require_once($templatePath);
  100. $className = str_replace('.class.php','',basename($templatePath));
  101. $infos = get_class_vars($className);
  102. $templates[$infos['extension']] = array('handler' => $className,'mime'=>$infos['mime'],'extension'=>$infos['extension'], 'description'=>$infos['description']);
  103. }
  104. if(!isset($key)) return $templates;
  105. return isset($templates[$key]) ? $templates[$key] : $templates['txt'];
  106. }
  107. //Permet d'ajouter un jeu de données pour un plugin donné
  108. public static function add($plugin, $dataset, $label, $func){
  109. Plugin::addHook('export_model_data', function(&$datasets, $params) use ($plugin, $dataset, $label, $func){
  110. global $myUser;
  111. if(isset($params['plugin']) && $params['plugin'] != $plugin) return;
  112. if(isset($params['dataset']) && $params['dataset'] != $dataset) return;
  113. $datasets[$dataset] = array(
  114. 'plugin' => $plugin,
  115. 'dataset' => $dataset,
  116. 'label' => $label,
  117. 'function' => $func
  118. );
  119. });
  120. }
  121. //Récuperation de toutes les données (ou description) d'un dataset
  122. public static function dataset($plugin,$dataset,$parameters=array(),$description = null){
  123. $datasets = array();
  124. Plugin::callHook('export_model_data', array(
  125. &$datasets, array(
  126. 'plugin' => $plugin,
  127. 'dataset' => $dataset,
  128. 'description' => true
  129. )));
  130. $current = reset($datasets);
  131. $data = $current['function']($parameters);
  132. $current['values'] = array();
  133. //Merge des données génériques erp et des données du dataset
  134. $allDataset = array_merge_recursive(
  135. ExportModel::get_standard_dataset($parameters),
  136. (!$data?array():$data)
  137. );
  138. $current['values'] = self::recursive_dataset_empty($allDataset);
  139. return $current;
  140. }
  141. public static function rawData($dataset){
  142. foreach ($dataset as $key => $value) {
  143. if(is_array($value) && array_key_exists('value', $value)){
  144. if(is_array($value['value'])) $value['value'] = self::rawData($value['value']);
  145. $dataset[$key] = empty($value['value']) ? '' : $value['value'];
  146. }else{
  147. if(is_array($value)) $dataset[$key] = self::rawData($value);
  148. }
  149. }
  150. return $dataset;
  151. }
  152. public static function recursive_dataset_empty($set){
  153. $format = array();
  154. foreach ($set as $macro => $row) {
  155. if(is_array($row)){
  156. if(!empty($row['value']) && empty($row['type'])) $row['type'] = 'value';
  157. $row = self::recursive_dataset_empty($row);
  158. }
  159. if(isset($row['value']) && is_string($row['value'])){
  160. $row['value'] = '';
  161. }
  162. $format[$macro] = $row;
  163. }
  164. return $format;
  165. }
  166. //Export d'un set de donnée $datas dans le template $modelStream en fonction du type de document $type
  167. // ex : ExportModel::export('CsvExport', 'col1;{{name}}',array('name'=> array('value' => 'toto') ));
  168. public static function export($type, $modelStream, $datas, $parameters=array()){
  169. require_once(__DIR__.SLASH.'template'.SLASH.$type.'.class.php');
  170. $instance = new $type();
  171. if(method_exists($instance,'start')) $instance->start($modelStream,$datas, $parameters);
  172. $stream = $instance->from_template($modelStream, $datas);
  173. if(method_exists($instance,'end')) $stream = $instance->end($stream, $datas, $parameters);
  174. return $stream;
  175. }
  176. }
  177. ?>