action.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /** DYNAMICFORM **/
  3. //Récuperation d'une liste de dynamicform
  4. Action::register('dynamicform_form_search',function(&$response){
  5. global $_,$myFirm;
  6. User::check_access('dynamicform','read');
  7. Plugin::need('dynamicform/DynamicForm');
  8. $dynamicforms = DynamicForm::loadAll(array('state'=>DynamicForm::ACTIVE,'firm:IN'=>array($myFirm->id,0)));
  9. foreach($dynamicforms as $dynamicform){
  10. $row = $dynamicform->toArray();
  11. $row['firmLegend'] = $row['firm'] == 0 ? 'Tous': Firm::getById($row['firm'])->label;
  12. $row['created'] = complete_date($row['created']).' à '.date('H:i',$row['created']);
  13. $response['rows'][] = $row;
  14. }
  15. });
  16. //Ajout ou modification d'élément dynamicform
  17. Action::register('dynamicform_form_save',function(&$response){
  18. global $_,$myFirm;
  19. User::check_access('dynamicform','edit');
  20. Plugin::need('dynamicform/DynamicForm,dynamicform/DynamicField');
  21. $item = DynamicForm::provide();
  22. $item->slug = slugify($_['slug']);
  23. $item->color = $_['color'];
  24. $item->firm = $_['allFirm'] ? 0 : $myFirm->id ;
  25. $item->state = DynamicForm::ACTIVE;
  26. $item->icon = $_['icon'];
  27. $item->label = $_['label'];
  28. if(empty($item->slug)) $item->slug = generateSlug($item->label,DynamicForm::class,'slug');
  29. $oldFields = array();
  30. if($item->id != 0){
  31. foreach (DynamicField::loadAll(array('form'=>$item->id)) as $key => $field) {
  32. $oldFields[$field->id] = $field;
  33. }
  34. }
  35. $item->save();
  36. $response = $item->toArray();
  37. if(!empty($_['rows'])){
  38. foreach ($_['rows'] as $i => $columns) {
  39. foreach ($columns as $u => $fields) {
  40. foreach ($fields as $o => $fieldData) {
  41. $field = new DynamicField();
  42. $field->fromArray($fieldData);
  43. $field->form = $item->id;
  44. $field->row = $i;
  45. $field->state = DynamicField::ACTIVE;
  46. if(empty($field->slug)){
  47. $field->slug = generateSlug($item->slug.'_'.$field->label, DynamicField::class, 'slug', '_');
  48. }else{
  49. $field->slug = slugify($field->slug);
  50. }
  51. if(count(DynamicField::loadAll(array('id:!='=>$field->id, 'slug'=>$field->slug, 'state'=>DynamicForm::ACTIVE))) > 0) throw new Exception('Slug de champ déjà utilisé');
  52. $field->sort = $o;
  53. $field->column = $u;
  54. $field->meta = isset($field->meta) && !empty($field->meta) ? base64_decode($field->meta) : null;
  55. if(isset($oldFields[$field->id])) unset($oldFields[$field->id]);
  56. $field->save();
  57. }
  58. }
  59. }
  60. }
  61. //supression des anciens champs non redéfinis
  62. foreach ($oldFields as $oldField)
  63. DynamicField::deleteById($oldField->id);
  64. });
  65. //Suppression d'élement dynamicform
  66. Action::register('dynamicform_form_delete',function(&$response){
  67. global $_;
  68. User::check_access('dynamicform','delete');
  69. Plugin::need('dynamicform/DynamicForm');
  70. $item = DynamicForm::getById($_['id']);
  71. $item->state = DynamicForm::INACTIVE;
  72. $item->save();
  73. });
  74. /** DYNAMICFIELD **/
  75. //Récuperation d'une liste de dynamicfield
  76. Action::register('dynamicform_field_search',function(&$response){
  77. global $_;
  78. User::check_access('dynamicform','read');
  79. Plugin::need('dynamicform/DynamicForm');
  80. $response['rows'] = DynamicForm::get_fields_layout(array('id'=>$_['form']));
  81. });
  82. //Sauvegarde des configurations de dynamicform
  83. Action::register('dynamicform_setting_save',function(&$response){
  84. global $_,$conf;
  85. User::check_access('dynamicform','configure');
  86. //Si input file "multiple", possibilité de normlaiser le
  87. //tableau $_FILES récupéré avec la fonction => normalize_php_files();
  88. foreach(Configuration::setting('dynamicform') as $key=>$value){
  89. if(!is_array($value)) continue;
  90. $allowed[] = $key;
  91. }
  92. foreach ($_['fields'] as $key => $value) {
  93. if(in_array($key, $allowed))
  94. $conf->put($key,$value);
  95. }
  96. });
  97. //Affichage des champs de saisie des metas de champs dynamiques
  98. Action::register('dynamicform_field_setting_change',function(&$response){
  99. global $_;
  100. User::check_access('dynamicform','read');
  101. $fieldtype = FieldType::available($_['type']);
  102. $html = '';
  103. if(property_exists($fieldtype, 'settings')){
  104. $types = FieldType::available();
  105. $settingsValues = isset($_['settings']) ? json_decode(base64_decode($_['settings']),true) : array();
  106. foreach($fieldtype->settings as $key=>$field){
  107. //$fieldtype->settings[$key]['value'] = '';
  108. if(!isset($settingsValues[$key])) continue;
  109. $fieldtype->settings[$key]['value'] = is_string($settingsValues[$key])?$settingsValues[$key]:json_encode($settingsValues[$key]);
  110. }
  111. $fields = FieldType::toForm($fieldtype->settings, $types);
  112. foreach($fields as $field){
  113. $html .= '<span '.(isset($field['data']['block-class']) ? "class=\"".$field['data']['block-class']."\"":'').'>';
  114. $html .= $field['label'];
  115. $html .= isset($field['data']['before']) ? $field['data']['before'] : '';
  116. $html .= $field['input'];
  117. $html .= isset($field['data']['after']) ? $field['data']['after'] : '';
  118. $html .= '</span>';
  119. }
  120. $response[] = $html;
  121. }
  122. });
  123. //Gestion des fichiers
  124. Action::register('dynamicform_handle_file',function(&$response){
  125. global $_;
  126. Plugin::need('dynamicform/DynamicField');
  127. //options de base pour le upload/delete/download
  128. $options = array(
  129. 'access' => 'document', // crud sur summary,
  130. 'size' => '10000000', // taille max
  131. 'limit' => 1,
  132. 'path'=>isset($_['path']) ? $_['path'] : '',
  133. 'namespace'=>isset($_['data']['scope']) ? $_['data']['scope'] : ''
  134. );
  135. //si search gestion des datas pour le path + settings champ dynamique
  136. if(isset($_['data']) && isset($_['data']['slug'])){
  137. $field = DynamicField::load(array('slug'=>$_['data']['slug']));
  138. $meta = isset($field) ? json_decode($field->meta,true) : array();
  139. $folder = 'tmp';
  140. if(isset($_['data']['scope']) && isset($_['data']['id']) && !empty($_['data']['scope']) && !empty($_['data']['id']))
  141. $folder = $_['data']['scope'].SLASH.$_['data']['id'];
  142. if(empty($meta['limit'])) $meta['limit'] = 1;
  143. if(isset($meta['storage']) && !empty($meta['storage']))
  144. $folder = $meta['storage'];
  145. $meta['storage'] = $folder;
  146. if($meta['limit']>1) $meta['storage'] .= SLASH.$field->slug;
  147. $meta['storage'] .= SLASH.'*';
  148. $options = array_merge($options,$meta);
  149. }
  150. File::handle_component($options,$response);
  151. });
  152. ?>