Source of file BaseSection.php
Size: 10,251 Bytes - Last Modified: 2022-02-21T10:00:27+00:00
/var/www/docs.ssmods.com/process/src/code/BaseSection.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 | <?php use EVought\DataUri\DataUri; class BaseSection { public function __construct($type, $conf, $section=null, $extra=array()) { $this->type = $type; $this->section = $section; $this->conf = $conf; $this->extra = $extra; } //Abstract factory //Needs conf to create subobjects public static function create($section, $conf, $extra=array()) { $class = "BaseSection"; if(array_key_exists("class", $conf[$section->type])) { $class = $conf[$section->type]["class"]; } return new $class($section->type, $conf, $section, $extra); } //Create without instance public static function createabstract($type, $conf) { $class = "BaseSection"; if(array_key_exists("class", $conf[$type])) { $class = $conf[$type]["class"]; } return new $class($type, $conf); } public function getSize() { $base_path = Director::baseFolder(); $moduleconf = $this->conf[$this->type]; if(array_key_exists("preview-size", $moduleconf)) return $moduleconf["preview-size"]; return "default"; } public function getTemplate() { $base_path = Director::baseFolder(); $moduleconf = $this->conf[$this->type]; $templatefile = $base_path."/".$moduleconf["template"]; $handle = fopen($templatefile, "r"); $template = fread($handle, filesize($templatefile)); fclose($handle); return $template; } public function getFormDef() { $base_path = Director::baseFolder(); $moduleconf = $this->conf[$this->type]; if(array_key_exists("formdef", $moduleconf)) { $formdeffile = $base_path."/".$moduleconf["formdef"]; $handle = fopen($formdeffile, "r"); $formdef = fread($handle, filesize($formdeffile)); fclose($handle); $formdef_json = json_decode($formdef, true); } else { $formdef_json = null; } return $formdef_json; } public function getPreview() { $moduleconf = $this->conf[$this->type]; $base_url = Director::baseURL(); if(array_key_exists("preview", $moduleconf)) { return $moduleconf["preview"]; } return; } function getExtension($mimetype) { if(empty($mimetype)) return ""; switch($mimetype) { case 'image/gif': return '.gif'; case 'image/jpeg': return '.jpg'; case 'image/png': return '.png'; case 'application/pdf': return '.pdf'; default: $mimearray = explode("/", $mimetype); return array_pop($mimearray); } } public function isImage($mimetype) { if(empty($mimetype)) return false; if(in_array($mimetype, array('image/gif', 'image/jpeg', 'image/png'))) return true; return false; } public function replaceImages($content, $ignore=[]) { //Save, replace images foreach($content as $name => $value) { if(in_array($name, $ignore)) { //Skip children } else { if(is_array($value)) { $newvalue = []; foreach($value as $subcontent) { $newcontent = $this->replaceImages($subcontent); //No ignore $newvalue[] = $newcontent; } $content->$name = $newvalue; } else { if(strpos($value, "data:image") === 0) { DataUri::tryParse($value, $data); $mediaType = explode(";", $data->getMediaType()); $mime = $mediaType[0]; $filetype = $this->getExtension($mime); $folder = Folder::find_or_make("autoupload"); $filename = 'upload_'.RAND(1,10000).$filetype; $filepath = Director::baseFolder()."/assets/autoupload/".$filename; $relfilepath = "assets/autoupload/".$filename; file_put_contents($filepath, base64_decode($data->getEncodedData())); if($this->isImage($mime)) { $image = new Image(array( "Filename" => $relfilepath, "ParentID" => $folder->ID, "Name" => $filename, "Title" => $filename )); $image->write(); $realname = str_replace("load_", "", $name); $savename = str_replace("load_", "save_", $name); unset($content->$name); $content->$savename = "image:".$image->ID; } else { $file = new File(array( "Filename" => $relfilepath, "ParentID" => $folder->ID, "Name" => $filename, "Title" => $filename )); $file->write(); $realname = str_replace("load_", "", $name); $savename = str_replace("load_", "save_", $name); unset($content->$name); $content->$savename = "file:".$file->ID; } } } } } return $content; } public function beforeSave($content) { $conf = $this->conf; $subsection_names = []; foreach($this->getSubSections() as $name => $listcontent) { $subsection_names[] = $name; $preparedlist = []; foreach($listcontent as $subsection) { $subsection->content = BaseSection::create($subsection, $conf)->beforeSave($subsection->content); $preparedlist[] = $subsection; } $content->$name = $preparedlist; } $content = $this->replaceImages($content, $subsection_names); return $content; } public function getSubSections() { $section = $this->section; $conf = $this->conf; $subsections = []; if(array_key_exists("subsections", $conf[$section->type])) { $subsectionlists = $conf[$section->type]["subsections"]; foreach($subsectionlists as $subsectionlistname) { $listcontent = $section->content->$subsectionlistname?$section->content->$subsectionlistname:[]; $subsections[$subsectionlistname] = $listcontent; } } return $subsections; } public function getImageInfos() { $conf = $this->conf[$this->section->type]; if(!isset($conf["images"])) return []; return $conf["images"]; } public function renderImages($content, $ignore=[]) { $image_infos = $this->getImageInfos(); foreach($content as $name => $value) { if(in_array($name, $ignore)) { continue; //Skip subsections } else { if(is_array($value)) { $newvalue = []; foreach($value as $subcontent) { $newvalue[] = $this->renderImages($subcontent, []); } $content->$name = $newvalue; } else { if(strpos($value, "image:") === 0) { $id = intval(str_replace("image:", "", $value)); $image = Image::get()->byId($id); $realname = str_replace("save_", "", $name); if($image_infos && isset($image_infos[$realname])) { $info = $image_infos[$realname]; if($image) $image = $image->getFormattedImage($info["method"], isset($info["width"])?$info["width"]:0, isset($info["height"])?$info["height"]:0); } if($image) { $content->$realname = $image->URL; } } } } } return $content; } public function beforeRender($content, $prepareChildren=false) { $subsection_names = []; $section = $this->section; $extra = $this->extra; $conf = $this->conf; //Subsections foreach($this->getSubSections() as $name => $listcontent) { $preparedlist = []; $subsection_names[] = $name; if($prepareChildren) { foreach($listcontent as $subsection) { $subsection->content = BaseSection::create($subsection, $conf, $extra)->beforeRender($subsection->content); $preparedlist[] = $subsection; } } $content->$name = $preparedlist; } $content = $this->renderImages($content, $subsection_names); $content = (object) array_merge((array) $content, $extra); return $content; } public function render() { $engine = new Mustache_Engine; $section = $this->section; $conf = $this->conf; $templatesrc = $conf[$section->type]["template"]; $base_path = Director::baseFolder(); $template = file_get_contents($base_path."/".$templatesrc); $content = $this->beforeRender($section->content, true); //Subsections foreach($this->getSubSections() as $name => $listcontent) { $rendered_subcontent = ""; foreach($listcontent as $subsection) { $rendered_subcontent .= BaseSection::create($subsection, $conf)->render(); } $content->$name = $rendered_subcontent; } return $engine->render($template, $content); } } |