FieldType.class.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. <?php
  2. /**
  3. * Type de champs possibles dans l'application
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license MIT
  7. */
  8. class FieldType{
  9. public $label,$slug,$sqlType,$description,$icon,$default,$input;
  10. public function toArray(){
  11. return json_decode(json_encode($this), true);
  12. }
  13. public function __toString() {
  14. return json_encode($this->toArray());
  15. }
  16. //traduit une valeur en champ/label/légende HTML en fonction de son type
  17. public static function toHtml($field,$types=null,$options = array('allowCustomLabel'=>true)){
  18. if(!isset($types)) $types = self::available();
  19. if(!isset($field['id'])) $field['id'] = '';
  20. $type = isset($field['type']) && isset($types[$field['type']]) ? $types[$field['type']] : $types['text'];
  21. $label = '';
  22. if(!empty($field['label'])){
  23. $label = '<label class="mb-0"';
  24. if(!empty($field['id'])) $label .= 'for="'.$field['id'].'"';
  25. $label .= '>'.$field['label'].'</label>';
  26. }
  27. if(!isset($field['attributes'])) $field['attributes'] = array();
  28. $field['attributes'] = array_merge($type->default_attributes,$field['attributes']);
  29. if(empty($field['value']) && !empty($field['default'])) $field['value'] = $field['default'];
  30. if(!empty($field['placeholder'])) $field['attributes']['placeholder'] = $field['placeholder'];
  31. if(!empty($field['required'])) $field['attributes']['required'] = $field['required'];
  32. $func = $type->onInput;
  33. $inputOptions = array_merge(array('type'=>$type,'label'=>$label), $options);
  34. $input = $func($field,$inputOptions);
  35. return array(
  36. 'label' => isset($type->customLabel) && $type->customLabel && $options['allowCustomLabel'] ? '' : $label,
  37. 'legend' => isset($field['legend']) ? $field['legend'] : '',
  38. 'input' => $input,
  39. 'data' => $field
  40. );
  41. }
  42. public static function toForm($fields,$types=null){
  43. $htmlFields = array();
  44. if(!isset($types)) $types = self::available();
  45. uasort ($fields,function($a,$b){
  46. if(!isset($a['sort'])) $a['sort'] = 100;
  47. if(!isset($b['sort'])) $b['sort'] = 100;
  48. return $a['sort'] - $b['sort'];
  49. });
  50. foreach ($fields as $key => $field) {
  51. if(!isset($field['id'])) $field['id'] = $key;
  52. $htmlFields[] = self::toHtml($field,$types);
  53. }
  54. return $htmlFields;
  55. }
  56. //liste des types de champs disponibles
  57. public static function available($key=null){
  58. $types = array();
  59. //Texte
  60. $type_text = new self();
  61. $type_text->slug = 'text';
  62. $type_text->label = 'Texte';
  63. $type_text->sqlType = 'string';
  64. $type_text->label = 'Texte';
  65. $type_text->icon = 'fas fa-font';
  66. $type_text->description = 'Texte court mono ligne';
  67. $type_text->default = '';
  68. $type_text->default_attributes = array(
  69. 'class'=>'"form-control"',
  70. 'type'=>'"text"'
  71. );
  72. $type_text->filter = array(
  73. 'operators' => array (
  74. 'like' =>array("view" =>'text'),
  75. 'not like' =>array("view" =>'text'),
  76. '=' =>array("view" =>'text'),
  77. '!=' =>array("view" =>'text'),
  78. 'null' =>array(),
  79. 'not null' =>array()
  80. )
  81. );
  82. /* Exemples d'utilisation des méthodes de convertion de types
  83. Type | BDD | onLoad | onRawDisplay | onHtmlDisplay | onInput | fromRawDisplay
  84. ====================================================================================================================================================================
  85. Date | 12345 | d/m/y | d/m/y | d/m/y | <input data-type="date" ... value="{{onLoad}}"> | 12345
  86. User | theo.lecoq | theo.lecoq | Théo Lecoq | Théo Lecoq | <input data-type="user" ... value="{{onLoad}}"> | theo.lecoq
  87. Dictionary | 6 | 6 | Véhicules > bateau | Véhicules > bateau | <input data-type="dictionary" ... value="{{onLoad}}"> | 6
  88. password | ==x$yh.. | toto | toto | toto | <input data-type="password" ... value="{{onLoad}}"> | ==x$yh..
  89. url | http://... | http://... | http://... | <a href="http://..."></a> | <input data-type="url" ... value="{{onLoad}}"> | http://..
  90. onLoad : Utilisé pour converti une valeur de la base en une valeur comprehensible par l'erp et
  91. ses composants (pas forcement human readable)
  92. onRawDisplay : Valeur human readable brute sans mise en forme ou html, utilisé par exemple dans les export text/ excel
  93. fromRawDisplay : utilisé pour convertir la valeur affichée brute en valeur technique BDD (inverse de onRawDisplay) utilisé apr exemple pour les import depuis excel
  94. onHtmlDisplay : Valeur human readable avec mise en forme ou html, utilisé pour affichage sur les pages, un parametre decoration peut être mise a true pour avoir des décoration contextuelles (ex : une icone de calendrier devant la date)
  95. onInput : utilisé pour afficher l'input approprié, cette méthode doit s'appuyer sur le onLoad
  96. onSave : utilisé pour une transformation de la valeur avant enregistrement en base
  97. */
  98. $type_text->onLoad = function($value,$options = array()){
  99. return $value;
  100. };
  101. $type_text->onRawDisplay = function($value,$options = array()){
  102. return $value;
  103. };
  104. $type_text->onHtmlDisplay = function($value,$options = array()){
  105. $raw = $options['type']->onRawDisplay;
  106. $html = $raw($value,$options);
  107. return $html;
  108. };
  109. $type_text->onInput = function($field=array(),$options=array()){
  110. $attributes = array_merge($options['type']->default_attributes,$field['attributes']);
  111. if(isset($field['id'])) $field['attributes']['id'] ='"'.$field['id'].'"';
  112. $html = '';
  113. $html.= '<input value="'.(isset($field['value']) ? str_replace('"','&quot;',$field['value']) : '').'"';
  114. foreach ($field['attributes'] as $key => $value) {
  115. $html.= ' ';
  116. $html.= $key;
  117. if(!empty($value) && is_string($value)) $html.= '='.$value;
  118. $html.= ' ';
  119. }
  120. $html.= '>';
  121. return $html;
  122. };
  123. $types[$type_text->slug] = $type_text;
  124. //File
  125. $type_file = new self();
  126. $type_file->slug = 'file';
  127. $type_file->label = 'Fichier';
  128. $type_file->sqlType = '';
  129. $type_file->default_attributes = array_merge($type_text->default_attributes,array(
  130. 'class'=>'"component-file-default bg-white shadow-sm rounded-sm"',
  131. 'data-type'=>'"file"',
  132. 'data-extension'=>'"jpg,png,bmp,jpeg,gif,svg,webp,docx,xlsx,pptx,msg,eml,pdf,zip,doc,xls,ppt,txt,csv,mp3,wav,mp4,avi,flv"',
  133. 'data-action'=>'{{action}}',
  134. 'data-id'=>'"'.uniqid(rand(0,100)).'"',
  135. 'data-data'=> "'{}'",
  136. ));
  137. $type_file->settings = array(
  138. 'action'=>array('type'=>'text','default'=>"dynamicform_handle_file",'attributes'=> array('class'=>'"form-control hidden"','disabled'=>true)),
  139. 'extension'=> array('type'=>'text','label'=>'Extensions permises','placeholder'=>'"Séparation par \',\'"','attributes'=> array('class'=>'"form-control"')),
  140. 'size'=> array('type'=>'integer','label'=>'Taille max (octets)','attributes'=> array('class'=>'"form-control"')),
  141. 'limit'=> array('type'=>'integer','label'=>'Nombre max de fichiers','attributes'=> array('class'=>'"form-control"')),
  142. 'storage'=> array('type'=>'filepicker','label'=>'Dossier d\'enregistrement','attributes'=> array('class'=>'"form-control"','data-editable'=>true)),
  143. 'access'=> array(
  144. 'type'=>'list',
  145. 'label'=>'Contrôle des droits et accès à faire sur la section',
  146. 'values'=>function(){
  147. $scopes = array();
  148. foreach(Right::availables() as $slug=>$scope)
  149. $scopes[$slug] = $scope['label'];
  150. return $scopes;
  151. },
  152. 'attributes'=> array('class'=>'"form-control"')
  153. ),
  154. /*'label'=> array('type'=>'text','label'=>'Libellé visible dans la dropzone du composant','placeholder'=>'"Faites glisser vos fichiers ici"','attributes'=> array('class'=>'"form-control"')),
  155. 'readonly'=> array('type'=>'boolean','label'=>'Lecture seule','attributes'=> array('class'=>'"form-control"')),*/
  156. );
  157. $type_file->onInput = function($field=array(),$options=array()){
  158. $attributes = array_merge($options['type']->default_attributes,$field['attributes']);
  159. if(isset($field['id'])) $field['attributes']['id'] ='"'.$field['id'].'"';
  160. //gestion des scopes, uid et slug si dynamics
  161. $data = array();
  162. //id entité si présente
  163. $data['id'] = isset($field['options']['uid']) ? $field['options']['uid'] : '';
  164. //id scope de l'entité si présente
  165. $data['scope'] = isset($field['options']['scope']) ? $field['options']['scope'] : '';
  166. //slug du composant
  167. $data['slug'] = isset($field['slug']) ? $field['slug'] : '';
  168. //Gestion des data-data depuis le champ (ex: type image en settings globaux)
  169. if(!empty($field['attributes']) && !empty($field['attributes']['data-data'])){
  170. $fieldData = json_decode($field['attributes']['data-data'], true);
  171. $data = !empty($fieldData) ? json_encode(array_merge($data, $fieldData)) : json_encode($data);
  172. }
  173. $field['attributes']['data-data'] = $data;
  174. $html = '';
  175. $html.= '<input ';
  176. foreach ($field['attributes'] as $key => $value) {
  177. $html.= ' ';
  178. $html.= $key;
  179. if(!empty($value)) $html.= '='.$value;
  180. $html.= ' ';
  181. }
  182. $html.= '>';
  183. return $html;
  184. };
  185. $type_file->onSave = function($value,$options=array()){
  186. $path = $options['options']['scope'].SLASH.$options['options']['uid'];
  187. $limit = 1;
  188. if(!empty($options['field']->meta)){
  189. $meta = json_decode($options['field']->meta,true);
  190. if(!empty($meta['limit']) && $meta['limit']>1) $limit =$meta['limit'] ;
  191. if(!empty($meta['storage']))
  192. $path = template($meta['storage'],$options['options'],true);
  193. }
  194. File::save_component($options['field']->slug,$path.($limit==1?'':SLASH.$options['field']->slug).SLASH.'{{label}}');
  195. return null;
  196. };
  197. $type_file->onRawDisplay = function($value,$options = array()){
  198. $value = '';
  199. $meta = json_decode($options['meta'],true);
  200. $templateData = array();
  201. foreach($options as $key=>$option){
  202. if(!is_string($option)) continue;
  203. $templateData[$key] = $option;
  204. }
  205. $path = File::dir().template($meta['storage'],$templateData,true).'/*';
  206. foreach (glob($path) as $file) {
  207. $filePath = str_replace(File::dir(),'',$file);
  208. $value .= PHP_EOL.basename($file).' ('.ROOT_URL.'action.php?action='.$meta['action'].'&type=download&path='.base64_encode($filePath).')';
  209. }
  210. return $value;
  211. };
  212. $type_file->onHtmlDisplay = function($value,$options = array()){
  213. $html = '';
  214. $meta = json_decode($options['meta'],true);
  215. $templateData = array();
  216. foreach($options as $key=>$option){
  217. if(!is_string($option)) continue;
  218. $templateData[$key] = $option;
  219. }
  220. $path = File::dir().template($meta['storage'],$templateData,true).'/*';
  221. $html .= '<ul class="list-group list-group-flush shadow-sm">';
  222. foreach (glob($path) as $file) {
  223. $filePath = str_replace(File::dir(),'',$file);
  224. $html .= '<li class="list-group-item p-1"><a href="';
  225. $html .= 'action.php?action='.$meta['action'].'&type=download&path='.base64_encode($filePath);
  226. $html.= '">'.basename($file).'<a></li>';
  227. }
  228. $html.='</ul>';
  229. return $html;
  230. };
  231. $type_file->icon = 'fas fa-file-upload';
  232. $type_file->description = 'Envoi de fichier';
  233. $type_file->default = '';
  234. $types[$type_file->slug] = $type_file;
  235. //Image
  236. $type = new self();
  237. $type->slug = 'image';
  238. $type->label = 'Image';
  239. $type->sqlType = '';
  240. $type->default_attributes = array_merge($type_text->default_attributes,array(
  241. 'class'=>'"component-file-cover bg-white shadow-sm rounded-sm"',
  242. 'data-type'=>'"file"',
  243. 'data-limit'=>'"1"',
  244. 'data-extension'=>'"jpg,png,bmp,jpeg,gif,svg,webp"',
  245. 'data-action'=>'{{action}}',
  246. 'data-id'=>'"'.uniqid(rand(0,100)).'"',
  247. 'data-data'=> "'{}'" ,
  248. ));
  249. $type->settings = array(
  250. 'action'=>array('type'=>'text','default'=>"dynamicform_handle_file",'attributes'=> array('class'=>'"form-control hidden"','disabled'=>true)),
  251. 'extension'=> array('type'=>'text','label'=>'Extensions permises','placeholder'=>'"Séparation par \',\'"','attributes'=> array('class'=>'"form-control"')),
  252. 'size'=> array('type'=>'integer','label'=>'Taille max (octets)','attributes'=> array('class'=>'"form-control"')),
  253. 'storage'=> array('type'=>'filepicker','label'=>'Dossier d\'enregistrement','attributes'=> array('class'=>'"form-control"','data-editable'=>true)),
  254. 'access'=> array(
  255. 'type'=>'list',
  256. 'label'=>'Contrôle des droits et accès à faire sur la section',
  257. 'values'=>function(){
  258. $scopes = array();
  259. foreach(Right::availables() as $slug=>$scope)
  260. $scopes[$slug] = $scope['label'];
  261. return $scopes;
  262. },
  263. 'attributes'=> array('class'=>'"form-control"')
  264. ),
  265. /*'label'=> array('type'=>'text','label'=>'Libellé visible dans la dropzone du composant','placeholder'=>'"Faites glisser vos fichiers ici"','attributes'=> array('class'=>'"form-control"')),
  266. 'readonly'=> array('type'=>'boolean','label'=>'Lecture seule','attributes'=> array('class'=>'"form-control"')),*/
  267. );
  268. $type->onInput = $type_file->onInput;
  269. $type->onSave = $type_file->onSave;
  270. $type->onHtmlDisplay = function($value,$options = array()){
  271. $html = '<div class="fieldtype-image-cover">';
  272. $meta = json_decode($options['meta'],true);
  273. $templateData = array();
  274. foreach($options as $key=>$option){
  275. if(!is_string($option)) continue;
  276. $templateData[$key] = $option;
  277. }
  278. $path = File::dir().template($meta['storage'],$templateData,true).'/*';
  279. $files = glob($path);
  280. $file = $files[0];
  281. $filePath = str_replace(File::dir(),'',$file);
  282. $action = 'action.php?action='.$meta['action'].'&type=download&path='.base64_encode($filePath);
  283. $html .= '<img class="shadow-sm rounded-sm" style="max-width: 100px;height: auto;" src="'.$action.'">';
  284. if(count($files)>1) $html .= '<span><i class="far fa-file-image"></i> +'.count($files).' </span>';
  285. $html .= '</div>';
  286. return $html;
  287. };
  288. $type->onRawDisplay = $type_file->onRawDisplay;
  289. $type->icon = 'far fa-image';
  290. $type->description = "Envoi d'image";
  291. $type->default = '';
  292. $types[$type->slug] = $type;
  293. //User
  294. $type = new self();
  295. $type->slug = 'user';
  296. $type->label = 'Utilisateur';
  297. $type->sqlType = 'string';
  298. $type->default_attributes = array_merge($type_text->default_attributes,array(
  299. 'data-type'=>'"user"',
  300. ));
  301. $type->filter = array(
  302. 'operators' => array (
  303. '=' =>array("view" =>'user'),
  304. '!=' =>array("view" =>'user'),
  305. 'like' =>array("view" =>'user'),
  306. 'not like' =>array("view" =>'user'),
  307. 'null' =>array(),
  308. 'not null' =>array()
  309. )
  310. );
  311. $type->onInput = $type_text->onInput;
  312. $type->onRawDisplay = function($value,$options = array()){
  313. if(empty($value)) return '';
  314. $user = User::byLogin($value);
  315. return !$user? '':$user->fullName();
  316. };
  317. $type->onHtmlDisplay = function($value,$options = array()){
  318. $raw = $options['type']->onRawDisplay;
  319. $html = $raw($value,$options);
  320. if(isset($options['decoration']) && $options['decoration']) $html = '<img src="action.php?action=core_account_avatar_download&amp;user='.$value.'&amp;extension=jpg" class="avatar-mini avatar-rounded avatar-login" title="'.$html.'"> '.$html;
  321. return $html;
  322. };
  323. $type->icon = 'fas fa-user';
  324. $type->description = 'Autocompletion sur les comptes utilisateurs';
  325. $type->default = '';
  326. $types[$type->slug] = $type;
  327. //Firm
  328. $type = new self();
  329. $type->slug = 'firm';
  330. $type->label = 'Etablissement';
  331. $type->sqlType = 'string';
  332. $type->default_attributes = array_merge($type_text->default_attributes,array(
  333. 'data-type'=>'"firm"',
  334. ));
  335. $type->onInput = $type_text->onInput;
  336. $type->icon = 'fas fa-building';
  337. $type->description = 'Autocompletion sur les établissements';
  338. $type->default = '';
  339. $type->onRawDisplay = function($value,$options = array()){
  340. if(empty($value) || !is_numeric($value)) return '';
  341. $firm = Firm::getById($value);
  342. return !$firm? '':$firm->label;
  343. };
  344. $type->filter = array(
  345. 'operators' => array (
  346. '=' =>array("view" =>'firm'),
  347. '!=' =>array("view" =>'firm'),
  348. 'null' =>array(),
  349. 'not null' =>array()
  350. )
  351. );
  352. $types[$type->slug] = $type;
  353. //Rank
  354. $type = new self();
  355. $type->slug = 'rank';
  356. $type->label = 'Rang';
  357. $type->sqlType = 'int';
  358. $type->default_attributes = array_merge($type_text->default_attributes,array(
  359. 'data-type'=>'"user"',
  360. 'data-types'=>'"rank"',
  361. ));
  362. $type->onInput = $type_text->onInput;
  363. $type->icon = 'fas fa-user-lock';
  364. $type->description = 'Autocompletion sur les rangs';
  365. $type->default = '';
  366. $type->onRawDisplay = function($value,$options = array()){
  367. if(empty($value) || !is_numeric($value)) return '';
  368. $rank = Rank::getById($value);
  369. return !$rank? '':$rank->label;
  370. };
  371. $type->filter = array(
  372. 'operators' => array (
  373. '=' =>array("view" =>'rank'),
  374. '!=' =>array("view" =>'rank'),
  375. 'null' =>array(),
  376. 'not null' =>array()
  377. )
  378. );
  379. $types[$type->slug] = $type;
  380. //Textarea
  381. $type_longstring = new self();
  382. $type_longstring->slug = 'textarea';
  383. $type_longstring->label = 'Texte Long';
  384. $type_longstring->sqlType = 'longstring';
  385. $type_longstring->default_attributes = $type_text->default_attributes;
  386. $type_longstring->onInput = function($field=array(),$options=array()){
  387. $html = '';
  388. $html .= '<textarea ';
  389. if(isset($field['id'])) $field['attributes']['id'] ='"'.$field['id'].'"';
  390. foreach ($field['attributes'] as $key=>$attribute) {
  391. if( $key=='value' ) continue;
  392. $html .= ' '.$key;
  393. if(!empty($attribute))
  394. $html .= '='.$attribute;
  395. }
  396. if(isset($field['value']) && is_array($field['value']))
  397. $field['value'] = json_encode($field['value']);
  398. $html .='>'.(isset($field['value']) && !empty($field['value']) ? (is_callable($field['value']) ? $field['value']() : $field['value']) : '').'</textarea>';
  399. return $html ;
  400. };
  401. $type_longstring->filter = array(
  402. 'operators' => array (
  403. 'like' =>array("view" =>'text'),
  404. 'not like' =>array("view" =>'text'),
  405. '=' =>array("view" =>'text'),
  406. '!=' =>array("view" =>'text'),
  407. 'null' =>array(),
  408. 'not null' =>array()
  409. )
  410. );
  411. $type_longstring->icon = 'fas fa-text-width';
  412. $type_longstring->description = 'Texte long multi ligne';
  413. $type_longstring->default = '';
  414. $types[$type_longstring->slug] = $type_longstring;
  415. //Wysiwyg
  416. $type = new self();
  417. $type->slug = 'wysiwyg';
  418. $type->label = 'Texte enrichi';
  419. $type->sqlType = 'longstring';
  420. $type->default_attributes = array_merge($type_longstring->default_attributes,array(
  421. 'data-type'=>'"wysiwyg"',
  422. 'class'=>'""',
  423. ));
  424. $type->onInput = $type_longstring->onInput;
  425. $type->onRawDisplay = function($value,$options = array()){
  426. if(empty($value)) return '';
  427. return strip_tags($value);
  428. };
  429. $type->onHtmlDisplay = function($value,$options = array()){
  430. $html = html_entity_decode($value);
  431. return $html;
  432. };
  433. $type->filter = $type_longstring->filter;
  434. $type->icon = 'fas fa-spell-check';
  435. $type->description = 'Texte riche multi ligne';
  436. $type->default = '';
  437. $types[$type->slug] = $type;
  438. //Date
  439. $type = new self();
  440. $type->slug = 'date';
  441. $type->label = 'Date';
  442. $type->sqlType = 'date';
  443. $type->default_attributes = array_merge($type_text->default_attributes,array(
  444. 'data-type'=>'"date"',
  445. 'title'=>'"format jj/mm/aaaa"',
  446. 'placeholder'=>'"JJ/MM/AAAA"',
  447. ));
  448. $type->onLoad = function($value){
  449. return !is_null($value) || is_numeric($value) ? date('d/m/Y',$value) : '';
  450. };
  451. $type->onSave = function($value,$options=array()){
  452. return !empty($value) ? timestamp_date($value) : null;
  453. };
  454. $type->onRawDisplay = function($value,$options = array()){
  455. $html = !is_null($value) && is_numeric($value) ? date('d/m/Y',$value) : '';
  456. return $html;
  457. };
  458. $type->fromRawDisplay = function($value,$options = array()){
  459. return timestamp_date($value);
  460. };
  461. $type->filter = array(
  462. 'operators' => array (
  463. 'between' =>array("view" =>'date'),
  464. '=' =>array("view" =>'date'),
  465. '!=' =>array("view" =>'date'),
  466. 'null' =>array(),
  467. 'not null' =>array()
  468. )
  469. );
  470. $type->onInput = $type_text->onInput;
  471. $type->icon = 'far fa-calendar';
  472. $type->description = 'Date au format jj/mm/aaaa';
  473. $type->default = '';
  474. $types[$type->slug] = $type;
  475. //Tags
  476. $type = new self();
  477. $type->slug = 'tag';
  478. $type->label = 'Etiquettes';
  479. $type->sqlType = 'string';
  480. $type->default_attributes = array_merge($type_text->default_attributes,array(
  481. 'data-type'=>'"tag"',
  482. 'data-multiple'=>'true'
  483. ));
  484. $type->onInput = $type_text->onInput;
  485. $type->icon = 'fas fa-tags';
  486. $type->description = 'Etiquettes (tags) séparés par virgules, espace ou tabulation';
  487. $type->default = '';
  488. $type->onHtmlDisplay = function($value,$options = array()){
  489. $html = '';
  490. $tags = explode(',',$value);
  491. $html = '<span class="badge">'.implode('</span><span class="badge">',$tags).'</span>';
  492. return $html;
  493. };
  494. $type->filter = array(
  495. 'operators' => array (
  496. 'in' =>array("view" =>'tag'),
  497. 'not in' =>array("view" =>'tag'),
  498. 'inline-and' =>array("view" =>'tag'),
  499. 'inline-or' =>array("view" =>'tag'),
  500. 'null' =>array(),
  501. 'not null' =>array()
  502. )
  503. );
  504. $type->onSave = function($value,$options=array()){
  505. $value = explode(',',$value);
  506. $value = array_filter($value);
  507. $value = ','.implode(',',$value).',';
  508. if($value==',,') $value = '';
  509. return $value;
  510. };
  511. $types[$type->slug] = $type;
  512. //File picker
  513. $type = new self();
  514. $type->slug = 'filepicker';
  515. $type->label = 'Parcourir';
  516. $type->sqlType = 'longstring';
  517. $type->default_attributes = array_merge($type_text->default_attributes,array(
  518. 'data-type'=>'"filepicker"',
  519. ));
  520. $type->onInput = $type_text->onInput;
  521. $type->icon = 'far fa-folder-open';
  522. $type->description = 'Rechercher dans un dossier';
  523. $type->default = '';
  524. $type->settings = array(
  525. 'root'=> array(
  526. 'type'=>'filepicker',
  527. 'label'=>'Racine ciblée',
  528. 'attributes'=> array('class'=>'"form-control"', 'data-root'=>"")
  529. )
  530. );
  531. $type->filter = array(
  532. 'operators' => array (
  533. '=' =>array("view" =>'filepicker'),
  534. '!=' =>array("view" =>'filepicker'),
  535. 'null' =>array(),
  536. 'not null' =>array()
  537. )
  538. );
  539. $types[$type->slug] = $type;
  540. //Heure
  541. $type = new self();
  542. $type->slug = 'hour';
  543. $type->label = 'Heure';
  544. $type->sqlType = 'string';
  545. $type->default_attributes = array_merge($type_text->default_attributes,array(
  546. 'data-type'=>'"hour"',
  547. 'title'=>'"format hh:mm"',
  548. 'placeholder'=>'"13:37"',
  549. ));
  550. $type->onInput = $type_text->onInput;
  551. $type->icon = 'far fa-clock';
  552. $type->description = 'Combo Heures/minutes';
  553. $type->default = '';
  554. $type->filter = array(
  555. 'operators' => array (
  556. '<' =>array("view" =>'hour'),
  557. '>' =>array("view" =>'hour'),
  558. '=' =>array("view" =>'hour'),
  559. 'null' =>array(),
  560. 'not null' =>array()
  561. )
  562. );
  563. $types[$type->slug] = $type;
  564. //Dictionnaire
  565. $type_dictionary = new self();
  566. $type_dictionary->slug = 'dictionary';
  567. $type_dictionary->label = 'Liste configurable';
  568. $type_dictionary->sqlType = 'int';
  569. $type_dictionary->default_attributes = array_merge($type_text->default_attributes,array(
  570. 'data-type'=>'"dictionary"',
  571. 'data-slug'=>'"{{key}}"',
  572. 'data-depth'=>'"1"',
  573. 'key'=>'data-disable-label',
  574. 'data-value'=>'',
  575. 'class'=>'"form-control select-control"'
  576. ));
  577. $type_dictionary->settings = array(
  578. 'slug'=> array(
  579. 'type'=>'dictionary',
  580. 'label'=>'Liste ciblée',
  581. 'attributes'=> array(
  582. 'class'=>'"form-control"',
  583. 'data-slug'=>"",
  584. 'data-output'=>"slug"
  585. )
  586. )
  587. );
  588. $type_dictionary->onRawDisplay = function($value,$options = array()){
  589. if(empty($value) || !is_numeric($value)) return '';
  590. $dictionary = Dictionary::getById($value);
  591. return !$dictionary? '':$dictionary->label;
  592. };
  593. $type_dictionary->fromRawDisplay = function($value,$options = array()){
  594. $filters = array('label'=>$value);
  595. if(isset($options['parent'])) $filters['parent'] = $options['parent'];
  596. $dictionary = Dictionary::load($filters);
  597. return !$dictionary ? 0 : $dictionary->id;
  598. };
  599. $type_dictionary->onInput = function($field = array(),$options = array()){
  600. if(isset($field['id'])) $field['attributes']['id'] ='"'.$field['id'].'"';
  601. if(isset($field['value'])) $field['attributes']['data-value'] = $field['value'];
  602. //gestion des metas de dynamiques fields pour listes simples
  603. if(isset($field['attributes']['data-values'])){
  604. $field['values'] = $field['attributes']['data-values'];
  605. unset($field['attributes']['data-values']);
  606. }
  607. $html = '';
  608. $html .= '<select ';
  609. foreach ($field['attributes'] as $key=>$attribute) {
  610. $html .= $key;
  611. if(!empty($attribute))
  612. $html .= '='.$attribute;
  613. $html .= ' ';
  614. }
  615. $html .='>';
  616. if(isset($field['values'])){
  617. $values = is_callable($field['values']) ? $field['values']() : $field['values'];
  618. if(!empty($values) && is_array($values)){
  619. foreach($values as $key=>$value){
  620. $html .='<option '.(isset($field['value']) && $field['value']==$key?' selected="selected" ':'').' value="'.$key.'">'.$value.'</option>';
  621. }
  622. }
  623. }
  624. $html .='</select>';
  625. return $html ;
  626. };
  627. $type_dictionary->icon = 'fas fa-list-ol';
  628. $type_dictionary->description = 'Liste de sélection configurable récursive';
  629. $type_dictionary->default = '';
  630. $type_dictionary->filter = array(
  631. 'attributes' => array(
  632. 'data-display'=>'"dropdown"',
  633. 'data-slug'=>'"{{slug}}"',
  634. 'data-depth'=>'"{{depth}}"',
  635. 'data-value-selector'=>'".filter-value:last-child"'
  636. ),
  637. 'operators' => array(
  638. 'in' =>array(
  639. "view" => "checkbox-list",
  640. "value-separator" => ","
  641. ),
  642. 'not in' =>array(
  643. "view" => "checkbox-list",
  644. "value-separator" => ","
  645. ),
  646. '=' =>array("view" =>'dictionary'),
  647. '!=' =>array("view" =>'dictionary'),
  648. 'null' =>array(),
  649. 'not null' =>array()
  650. )
  651. );
  652. $types[$type_dictionary->slug] = $type_dictionary;
  653. //Table JSON clé / valeur
  654. $type = new self();
  655. $type->slug = 'jsontable';
  656. $type->label = 'Table clé/valeur';
  657. $type->sqlType = 'longstring';
  658. $type->default_attributes = array(
  659. 'type'=>'"text"',
  660. 'data-type'=>'"jsontable"',
  661. 'data-format'=>'"key-value"',
  662. );
  663. $type->onInput = $type_text->onInput;
  664. $type->icon = 'fas fa-table';
  665. $type->description = 'Table clé/valeur enregistré en JSON';
  666. $type->default = '';
  667. $type->onRawDisplay = function($value){
  668. $stream = '';
  669. $table = json_decode($value,true);
  670. if(!is_array($table)) return '';
  671. foreach($table as $key=>$value){
  672. $stream .= ''.$key.' : '.$value.',';
  673. }
  674. return $stream;
  675. };
  676. $type->onHtmlDisplay = function($value){
  677. $html = '<ul class="list-group">';
  678. $table = json_decode($value,true);
  679. if(!is_array($table)) return '';
  680. foreach($table as $key=>$value){
  681. $html .= '<li class="list-group-item p-1"><strong>'.$key.'</strong> : '.$value.'</li>';
  682. }
  683. $html .= '</ul>';
  684. return $html;
  685. };
  686. $types[$type->slug] = $type;
  687. //Liste
  688. $type_list = new self();
  689. $type_list->slug = 'list';
  690. $type_list->label = 'Liste classique';
  691. $type_list->sqlType = 'string';
  692. $type_list->default_attributes = array_merge($type_text->default_attributes,array(
  693. 'class'=>'"form-control select-control"'
  694. ));
  695. $type_list->filter = array(
  696. 'attributes' => array(
  697. 'data-values'=>'"{{filterTypeValue}}"',
  698. ),
  699. 'operators' => array(
  700. 'in' =>array(
  701. "view" => "checkbox-list",
  702. "value-separator" => ",",
  703. ),
  704. 'not in' =>array(
  705. "view" => "checkbox-list",
  706. "value-separator" => ",",
  707. ),
  708. 'inline-and' =>array(
  709. "view" => "checkbox-list",
  710. "value-separator" => ",",
  711. ),
  712. 'inline-or' =>array(
  713. "view" => "checkbox-list",
  714. "value-separator" => ",",
  715. ),
  716. '=' =>array("view" =>'list'),
  717. '!=' =>array("view" =>'list'),
  718. 'null' =>array(),
  719. 'not null' =>array()
  720. )
  721. );
  722. $type_list->onInput = $type_dictionary->onInput;
  723. $type_list->onRawDisplay = function($value,$options = array()){
  724. if(!isset($options['meta'])) return '';
  725. $list = json_decode($options['meta'],true);
  726. return isset($list['values']) && isset($list['values'][$value]) ? $list['values'][$value] : '';
  727. };
  728. $type_list->icon = 'fas fa-list-ol';
  729. $type_list->description = 'Liste de sélection classique';
  730. $type_list->default = '';
  731. $type_list->settings = array(
  732. 'values'=> array(
  733. 'type'=>'jsontable',
  734. 'label'=>'Valeurs possibles',
  735. 'attributes'=> array(
  736. 'data-columns' => '\'{"key":"Clé","value":"Valeur"}\'',
  737. 'data-format' => '"key-value"'
  738. ))
  739. );
  740. $types[$type_list->slug] = $type_list;
  741. //Checkbox list
  742. $type_checkbox = new self();
  743. $type_checkbox->slug = 'checkbox-list';
  744. $type_checkbox->label = 'Liste de cases à cocher';
  745. $type_checkbox->sqlType = 'longtext';
  746. $type_checkbox->default_attributes = array_merge($type_text->default_attributes,array(
  747. 'data-type'=>'"checkbox-list"',
  748. 'data-slug'=>'"{{key}}"',
  749. 'data-display'=>'"dropdown"',
  750. 'value'=>'"{{value}}"',
  751. 'class'=>'"form-control"'
  752. ));
  753. $type_checkbox->settings = array(
  754. 'slug'=> array(
  755. 'type'=>'dictionary',
  756. 'label'=>'Liste ciblée',
  757. 'attributes'=> array(
  758. 'class'=>'"form-control"',
  759. 'data-slug'=>"",
  760. 'data-output'=>"slug",
  761. 'data-format'=>"slug"
  762. )
  763. ),
  764. 'values'=> array(
  765. 'type'=>'jsontable',
  766. 'label'=>'Valeurs possibles',
  767. 'attributes'=> array(
  768. 'data-columns' => '\'{"key":"Clé","value":"Valeur"}\'',
  769. 'data-format' => '"key-value"'
  770. )),
  771. 'operator-delete'=> array(
  772. 'type'=>'text',
  773. 'value'=>'["in","not in"]',
  774. 'attributes'=> array(
  775. 'class' => '"hidden"',
  776. ))
  777. );
  778. $type_checkbox->filter = array(
  779. 'attributes' => array(
  780. 'data-display'=>'"dropdown"',
  781. 'data-slug'=>'"{{slug}}"',
  782. 'data-depth'=>'"{{depth}}"',
  783. //'data-display'=>'"{{display}}"',
  784. 'data-values'=>'"{{filterTypeValue}}"',
  785. 'data-multi-level-select'=>'"{{multiLevelSelect}}"',
  786. ),
  787. 'operators' => array(
  788. 'in' =>array(
  789. "view" => "checkbox-list",
  790. "value-separator" => ","
  791. ),
  792. 'not in' =>array(
  793. "view" => "checkbox-list",
  794. "value-separator" => ","
  795. ),
  796. 'inline-and' =>array(
  797. "view" => "checkbox-list"
  798. ),
  799. 'inline-or' =>array(
  800. "view" => "checkbox-list"
  801. ),
  802. '=' =>array("view" =>'dictionary'),
  803. '!=' =>array("view" =>'dictionary'),
  804. 'null' =>array(),
  805. 'not null' =>array()
  806. )
  807. );
  808. $type_checkbox->onInput = function($field=array(),$options=array()){
  809. $attributes = array_merge($options['type']->default_attributes,$field['attributes']);
  810. if(isset($field['id'])) $field['attributes']['id'] ='"'.$field['id'].'"';
  811. $html = '';
  812. $html.= '<input value="'.(isset($field['value']) ? str_replace('"','&quot;',$field['value']) : '').'"';
  813. foreach ($field['attributes'] as $key => $value) {
  814. $html.= ' ';
  815. $html.= $key;
  816. if(!empty($value)) $html.= '='.(is_string($value)?$value:"'".json_encode($value)."'");
  817. $html.= ' ';
  818. }
  819. $html.= '>';
  820. return $html;
  821. };
  822. $type_checkbox->onSave = function($value,$options=array()){
  823. $value = explode(',',$value);
  824. $value = array_filter($value);
  825. $value = ','.implode(',',$value).',';
  826. if($value==',,') $value = '';
  827. return $value;
  828. };
  829. $type_checkbox->onLoad = function($value){
  830. $value = explode(',',$value);
  831. $value = array_filter($value);
  832. $value = implode(',',$value);
  833. return $value;
  834. };
  835. $type_checkbox->onRawDisplay = function($value,$field) use($type_checkbox){
  836. $onLoad = $type_checkbox->onLoad;
  837. $value = $onLoad($value);
  838. $values = explode(',',$value);
  839. if(count($values)==0) return '';
  840. $results = array();
  841. //dictionary
  842. $ids = array();
  843. foreach($values as $i=>$id){
  844. if(empty($id) || !is_numeric($id)) continue;
  845. unset($values[$i]);
  846. $ids[] = $id ;
  847. }
  848. if(count($ids)!=0){
  849. $dictionnaries = Dictionary::loadAll(array('id:IN'=>$ids));
  850. foreach($dictionnaries as $dictionary){
  851. $results[] = $dictionary->label;
  852. }
  853. }
  854. //valeur en dur
  855. if(!empty($field['meta'])){
  856. $meta = json_decode($field['meta'],true);
  857. if(!empty($meta['values'])){
  858. $availableValues = $meta['values'];
  859. foreach($values as $id){
  860. if(isset($availableValues[$id])) $results[] = $availableValues[$id];
  861. }
  862. }
  863. };
  864. $value = implode(',',$results);
  865. return $value;
  866. };
  867. $type_checkbox->onHtmlDisplay = function($value,$field) use($type_checkbox){
  868. $onRawDisplay = $type_checkbox->onRawDisplay;
  869. $value = explode(',',$onRawDisplay($value,$field));
  870. $value = array_filter($value);
  871. $value = '<span class="badge badge-secondary">'.implode('</span> <span class="badge badge-secondary">',$value).'</span>';
  872. return $value;
  873. };
  874. $type_checkbox->fromRawDisplay = function($value,$options = array()){
  875. $filters = array('label'=>$value);
  876. if(isset($options['parent'])) $filters['parent'] = $options['parent'];
  877. $dictionary = Dictionary::load($filters);
  878. return !$dictionary ? 0 : $dictionary->id;
  879. };
  880. $type_checkbox->icon = 'fas fa-list';
  881. $type_checkbox->description = 'Liste de cases à cocher configurable';
  882. $type_checkbox->default = '';
  883. $types[$type_checkbox->slug] = $type_checkbox;
  884. //Entier
  885. $type = new self();
  886. $type->slug = 'integer';
  887. $type->label = 'Nombre Entier';
  888. $type->sqlType = 'int';
  889. $type->default_attributes = array_merge($type_text->default_attributes,array(
  890. 'type'=>'"number"',
  891. ));
  892. $type->onInput = $type_text->onInput;
  893. $type->icon = 'fas fa-sort-numeric-down';
  894. $type->description = 'Nombre entier';
  895. $type->onSave = function($value,$options=array()){
  896. $value = trim($value);
  897. if(!empty($value) && !is_numeric($value)) throw new Exception("Ce champ ne doit contenir que des chiffres et être entier");
  898. return $value;
  899. };
  900. $type->filter = array(
  901. 'operators' => array (
  902. 'in' => array(
  903. "view" =>'tag',
  904. "value-separator" => ","
  905. ),
  906. 'not in' => array(
  907. "view" =>'tag',
  908. "value-separator" => ","
  909. ),
  910. '<' => array("view" =>'decimal'),
  911. '>' => array("view" =>'decimal'),
  912. '=' => array("view" =>'decimal'),
  913. '!=' => array("view" =>'decimal'),
  914. 'between' => array("view" =>'decimal'),
  915. 'null' => array(),
  916. 'not null' => array()
  917. )
  918. );
  919. $type->default = '';
  920. $types[$type->slug] = $type;
  921. //Flottant
  922. $type = new self();
  923. $type->slug = 'decimal';
  924. $type->label = 'Décimal';
  925. $type->sqlType = 'decimal';
  926. $type->default_attributes = array_merge($type_text->default_attributes,array(
  927. 'data-type'=>'"decimal"'
  928. ));
  929. $type->onInput = $type_text->onInput;
  930. $type->icon = 'fas fa-sort-numeric-down';
  931. $type->description = 'Nombre décimal';
  932. $type->onSave = function($value,$options=array()){
  933. $value = trim(str_replace(',', '.', $value));
  934. if(!empty($value) && !is_numeric($value)) throw new Exception("Ce champ ne doit contenir que des chiffres");
  935. return $value;
  936. };
  937. $type->filter = array(
  938. 'operators' => array(
  939. 'in' => array(
  940. "view" =>'tag',
  941. "value-separator" => ","
  942. ),
  943. 'not in' => array(
  944. "view" =>'tag',
  945. "value-separator" => ","
  946. ),
  947. '<' => array("view" =>'decimal'),
  948. '>' => array("view" =>'decimal'),
  949. '=' => array("view" =>'decimal'),
  950. '!=' => array("view" =>'decimal'),
  951. 'between' => array("view" =>'decimal'),
  952. 'null' => array(),
  953. 'not null' => array()
  954. )
  955. );
  956. $type->default = '';
  957. $types[$type->slug] = $type;
  958. //Adresse
  959. $type = new self();
  960. $type->slug = 'address';
  961. $type->label = 'Adresse';
  962. $type->sqlType = 'longstring';
  963. $type->default_attributes = array_merge($type_text->default_attributes,array(
  964. 'data-type'=>'"location"',
  965. ));
  966. $type->onInput = $type_text->onInput;
  967. $type->icon = 'fas fa-street-view';
  968. $type->description = 'Adresse postale';
  969. $type->default = '';
  970. $type->filter = array(
  971. 'operators' => array (
  972. 'like' => array("view" =>'text'),
  973. '=' => array("view" =>'text'),
  974. '!=' => array("view" =>'text'),
  975. 'null' => array(),
  976. 'not null' => array()
  977. )
  978. );
  979. $types[$type->slug] = $type;
  980. //Password
  981. $type = new self();
  982. $type->slug = 'password';
  983. $type->label = 'Mot de passe';
  984. $type->sqlType = 'string';
  985. $type->default_attributes = array_merge($type_text->default_attributes,array(
  986. 'data-type'=>'"password"',
  987. 'autocomplete'=>'"new-password"',
  988. ));
  989. $type->onInput = $type_text->onInput;
  990. $type->onSave = function($value,$options=array()){
  991. return encrypt($value);
  992. };
  993. $type->onLoad = function($value){
  994. $value = decrypt($value);
  995. return $value === false ? '' : $value;
  996. };
  997. $type->onRawDisplay = function($value,$options = array()){
  998. $value = decrypt($value);
  999. return $value === false ? '' : $value;
  1000. };
  1001. $type->icon = 'fas fa-unlock-alt';
  1002. $type->description = 'Mot de passe caché';
  1003. $type->default = '';
  1004. $types[$type->slug] = $type;
  1005. //Couleur
  1006. $type = new self();
  1007. $type->slug = 'color';
  1008. $type->label = 'Couleur';
  1009. $type->sqlType = 'string';
  1010. $type->default_attributes = array_merge($type_text->default_attributes,array(
  1011. 'data-type'=>'"color"',
  1012. ));
  1013. $type->onHtmlDisplay = function($value,$options = array()){
  1014. if(empty($value)){
  1015. $html = '<span class="fa-stack">
  1016. <i class="far fa-circle fa-stack-1x"></i>
  1017. <i class="fas fa-slash text-danger fa-stack-1x"></i>
  1018. </span>';
  1019. }else{
  1020. $html = '<i class="fas fa-circle" style="color:'.$value.'"></i>';
  1021. }
  1022. return $html;
  1023. };
  1024. $type->filter = array(
  1025. 'operators' => array (
  1026. '=' => array("view" =>'text'),
  1027. '!=' => array("view" =>'text'),
  1028. 'null' => array(),
  1029. 'not null' => array()
  1030. )
  1031. );
  1032. $type->onInput = $type_text->onInput;
  1033. $type->icon = 'fas fa-palette';
  1034. $type->description = 'Couleur';
  1035. $type->default = '';
  1036. $types[$type->slug] = $type;
  1037. //Icône
  1038. $type = new self();
  1039. $type->slug = 'icon';
  1040. $type->label = 'Icône';
  1041. $type->sqlType = 'string';
  1042. $type->default_attributes = array_merge($type_text->default_attributes,array(
  1043. 'data-type'=>'"icon"',
  1044. ));
  1045. $type->onInput = $type_text->onInput;
  1046. $type->onHtmlDisplay = function($value,$options = array()){
  1047. $html = '<i class="'.$value.'"></i>';
  1048. return $html;
  1049. };
  1050. $type->filter = array(
  1051. 'operators' => array (
  1052. '=' => array("view" =>'icon'),
  1053. '!=' => array("view" =>'icon'),
  1054. 'null' => array(),
  1055. 'not null' => array()
  1056. )
  1057. );
  1058. $type->icon = 'fas fa-icons';
  1059. $type->description = 'Icône d\'illustration';
  1060. $type->default = '';
  1061. $types[$type->slug] = $type;
  1062. //Prix
  1063. $type = new self();
  1064. $type->slug = 'price';
  1065. $type->label = 'Prix';
  1066. $type->sqlType = 'decimal';
  1067. $type->onInput = $type_text->onInput;
  1068. $type->icon = 'fas fa-euro-sign';
  1069. $type->description = 'Devis monetaire';
  1070. $type->default = '';
  1071. $type->default_attributes = array_merge($type_text->default_attributes,array(
  1072. 'data-type'=>'"price"',
  1073. 'step'=>'"0.01"',
  1074. 'type'=>'number'
  1075. ));
  1076. $type->filter = array(
  1077. 'operators' => array (
  1078. '=' => array("view" =>'price'),
  1079. '!=' => array("view" =>'price'),
  1080. '<' => array("view" =>'price'),
  1081. '>' => array("view" =>'price'),
  1082. 'between' =>array("view" =>'price'),
  1083. 'in' => array(
  1084. "view" =>'tag',
  1085. "value-separator" => ","
  1086. ),
  1087. 'not in' => array(
  1088. "view" =>'tag',
  1089. "value-separator" => ","
  1090. ),
  1091. 'null' => array(),
  1092. 'not null' => array()
  1093. )
  1094. );
  1095. $type->onSave = function($value,$options=array()){
  1096. $value = trim(str_replace(',', '.', $value));
  1097. if(!empty($value) && !is_numeric($value)) throw new Exception("Le prix ne doit contenir que des chiffres");
  1098. return $value;
  1099. };
  1100. $types[$type->slug] = $type;
  1101. //Choix
  1102. $type = new self();
  1103. $type->slug = 'choice';
  1104. $type->label = 'Choix';
  1105. $type->customLabel = false;
  1106. $type->sqlType = 'longstring';
  1107. $type->default_attributes = array_merge($type_text->default_attributes,array(
  1108. 'data-type'=>'"choice"',
  1109. 'class'=>''
  1110. ));
  1111. $type->filter = array(
  1112. 'attributes' => array('data-values'=>'"{{filterTypeValue}}"'),
  1113. 'operators' => array (
  1114. '=' => array("view" =>'choice'),
  1115. '!=' => array("view" =>'choice'),
  1116. 'null' => array(),
  1117. 'not null' => array()
  1118. )
  1119. );
  1120. $type->settings = array(
  1121. 'values'=> array(
  1122. 'type'=>'jsontable',
  1123. 'label'=>'Options possibles',
  1124. 'attributes'=> array(
  1125. 'data-columns' => '\'{"key":"Clé","value":"Valeur"}\'',
  1126. 'data-format' => '"key-value"'
  1127. ))
  1128. );
  1129. $type->onInput = function($field=array(),$options=array()){
  1130. $attributes = array_merge($options['type']->default_attributes,$field['attributes']);
  1131. if(isset($field['id'])) $field['attributes']['id'] ='"'.$field['id'].'"';
  1132. $html = '';
  1133. $html.= '<input value="'.(isset($field['value']) ? str_replace('"','&quot;',$field['value']) : '').'" data-values="'.(isset($field['values']) ? htmlspecialchars(json_encode($field['values']), ENT_QUOTES, 'UTF-8') : '').'"';
  1134. foreach ($field['attributes'] as $key => $value) {
  1135. $html.= ' ';
  1136. $html.= $key;
  1137. if(!empty($value)) $html.= '='.(is_string($value)?$value:"'".json_encode($value)."'");
  1138. $html.= ' ';
  1139. }
  1140. $html.= '>';
  1141. return $html;
  1142. };
  1143. $type->onRawDisplay = function($value,$options = array()){
  1144. if(isset($options['meta'])){
  1145. $meta = is_string($options['meta']) ? json_decode($options['meta'],true) : $options['meta'];
  1146. if(isset($meta['values']) && isset($meta['values'][$value])){
  1147. $value = $meta['values'][$value];
  1148. }
  1149. }
  1150. return $value;
  1151. };
  1152. $type->onHtmlDisplay = $type->onRawDisplay;
  1153. $type->icon = 'far fa-dot-circle';
  1154. $type->description = 'Choix unique';
  1155. $type->default = '';
  1156. $types[$type->slug] = $type;
  1157. //Booléen
  1158. $type = new self();
  1159. $type->slug = 'boolean';
  1160. $type->label = 'Vrai ou Faux';
  1161. $type->customLabel = true;
  1162. $type->sqlType = 'boolean';
  1163. $type->default_attributes = array_merge($type_text->default_attributes,array(
  1164. 'data-type'=>'"checkbox"',
  1165. 'class'=>'',
  1166. 'type'=>'"checkbox"'
  1167. ));
  1168. $type->filter = array(
  1169. 'operators' => array (
  1170. '=' => array("view" =>'boolean'),
  1171. 'null' => array(),
  1172. 'not null' => array()
  1173. )
  1174. );
  1175. $type->onRawDisplay = function($value,$options = array()){
  1176. return $value ? 'VRAI' : 'FAUX';
  1177. };
  1178. $type->onHtmlDisplay = function($value,$options = array()){
  1179. $html = $value ? '<i class="fas fa-check text-success"></i>' : '<i class="fas fa-times text-danger"></i>' ;
  1180. return $html;
  1181. };
  1182. $type->onInput = function($field=array(),$option=array()){
  1183. $html = '';
  1184. $html.= '<div><label><input id="'.(isset($field['id'])?$field['id']:'').'" ';
  1185. if(isset($field['value']) && $field['value'] == 1){
  1186. $field['attributes']['checked'] = '"checked"';
  1187. }
  1188. foreach ($field['attributes'] as $key => $value) {
  1189. $html.= ' ';
  1190. $html.= $key;
  1191. if(!empty($value)) $html.= '='.$value;
  1192. $html.= ' ';
  1193. }
  1194. $html.= '>';
  1195. if(!empty($field['label']) && $option['allowCustomLabel']) $html.= ' '.$field['label'];
  1196. $html.= '</label></div>';
  1197. return $html;
  1198. };
  1199. $type->icon = 'far fa-check-square';
  1200. $type->description = 'Vrai ou Faux';
  1201. $type->default = '';
  1202. $types[$type->slug] = $type;
  1203. //Url
  1204. $type = new self();
  1205. $type->slug = 'url';
  1206. $type->label = 'Adresse web';
  1207. $type->sqlType = 'string';
  1208. $type->default_attributes = array_merge($type_text->default_attributes,array(
  1209. 'data-type'=>'"url"',
  1210. ));
  1211. $type->onSave = function($value,$options=array()){
  1212. if(!empty($value) && !filter_var($value, FILTER_VALIDATE_URL) ) throw new Exception("Mauvais format d'adresse web");
  1213. return $value;
  1214. };
  1215. $type->onInput = $type_text->onInput;
  1216. $type->onHtmlDisplay = function($value,$options = array()){
  1217. if(empty($value)) return $value;
  1218. $html = '<a href="'.$value.'">';
  1219. $html .= $value;
  1220. $html.= '</a>';
  1221. return $html;
  1222. };
  1223. $type->filter = array(
  1224. 'operators' => array (
  1225. '=' => array("view" =>'url'),
  1226. '!=' => array("view" =>'url'),
  1227. 'like' => array("view" =>'text'),
  1228. 'not like' => array("view" =>'text'),
  1229. 'null' => array(),
  1230. 'not null' => array()
  1231. )
  1232. );
  1233. $type->icon = 'fas fa-globe';
  1234. $type->description = 'Adresse Url (web)';
  1235. $type->default = '';
  1236. $types[$type->slug] = $type;
  1237. //Mail
  1238. $type = new self();
  1239. $type->slug = 'mail';
  1240. $type->label = 'E-mail';
  1241. $type->sqlType = 'string';
  1242. $type->default_attributes = array_merge($type_text->default_attributes,array(
  1243. 'data-type'=>'"mail"',
  1244. 'type'=>'"mail"',
  1245. 'pattern'=>'".+@.+"'
  1246. ));
  1247. $type->onSave = function($value,$options=array()){
  1248. if(!empty($value) && !check_mail($value)) throw new Exception("Mauvais format d'e-mail");
  1249. return $value;
  1250. };
  1251. $type->onHtmlDisplay = function($value,$options = array()){
  1252. $html = '<a href="mailto:'.$value.'">';
  1253. $html .= $value;
  1254. $html.= '</a>';
  1255. return $html;
  1256. };
  1257. $type->filter = array(
  1258. 'operators' => array (
  1259. '=' => array("view" =>'mail'),
  1260. '!=' => array("view" =>'mail'),
  1261. 'like' => array("view" =>'text'),
  1262. 'not like' => array("view" =>'text'),
  1263. 'null' => array(),
  1264. 'not null' => array()
  1265. )
  1266. );
  1267. $type->onInput = $type_text->onInput;
  1268. $type->icon = 'far fa-envelope-open';
  1269. $type->description = 'E-mail';
  1270. $type->default = '';
  1271. $types[$type->slug] = $type;
  1272. //Téléphone
  1273. $type = new self();
  1274. $type->slug = 'phone';
  1275. $type->label = 'Téléphone';
  1276. $type->sqlType = 'string';
  1277. $type->default_attributes = array_merge($type_text->default_attributes,array(
  1278. 'data-type'=>'"phone"'
  1279. ));
  1280. $type->onSave = function($value,$options=array()){
  1281. if(!empty($value) && !check_phone_number($value)) throw new Exception("Mauvais format de téléphone");
  1282. $value = normalize_phone_number($value);
  1283. return $value;
  1284. };
  1285. $type->onInput = $type_text->onInput;
  1286. $type->icon = 'fas fa-mobile-alt';
  1287. $type->description = 'N° Téléphone';
  1288. $type->default = '';
  1289. $type->filter = array(
  1290. 'operators' => array (
  1291. 'like' =>array("view" =>'text'),
  1292. 'not like' =>array("view" =>'text'),
  1293. '=' =>array("view" =>'phone'),
  1294. '!=' =>array("view" =>'phone'),
  1295. 'null' =>array(),
  1296. 'not null' =>array()
  1297. )
  1298. );
  1299. $types[$type->slug] = $type;
  1300. Plugin::callHook('field_types',array(&$types));
  1301. if(isset($key)) return isset($types[$key])? $types[$key] : $types['text'];
  1302. return $types;
  1303. }
  1304. }
  1305. ?>