WidgetElement.class.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. abstract class WidgetElement extends Entity{
  3. public $id,$widget,$sort,$label,$slug;
  4. public $javascript = '';
  5. public $icon = 'far fa-file';
  6. public $fields = array(
  7. 'id'=>'key',
  8. 'widget'=>'int',
  9. 'sort'=>'int',
  10. 'slug'=>'string',
  11. 'label'=>'string'
  12. );
  13. function editor(){
  14. return 'No editor yet';
  15. }
  16. function preview(){
  17. return 'No preview yet';
  18. }
  19. public static function getType($type){
  20. $file = __DIR__.SLASH.'element'.SLASH.ucfirst($type).'.class.php';
  21. if(!file_exists($file)) throw new Exception ('Invalid widget element');
  22. require_once($file);
  23. return $type;
  24. }
  25. public static function cascadePreview($type,$id,$filters = array(),$widget){
  26. $type = self::getType($type);
  27. $currentElement = $type::getById($id);
  28. $elements = self::getElementsByWidget($currentElement->widget);
  29. $data = array();
  30. if($currentElement != null){
  31. foreach($elements as $element){
  32. if($currentElement->sort<$element->sort) continue;
  33. $response = $element->preview($data,$filters,$widget);
  34. $data[$element->slug] = $response['data'];
  35. }
  36. }
  37. return $response;
  38. }
  39. public static function getElementsByWidget($id){
  40. $elements = array();
  41. foreach(glob(__DIR__.SLASH.'element'.SLASH.'*.class.php') as $file){
  42. require_once($file);
  43. $type = str_replace('.class.php','',basename($file));
  44. foreach($type::loadAll(array('widget'=>$id)) as $element)
  45. $elements[] = $element;
  46. }
  47. usort($elements, function($a,$b){
  48. if($a->sort < $b->sort) return -1;
  49. if($a->sort > $b->sort) return 1;
  50. if($a->sort == $b->sort) return 0;
  51. });
  52. return $elements;
  53. }
  54. public function toFile($sort){
  55. return array(
  56. 'filename' => '',
  57. 'stream' => ''
  58. );
  59. }
  60. public static function template($tpl,$data=array(),$emptyIsNull = false){
  61. if($emptyIsNull){
  62. foreach($data as $key=>$value){
  63. if($data[$key]=='') unset($data[$key]);
  64. }
  65. }
  66. return template($tpl,$data,true);
  67. }
  68. }
  69. ?>