action.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. /** PART **/
  5. case 'part_part_search':
  6. Action::write(function(&$response){
  7. global $myUser,$_;
  8. require_once(__DIR__.SLASH.'Part.class.php');
  9. foreach(Part::loadAll(array('state'=>Part::ACTIVE)) as $part){
  10. $row = $part->toArray();
  11. $row['picture'] = $part->picture(true);
  12. $response['rows'][] = $row;
  13. }
  14. });
  15. break;
  16. //Récuperation d'une liste de part
  17. case 'part_resource_part_search':
  18. Action::write(function(&$response){
  19. global $myUser,$_;
  20. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
  21. require_once(__DIR__.SLASH.'Part.class.php');
  22. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
  23. require_once(__DIR__.SLASH.'ResourcePart.class.php');
  24. $item = Resource::provide('resource',1);
  25. $sketch = $item->join('sketch');
  26. if(!$sketch->state && $sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  27. foreach(ResourcePart::loadAll(array('resource'=>$_['resource']), null, null, array('*'),1) as $resourcepart){
  28. $part = $resourcepart->join('part');
  29. $row = $part->toArray();
  30. $row['picture'] = $part->picture(true);
  31. $row['id'] = $resourcepart->id;
  32. $row['part'] = $part->id;
  33. $response['rows'][] = $row;
  34. }
  35. });
  36. break;
  37. //Ajout ou modification d'élément part
  38. case 'part_part_save':
  39. Action::write(function(&$response){
  40. global $myUser,$_;
  41. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
  42. require_once(__DIR__.SLASH.'Part.class.php');
  43. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
  44. require_once(__DIR__.SLASH.'ResourcePart.class.php');
  45. $item = Resource::provide('resource',1);
  46. $sketch = $item->join('sketch');
  47. if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  48. $part = Part::provide('part');
  49. $part->label = $_['label'];
  50. if(isset($_['price'])) $part->price = $_['price'];
  51. if(isset($_['link'])) $part->link = $_['link'];
  52. if(isset($_['brand'])) $part->brand = $_['brand'];
  53. $part->state = Part::ACTIVE;
  54. $part->save();
  55. if(isset($_['picture'])){
  56. $stream = base64_decode(preg_replace('|.*image/[^;]*;base64,|i','',$_['picture']));
  57. $dir = File::dir().'hackpoint'.SLASH.'part'.SLASH.$part->id;
  58. if(!file_exists($dir)) mkdir($dir,0755,true);
  59. file_put_contents($dir.SLASH.'cover.jpg', $stream);
  60. }
  61. $item = ResourcePart::provide();
  62. $item->part = $part->id;
  63. $item->resource = $_['resource'];
  64. $item->save();
  65. $response = $item->toArray();
  66. });
  67. break;
  68. //Suppression d'élement part
  69. case 'part_resource_part_delete':
  70. Action::write(function(&$response){
  71. global $myUser,$_;
  72. //if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  73. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
  74. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
  75. require_once(__DIR__.SLASH.'ResourcePart.class.php');
  76. require_once(__DIR__.SLASH.'Part.class.php');
  77. $resourcePart = ResourcePart::getById($_['id'],2);
  78. $resource = $resourcePart->join('resource');
  79. $sketch = $resource->join('sketch');
  80. if($sketch->creator!=$myUser->login) throw new Exception("Permissions insuffisantes",403);
  81. ResourcePart::deleteById($_['id']);
  82. });
  83. break;
  84. //Suppression d'élement part
  85. case 'part_part_delete':
  86. Action::write(function(&$response){
  87. global $myUser,$_;
  88. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  89. require_once(__DIR__.SLASH.'Part.class.php');
  90. Part::deleteById($_['id']);
  91. });
  92. break;
  93. case 'autocomplete_part':
  94. Action::write(function(&$response){
  95. require_once(__DIR__.SLASH.'Part.class.php');
  96. global $myUser,$_;
  97. if (!$myUser->connected()) throw new Exception("Error Processing Request", 1);
  98. new Exception("Vous devez être connecté!");
  99. $response['rows'] = array();
  100. $data = array("%".$_['keyword']."%",0);
  101. $parts = Part::staticQuery('SELECT * FROM {{table}} WHERE label LIKE ? AND state=? LIMIT 10',array("%".$_['keyword']."%",Part::ACTIVE),true);
  102. foreach($parts as $part){
  103. $response['rows'][] = array(
  104. 'name'=>html_entity_decode($part->label, ENT_QUOTES),
  105. 'id'=>$part->id,
  106. 'price'=>$part->price,
  107. 'brand'=>$part->brand,
  108. 'picture' => $part->picture(true)
  109. );
  110. }
  111. if(isset($_['data']) && isset($_['data']['before']) && isset($_['data']['before'])!=''){
  112. $list = json_decode(html_entity_decode($_['data']['before']),true);
  113. if(is_array($list)){
  114. foreach ($list as $key=>$value) {
  115. if(preg_match('/'.$_['keyword'].'/i', $value))
  116. array_unshift($response['rows'],array('name'=>$value,'id'=>$key));
  117. }
  118. }
  119. }
  120. });
  121. break;
  122. case 'get_part_by_id':
  123. Action::write(function(&$response){
  124. global $myUser,$_;
  125. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
  126. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
  127. require_once(__DIR__.SLASH.'Part.class.php');
  128. $part = Part::getById($_['id'],1);
  129. $part = !$part ? new Part() : Part::getById($_['id']);
  130. $row = $part->toArray();
  131. $row['label'] = html_entity_decode($row['label'], ENT_QUOTES);
  132. if(isset($_['before']) && isset($_['before'])!=''){
  133. $list = json_decode(html_entity_decode($_['before']),true);
  134. if(is_array($list)){
  135. if(isset($list[$_['id']])) $row = array('label' => $list[$_['id']], 'id'=>$_['id']);
  136. }
  137. }
  138. $response['part'] = $row;
  139. });
  140. break;
  141. //Sauvegarde des configurations de part
  142. case 'part_setting_save':
  143. Action::write(function(&$response){
  144. global $myUser,$_,$conf;
  145. User::check_access('part','configure');
  146. foreach(Configuration::setting('part') as $key=>$value){
  147. if(!is_array($value)) continue;
  148. $allowed[] = $key;
  149. }
  150. foreach ($_['fields'] as $key => $value) {
  151. if(in_array($key, $allowed))
  152. $conf->put($key,$value);
  153. }
  154. });
  155. break;
  156. }
  157. ?>