ExportModel.class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Define a exportmodel.
  4. * @author Valentin MORREEL
  5. * @category Plugin
  6. * @license MIT
  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. if(get_OS() === 'WIN') $file = utf8_encode($file);
  35. $ext = getExt($file);
  36. $documents[] = array(
  37. 'path' => 'export'.SLASH.'documents'.SLASH.$this->plugin.SLASH.$this->id.SLASH.basename($file),
  38. 'url' => 'action.php?action=export_model_download_document&path='.$this->plugin.SLASH.$this->id.SLASH.rawurlencode(basename($file)),
  39. 'name' => basename($file),
  40. 'ext' => $ext,
  41. 'icon' => getExtIcon($ext),
  42. 'lastModification' => ' - '.date('d/m/Y', filemtime($filenameDisk))
  43. );
  44. }
  45. return $documents;
  46. }
  47. public static function get_standard_dataset($parameters){
  48. global $myUser, $myFirm;
  49. setlocale (LC_TIME, 'fr_FR.utf8','fra');
  50. //Common DS
  51. $data['programme'] = array('type'=>'object','value'=>array(
  52. 'date' => array(
  53. 'label'=>'Date courante',
  54. 'type'=>'object',
  55. 'value' => array(
  56. 'compact' => array('label'=>'Date courante (format "dd/mm/YYYY")','value'=>date('d/m/Y')),
  57. 'littéral' => array('label'=>'Date courante (format "01 Janvier 1970")','value'=>strftime('%d %B %Y'))
  58. )
  59. ),
  60. 'heure' => array('label'=>'Heure courante (format hh:mm)','value'=> date('H:i')),
  61. 'adresse-racine' => array('label'=>'Adresse racine','value'=> ROOT_URL),
  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. 'logo_64' => array(
  82. 'label'=>'Le logo de l\'établissement courant en base64',
  83. 'type'=>'image',
  84. 'value' => base64_encode(file_get_contents($myFirm->logo('path'))),
  85. ),
  86. 'logo_mime' => array(
  87. 'label'=>'Le type mime du logo',
  88. 'value' => mime_content_type($myFirm->logo('path')),
  89. ),
  90. 'libellé' => array('label'=>'Le libellé de l\'établissement courant','value'=>$myFirm->label),
  91. 'téléphone' => array('label'=>'Le n° de téléphone de l\'établissement courant','value'=>$myFirm->phone),
  92. 'fax' => array('label'=>'Le n° de fax de l\'établissement courant','value'=>$myFirm->fax),
  93. 'email' => array('label'=>'L\'adresse mail de l\'établissement courant','value'=>$myFirm->mail),
  94. 'siret' => array('label'=>'Le n° de SIRET de l\'établissement courant','value'=>$myFirm->siret),
  95. ));
  96. $data['établissement']['value']['adresse'] = array('type'=>'object','value'=>array(
  97. 'rue' => array('label'=>'La rue de l\'établissement courant','value'=>$myFirm->street),
  98. 'complément' => array('label'=>'Le complément d\'adresse de l\'établissement courant','value'=>$myFirm->street2),
  99. 'ville' => array('label'=>'La ville de l\'établissement courant','value'=>$myFirm->city),
  100. 'cp' => array('label'=>'Le code postal de l\'établissement courant','value'=>$myFirm->zipcode),
  101. ));
  102. return $data;
  103. }
  104. //Renvoie les différents type de templates pris en compte
  105. public static function templates($key=null){
  106. $templates = array();
  107. foreach (glob(__DIR__.SLASH.'template'.SLASH.'*.class.php') as $templatePath) {
  108. require_once($templatePath);
  109. $className = str_replace('.class.php','',basename($templatePath));
  110. $infos = get_class_vars($className);
  111. $templates[$infos['extension']] = array('handler' => $className,'mime'=>$infos['mime'],'extension'=>$infos['extension'], 'description'=>$infos['description']);
  112. }
  113. if(!isset($key)) return $templates;
  114. return isset($templates[$key]) ? $templates[$key] : $templates['txt'];
  115. }
  116. //Permet d'ajouter un jeu de données pour un plugin donné
  117. public static function add($plugin, $dataset, $label, $func){
  118. Plugin::addHook('export_model_data', function(&$datasets, $params) use ($plugin, $dataset, $label, $func){
  119. global $myUser;
  120. if(isset($params['plugin']) && $params['plugin'] != $plugin) return;
  121. if(isset($params['dataset']) && $params['dataset'] != $dataset) return;
  122. $datasets[$dataset] = array(
  123. 'plugin' => $plugin,
  124. 'dataset' => $dataset,
  125. 'label' => $label,
  126. 'function' => $func
  127. );
  128. });
  129. }
  130. //Récuperation de toutes les données (ou description) d'un dataset
  131. public static function dataset($plugin,$dataset,$parameters=array(),$description = null){
  132. $datasets = array();
  133. Plugin::callHook('export_model_data', array(
  134. &$datasets, array(
  135. 'plugin' => $plugin,
  136. 'dataset' => $dataset,
  137. 'description' => true
  138. )));
  139. $current = reset($datasets);
  140. $data = $current['function']($parameters);
  141. $current['values'] = array();
  142. //Merge des données génériques erp et des données du dataset
  143. $allDataset = array_merge_recursive(
  144. ExportModel::get_standard_dataset($parameters),
  145. (!$data?array():$data)
  146. );
  147. $current['values'] = self::recursive_dataset_empty($allDataset);
  148. return $current;
  149. }
  150. public static function rawData($dataset){
  151. foreach ($dataset as $key => $value) {
  152. if(is_array($value) && array_key_exists('value', $value)){
  153. if(is_array($value['value'])) $value['value'] = self::rawData($value['value']);
  154. $dataset[$key] = (empty($value['value']) && (!is_array($value['value']) && !strlen($value['value'])) ) ? '' : $value['value'];
  155. }else{
  156. if(is_array($value)) $dataset[$key] = self::rawData($value);
  157. }
  158. }
  159. return $dataset;
  160. }
  161. public static function recursive_dataset_empty($set){
  162. $format = array();
  163. foreach ($set as $macro => $row) {
  164. if(is_array($row)){
  165. if(!empty($row['value']) && empty($row['type'])) $row['type'] = 'value';
  166. $row = self::recursive_dataset_empty($row);
  167. }
  168. if(isset($row['value']) && is_string($row['value'])){
  169. $row['value'] = '';
  170. }
  171. $format[$macro] = $row;
  172. }
  173. return $format;
  174. }
  175. //Export d'un set de donnée $datas dans le template $modelStream en fonction du type de document $type
  176. // ex : ExportModel::export('CsvExport', 'col1;{{name}}',array('name'=> array('value' => 'toto') ));
  177. public static function export($type, $modelStream, $datas, $parameters=array()){
  178. require_once(__DIR__.SLASH.'template'.SLASH.$type.'.class.php');
  179. $instance = new $type();
  180. if(method_exists($instance,'start')) $instance->start($modelStream,$datas, $parameters);
  181. $stream = $instance->from_template($modelStream, $datas);
  182. if(method_exists($instance,'end')) $stream = $instance->end($stream, $datas, $parameters);
  183. return $stream;
  184. }
  185. //convertis une entité en dataset
  186. public static function fromEntity($entity){
  187. $class = get_class($entity);
  188. $fields = $class::fields(false);
  189. $data = array('label'=>$class::entityLabel(), 'type'=>'object','value' => array());
  190. foreach($fields as $key=>$field){
  191. $value = $entity->$key;
  192. $type = FieldType::available($field['type']);
  193. if(property_exists($type, 'onLoad')){
  194. $method = $type->onLoad;
  195. $value = $method($value);
  196. }
  197. $data['value'][$key] = array('label' =>( isset($field['label'])?$field['label'] : $key),'value' => $value);
  198. }
  199. return $data;
  200. }
  201. }
  202. ?>