123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- global $_,$conf;
- switch($_['action']){
- /** PART **/
- case 'part_part_search':
- Action::write(function(&$response){
- global $myUser,$_;
- require_once(__DIR__.SLASH.'Part.class.php');
- foreach(Part::loadAll(array('state'=>Part::ACTIVE)) as $part){
-
- $row = $part->toArray();
- $row['picture'] = $part->picture(true);
- $response['rows'][] = $row;
- }
- });
- break;
- //Récuperation d'une liste de part
- case 'part_resource_part_search':
- Action::write(function(&$response){
- global $myUser,$_;
-
- require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
- require_once(__DIR__.SLASH.'Part.class.php');
- require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
- require_once(__DIR__.SLASH.'ResourcePart.class.php');
- $item = Resource::provide('resource',1);
- $sketch = $item->join('sketch');
- if(!$sketch->state && $sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'read') ) throw new Exception("Permissions insuffisantes",403);
-
- foreach(ResourcePart::loadAll(array('resource'=>$_['resource']), null, null, array('*'),1) as $resourcepart){
- $part = $resourcepart->join('part');
- $row = $part->toArray();
- $row['picture'] = $part->picture(true);
- $row['id'] = $resourcepart->id;
- $row['part'] = $part->id;
- $response['rows'][] = $row;
- }
- });
- break;
-
- //Ajout ou modification d'élément part
- case 'part_part_save':
- Action::write(function(&$response){
- global $myUser,$_;
- require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
- require_once(__DIR__.SLASH.'Part.class.php');
- require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
- require_once(__DIR__.SLASH.'ResourcePart.class.php');
-
- if(isset($_['resource'])){
- $item = Resource::provide('resource',1);
- $sketch = $item->join('sketch');
- if($sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'edit') ) throw new Exception("Permissions insuffisantes",403);
- }
- $part = Part::provide('part');
- $part->label = $_['label'];
- if(isset($_['price'])) $part->price = $_['price'];
- if(isset($_['link'])) $part->link = $_['link'];
- if(isset($_['brand'])) $part->brand = $_['brand'];
- $part->state = Part::ACTIVE;
- $part->save();
- if(isset($_['picture'])){
- $stream = base64_decode(preg_replace('|.*image/[^;]*;base64,|i','',$_['picture']));
- $dir = File::dir().'hackpoint'.SLASH.'part'.SLASH.$part->id;
- if(!file_exists($dir)) mkdir($dir,0755,true);
- file_put_contents($dir.SLASH.'cover.jpg', $stream);
- }
- if(isset($_['resource'])){
- $item = ResourcePart::provide();
- $item->part = $part->id;
- $item->resource = $_['resource'];
- $item->save();
- $response = $item->toArray();
- }
- });
- break;
-
-
- //Suppression d'élement part
- case 'part_resource_part_delete':
- Action::write(function(&$response){
- global $myUser,$_;
- //if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
- require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
- require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
- require_once(__DIR__.SLASH.'ResourcePart.class.php');
- require_once(__DIR__.SLASH.'Part.class.php');
- $resourcePart = ResourcePart::getById($_['id'],2);
- $resource = $resourcePart->join('resource');
- $sketch = $resource->join('sketch');
- if($sketch->creator!=$myUser->login && !$myUser->can('hackpoint',$sketch->id,'delete')) throw new Exception("Permissions insuffisantes",403);
- ResourcePart::deleteById($_['id']);
-
- });
- break;
- //Suppression d'élement part
- case 'part_part_delete':
- Action::write(function(&$response){
- global $myUser,$_;
- if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
- require_once(__DIR__.SLASH.'Part.class.php');
- Part::deleteById($_['id']);
-
- });
- break;
-
- case 'autocomplete_part':
- Action::write(function(&$response){
- require_once(__DIR__.SLASH.'Part.class.php');
-
- global $myUser,$_;
- if (!$myUser->connected()) throw new Exception("Error Processing Request", 1);
- new Exception("Vous devez être connecté!");
- $response['rows'] = array();
- $data = array("%".$_['keyword']."%",0);
- $parts = Part::staticQuery('SELECT * FROM {{table}} WHERE label LIKE ? AND state=? LIMIT 10',array("%".$_['keyword']."%",Part::ACTIVE),true);
- foreach($parts as $part){
- $response['rows'][] = array(
- 'name'=>html_entity_decode($part->label, ENT_QUOTES),
- 'id'=>$part->id,
- 'price'=>$part->price,
- 'brand'=>$part->brand,
- 'picture' => $part->picture(true)
- );
- }
-
- if(isset($_['data']) && isset($_['data']['before']) && isset($_['data']['before'])!=''){
- $list = json_decode(html_entity_decode($_['data']['before']),true);
- if(is_array($list)){
- foreach ($list as $key=>$value) {
- if(preg_match('/'.$_['keyword'].'/i', $value))
- array_unshift($response['rows'],array('name'=>$value,'id'=>$key));
- }
- }
- }
- });
- break;
- case 'get_part_by_id':
- Action::write(function(&$response){
- global $myUser,$_;
- require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
- require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
- require_once(__DIR__.SLASH.'Part.class.php');
- $part = Part::getById($_['id'],1);
- $part = !$part ? new Part() : Part::getById($_['id']);
-
- $row = $part->toArray();
- $row['label'] = html_entity_decode($row['label'], ENT_QUOTES);
- if(isset($_['before']) && isset($_['before'])!=''){
- $list = json_decode(html_entity_decode($_['before']),true);
- if(is_array($list)){
- if(isset($list[$_['id']])) $row = array('label' => $list[$_['id']], 'id'=>$_['id']);
- }
- }
- $response['part'] = $row;
- });
- break;
-
- //Sauvegarde des configurations de part
- case 'part_setting_save':
- Action::write(function(&$response){
- global $myUser,$_,$conf;
- User::check_access('part','configure');
- foreach(Configuration::setting('part') as $key=>$value){
- if(!is_array($value)) continue;
- $allowed[] = $key;
- }
- foreach ($_['fields'] as $key => $value) {
- if(in_array($key, $allowed))
- $conf->put($key,$value);
- }
- });
- break;
-
-
- }
- ?>
|