action.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 && !$myUser->can('hackpoint',$sketch->id,'read') ) 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. if(isset($_['resource'])){
  46. $item = Resource::provide('resource',1);
  47. $sketch = $item->join('sketch');
  48. if($sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'edit') ) throw new Exception("Permissions insuffisantes",403);
  49. }
  50. $part = Part::provide('part');
  51. $part->label = $_['label'];
  52. if(isset($_['price'])) $part->price = $_['price'];
  53. if(isset($_['link'])) $part->link = $_['link'];
  54. if(isset($_['brand'])) $part->brand = $_['brand'];
  55. $part->state = Part::ACTIVE;
  56. $part->save();
  57. if(isset($_['picture'])){
  58. $stream = base64_decode(preg_replace('|.*image/[^;]*;base64,|i','',$_['picture']));
  59. $dir = File::dir().'hackpoint'.SLASH.'part'.SLASH.$part->id;
  60. if(!file_exists($dir)) mkdir($dir,0755,true);
  61. file_put_contents($dir.SLASH.'cover.jpg', $stream);
  62. }
  63. if(isset($_['resource'])){
  64. $item = ResourcePart::provide();
  65. $item->part = $part->id;
  66. $item->resource = $_['resource'];
  67. $item->save();
  68. $response = $item->toArray();
  69. }
  70. });
  71. break;
  72. //Suppression d'élement part
  73. case 'part_resource_part_delete':
  74. Action::write(function(&$response){
  75. global $myUser,$_;
  76. //if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  77. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
  78. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
  79. require_once(__DIR__.SLASH.'ResourcePart.class.php');
  80. require_once(__DIR__.SLASH.'Part.class.php');
  81. $resourcePart = ResourcePart::getById($_['id'],2);
  82. $resource = $resourcePart->join('resource');
  83. $sketch = $resource->join('sketch');
  84. if($sketch->creator!=$myUser->login && !$myUser->can('hackpoint',$sketch->id,'delete')) throw new Exception("Permissions insuffisantes",403);
  85. ResourcePart::deleteById($_['id']);
  86. });
  87. break;
  88. //Suppression d'élement part
  89. case 'part_part_delete':
  90. Action::write(function(&$response){
  91. global $myUser,$_;
  92. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  93. require_once(__DIR__.SLASH.'Part.class.php');
  94. Part::deleteById($_['id']);
  95. });
  96. break;
  97. case 'autocomplete_part':
  98. Action::write(function(&$response){
  99. require_once(__DIR__.SLASH.'Part.class.php');
  100. global $myUser,$_;
  101. if (!$myUser->connected()) throw new Exception("Error Processing Request", 1);
  102. new Exception("Vous devez être connecté!");
  103. $response['rows'] = array();
  104. $data = array("%".$_['keyword']."%",0);
  105. $parts = Part::staticQuery('SELECT * FROM {{table}} WHERE label LIKE ? AND state=? LIMIT 10',array("%".$_['keyword']."%",Part::ACTIVE),true);
  106. foreach($parts as $part){
  107. $response['rows'][] = array(
  108. 'name'=>html_entity_decode($part->label, ENT_QUOTES),
  109. 'id'=>$part->id,
  110. 'price'=>$part->price,
  111. 'brand'=>$part->brand,
  112. 'picture' => $part->picture(true)
  113. );
  114. }
  115. if(isset($_['data']) && isset($_['data']['before']) && isset($_['data']['before'])!=''){
  116. $list = json_decode(html_entity_decode($_['data']['before']),true);
  117. if(is_array($list)){
  118. foreach ($list as $key=>$value) {
  119. if(preg_match('/'.$_['keyword'].'/i', $value))
  120. array_unshift($response['rows'],array('name'=>$value,'id'=>$key));
  121. }
  122. }
  123. }
  124. });
  125. break;
  126. case 'get_part_by_id':
  127. Action::write(function(&$response){
  128. global $myUser,$_;
  129. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
  130. require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
  131. require_once(__DIR__.SLASH.'Part.class.php');
  132. $part = Part::getById($_['id'],1);
  133. $part = !$part ? new Part() : Part::getById($_['id']);
  134. $row = $part->toArray();
  135. $row['label'] = html_entity_decode($row['label'], ENT_QUOTES);
  136. if(isset($_['before']) && isset($_['before'])!=''){
  137. $list = json_decode(html_entity_decode($_['before']),true);
  138. if(is_array($list)){
  139. if(isset($list[$_['id']])) $row = array('label' => $list[$_['id']], 'id'=>$_['id']);
  140. }
  141. }
  142. $response['part'] = $row;
  143. });
  144. break;
  145. //Sauvegarde des configurations de part
  146. case 'part_setting_save':
  147. Action::write(function(&$response){
  148. global $myUser,$_,$conf;
  149. User::check_access('part','configure');
  150. foreach(Configuration::setting('part') as $key=>$value){
  151. if(!is_array($value)) continue;
  152. $allowed[] = $key;
  153. }
  154. foreach ($_['fields'] as $key => $value) {
  155. if(in_array($key, $allowed))
  156. $conf->put($key,$value);
  157. }
  158. });
  159. break;
  160. }
  161. ?>