public function setTemplateName($templatename){ $prefix = $this->get('name'); if($templatename === null) $templatename = ''; // Reset back to default. // First of all, check and see if this is already set. // This will work initially because the initial value is null, and the incoming string will be "". if($this->_selector->get('value') === $templatename) return; $this->_selector->set('value', $templatename); // Ok, it's not.... guess I should remove any existing elements then. foreach($this->_elements as $k => $el){ if($el == $this->_selector) continue; // I suppose I shouldn't remove the selector itself... unset($this->_elements[$k]); } if($templatename){ // Don't forget to prepend the base template! $templatename = substr($this->_selector->get('templatename'), 0, -4) . '/' . $templatename; } else{ // Default! I still need to transpose "" to the default template afterall. $templatename = $this->_selector->get('templatename'); } // Translate the filename to an absolute path. $tpl = Core\Templates\Template::ResolveFile($templatename); if (!$tpl) return null; // Scan through $tpl and find any {insertable} tag. $tplcontents = file_get_contents($tpl); preg_match_all('/\{insertable(.*)\}(.*)\{\/insertable\}/isU', $tplcontents, $matches); // Guess this page had no insertables. if (!sizeof($matches[0])){ return; } foreach ($matches[0] as $k => $v) { // The contents of the {insertable ...} tag. $tag = trim($matches[1][$k]); // The contents inside of the tags. $content = trim($matches[2][$k]); $default = $content; // To make this tag searchable easily, convert it to an xml element and get the attributes from that. $simple = new SimpleXMLElement('<insertable ' . $tag . '/>'); $attributes = array(); foreach($simple->attributes() as $k => $v){ $attributes[$k] = (string)$v; } $name = $attributes['name']; $title = isset($attributes['title']) ? $attributes['title'] : $name; $type = isset($attributes['type']) ? $attributes['type'] : null; // null means an automatic type. if(isset($attributes['default'])) $default = $attributes['default']; // This insertable may already have content from the database... if so I want to pull that! $i = new InsertableModel($this->get('baseurl'), $name); if ($i->get('value') !== null) $content = $i->get('value'); // These will be the default options for creating form elements, extend them as necessary. $elementoptions = array( 'name' => $prefix . "[$name]", 'title' => $title, 'value' => $content, ); if(isset($attributes['description'])) $elementoptions['description'] = $attributes['description']; // If the type is null, try to determine the form type based on the content. if($type === null){ if (strpos($default, "\n") === false && strpos($default, "<") === false) { $type = 'text'; } elseif (preg_match('/<img(.*?)>/i', $default)) { $type = 'image'; } else { $type = 'wysiwyg'; } } // Some elements have specific options that need to be set. switch($type){ case 'image': $type = 'file'; $elementoptions['accept'] = 'image/*'; $elementoptions['basedir'] = 'public/insertable'; break; case 'file': $elementoptions['basedir'] = 'public/insertable'; break; case 'select': $elementoptions['options'] = array_map('trim', explode('|', $attributes['options'])); break; } // Add the actual elements now! $this->addElement($type, $elementoptions); } }