StatisticReport.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. @nom: StatisticReport
  4. @auteur: Idleman (idleman@idleman.fr)
  5. @description: Modele de classe pour les plugins
  6. */
  7. //Ce fichier permet de gérer vos données en provenance de la base de données
  8. class StatisticReport extends Entity{
  9. public $id ,$label,$color,$icon;
  10. public $TABLE_NAME = 'statistic_report';
  11. public $fields = array(
  12. 'id'=>'key',
  13. 'label'=>'string',
  14. 'color'=>'string',
  15. 'icon'=>'string'
  16. );
  17. public function widgets(){
  18. require_once(__DIR__.SLASH.'Widget.class.php');
  19. return Widget::loadAll(array('report'=>$this->id));
  20. }
  21. public function remove(){
  22. require_once(__DIR__.SLASH.'Filter.class.php');
  23. require_once(__DIR__.SLASH.'Widget.class.php');
  24. self::deleteById($this->id);
  25. Filter::delete(array('report'=>$this->id));
  26. foreach (Widget::loadAll(array('report'=>$this->id)) as $widget) {
  27. $widget->remove();
  28. }
  29. }
  30. public static function import($zipFile){
  31. require_once(__DIR__.SLASH.'Filter.class.php');
  32. require_once(__DIR__.SLASH.'Connection.class.php');
  33. require_once(__DIR__.SLASH.'Widget.class.php');
  34. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  35. $zip = new ZipArchive();
  36. if(!$zip->open($zipFile)) throw new Exception("Impossible d'ouvrir l'import");
  37. $report = new self();
  38. $report->fromArray(json_decode($zip->getFromName('report.json'),true));
  39. unset($report->id);
  40. $report->save();
  41. $filters = $zip->getFromName('filters.json');
  42. if($filters!=false){
  43. foreach(json_decode($filters,true) as $line){
  44. $filter = new Filter();
  45. $filter->fromArray($line);
  46. unset($filter->id);
  47. $filter->report=$report->id;
  48. $filter->save();
  49. }
  50. }
  51. $connections = $zip->getFromName('connections.json');
  52. $connectionsMapping = array();
  53. if($connections!=false){
  54. foreach(json_decode($connections,true) as $line){
  55. $connection = new Connection();
  56. $connection->fromArray($line);
  57. $oldId = $connection->id;
  58. unset($connection->id);
  59. $existing = Connection::load(array('label'=>$connection->label));
  60. if($existing) $connection = $existing;
  61. $connection->save();
  62. $connectionsMapping[$oldId] = $connection->id;
  63. }
  64. }
  65. for( $i = 0; $i < $zip->numFiles; $i++ ){
  66. $stat = $zip->statIndex( $i );
  67. if(!preg_match('/([0-9]*)\/widget\.json/i', $stat['name'],$match)) continue;
  68. $folder = $match[1];
  69. $widget = new Widget();
  70. $widget->fromArray(json_decode($zip->getFromName($match[0]),true));
  71. unset($widget->id);
  72. $widget->report = $report->id;
  73. $widget->save();
  74. for( $u = 0; $u < $zip->numFiles; $u++ ){
  75. $stat2 = $zip->statIndex( $u );
  76. if(!preg_match('/'.$folder.'\/element\/.*/i', $stat2['name'],$match2)) continue;
  77. $elementJson = json_decode($zip->getFromName($match2[0]),true);
  78. $type = WidgetElement::getType($elementJson['type']);
  79. $element = new $type();
  80. $element->fromArray($elementJson);
  81. if(isset($element->connection)) $element->connection = $connectionsMapping[$element->connection];
  82. unset($element->id);
  83. $element->widget = $widget->id;
  84. $element->save();
  85. }
  86. }
  87. $zip->close();
  88. }
  89. public function export(){
  90. require_once(__DIR__.SLASH.'Filter.class.php');
  91. require_once(__DIR__.SLASH.'Connection.class.php');
  92. $zip = new ZipArchive();
  93. $file = str_replace('\\\\','\\',File::temp().mt_rand(0,1000).time().'.zip');
  94. if ($zip->open($file, ZipArchive::CREATE)!==TRUE) throw new Exception("Impossible de créer le zip ".$file);
  95. $zip->addFromString('report.json', json_encode($this->toArray(),JSON_PRETTY_PRINT));
  96. $filters = array();
  97. foreach (Filter::loadAll(array('report'=>$this->id),array('sort')) as $filter) {
  98. $filters[] = $filter->toArray();
  99. }
  100. $zip->addFromString('filters.json', json_encode($filters,JSON_PRETTY_PRINT));
  101. $connections = array();
  102. foreach ($this->widgets() as $widget) {
  103. $zip->addFromString($widget->id.'/widget.json', json_encode($widget->toArray(),JSON_PRETTY_PRINT));
  104. foreach ($widget->elements() as $i=>$element) {
  105. if(!empty($element->connection)){
  106. $connection = Connection::getById($element->connection);
  107. $connections[$connection->id] = $connection;
  108. }
  109. $zip->addFromString($widget->id.'/element/'.$i.'.json', json_encode(array_merge($element->toArray(),array('type'=>get_class($element))),JSON_PRETTY_PRINT));
  110. }
  111. }
  112. $connectionJson = array();
  113. foreach ($connections as $connection) {
  114. $connectionJson[$connection->id] = $connection->toArray();
  115. }
  116. if(!empty($connectionJson))
  117. $zip->addFromString('connections.json', json_encode($connectionJson,JSON_PRETTY_PRINT));
  118. $zip->close();
  119. $stream = file_get_contents($file);
  120. unlink($file);
  121. return $stream;
  122. }
  123. }
  124. ?>