Exemplo n.º 1
0
	/**
	 * Add a reason template entity
	 *
	 * Makes sure the template file exists and there is no existing template entity with the same name before creating entity
	 *
	 * @param string $template_name The name of the template file (not including .php)
	 * @return mixed Id of created template or false if failure
	 * @author Matt Ryan
	 * @since Reason 4.0 beta 4
	 */
	function reason_add_template($template_name)
	{
		if(!get_template_by_name($template_name))
		{
			if(reason_file_exists('minisite_templates/'.$template_name.'.php'))
			{
				return reason_create_entity( id_of('master_admin'), id_of('minisite_template'), $user_id, $template_name, array('new'=>0));
			}
			else
			{
				trigger_error('Template '.$template_name.' does not exist in filesystem; unable to be added');
				return false;
			}
		}
		else
		{
			trigger_error('Template '.$template_name.' already exists in db; unable to be added');
			return false;
		}
	}
Exemplo n.º 2
0
	function add_complete($unique_name,$name,$css = array(),$template_name,$user_id)
	{
		if($this->get_id())
		{
			trigger_error('add_complete method may only be called on a reasonTheme that does not yet have an ID assigned.');
			return false;
		}
		$all_ok = true;
		$output =  '<h4>Adding theme: '.$unique_name.'</h4>';
		$output .= '<ol>';
		if(empty($template_name))
		{
			$template_name = 'default';
		}
		if(empty($name))
		{
			$name = prettify_string($unique_name);
		}
		$template = get_template_by_name($template_name);
		if(empty($template))
		{
			$all_ok = false;
			$template_id = reason_add_template($template_name);
			if(!empty($template_id))
			{
				$template = new entity($template_id);
				$output .= '<li>Template created ('.$template_name.', id '.$template->id().')</li>';
			}
			else
			{
				$output .= '<li>Unable to create template '.$template_name.'. The template file may not be placed correctly.';
				if($this->test_mode)
				{
					$output .= ' Would abort theme addition.';
				}
				else
				{
					$output .= ' Aborting theme addition.';
				}
				$output .= '</li></ol>';
				return array('success'=>false,'report'=>$output);
			}
		}
		else
		{
			$output .= '<li>Template found ('.$template_name.', id '.$template->id().')</li>';
		}
		
		$theme_id = id_of($unique_name);
		if(!$theme_id)
		{
			$all_ok = false;
			$output .= '<li>Theme with unique name '.$unique_name.' needs to be created</li>';
			if(!$this->test_mode)
			{
				$theme_id = $this->create($unique_name,$name,$template->id(),$user_id);
				if(!$theme_id)
				{
					$output .= '<li>Theme '.$name.' unable to be created; aborting theme addition</li></ol>';
					return array('success'=>false,'report'=>$output);
				}
				else
				{
					$output .= '<li>Theme with unique name '.$unique_name.' created</li>';
				}
			}
			else
			{
				$output .= '<li>Would have attempted to create theme entity.</li>';
			}
		}
		else
		{
			$this->set_id($theme_id);
		}
		
		if($this->get_template_id() != $template->id())
		{
			$all_ok = false;
			$output .= '<li>Current theme template id ('.$this->get_template_id().') not the same as specified in update ('.$template->id().').</li>';
			if($this->test_mode)
			{
				$output .= '<li>Would have set theme template to be id '.$template->id().'</li>';
			}
			else
			{
				if($this->attach_template($template->id()))
				{
					$output .= '<li>Attached template id '.$template->id().' to '.$unique_name.'.</li>';
				}
				else
				{
					$output .= '<li>Unable to attach template for some reason. Aborting theme addition.</li>';
					return array('success'=>false,'report'=>$output);
				}
			}
				
		}
		
		$retrieved_css_entities = array();
		
		foreach($css as $css_name=>$css_info)
		{
			$output .= '<li>CSS: '.$css_name.'<ol>';
			if(empty($css_info['url']))
			{
				$output .= '<li>'.$css_name.' has no url specified; skipping this css item</li></ol></li>';
				continue;
			}
			else
			{
				$css_url = $css_info['url'];
			}
			if(empty($retrieved_css_entities[$css_url]))
			{
				$es = new entity_selector();
				$es->add_type(id_of('css'));
				$es->add_relation('url = "'.addslashes($css_url).'"');
				$es->set_num(1);
				$css_ents = $es->run_one();
				if(!empty($css_ents))
				{
					$retrieved_css_entities[$css_url] = current($css_ents);
				}
			}
			if(empty($retrieved_css_entities[$css_url]))
			{
				$all_ok = false;
				//create css & get id of css
				if($this->test_mode)
				{
					$output .= '<li>Would have created a css entity for '.$css_url.' and attached it to the theme</li>';
				}
				else
				{
					$css_id = reason_create_entity(id_of('master_admin'), id_of('css'), $user_id, $css_name, $css_info);
					if($css_id)
					{
						$output .= '<li>Created a css entity for '.$css_url.'</li>';
					}
					else
					{
						$output .= '<li>Tried to create a css entity for '.$css_url.' but was unsuccessful. Aborting creation of this css item.</li>';
						continue;
					}
				}
			}
			else
			{
				$css_id = $retrieved_css_entities[$css_url]->id();
				$output .= '<li>A css entity exists for '.$css_url.' at Reason id '.$css_id.'</li>';
			}
			if(!empty($css_id))
			{
				// attach css
				$attached_css = $this->get_css();
				if(empty($attached_css[$css_id]))
				{
					$all_ok = false;
					if($this->test_mode)
					{
						$output .= '<li>Would have attached css at '.$css_url.' to '.$unique_name.'.</li>';
					}
					else
					{
						if($this->attach_css($css_id))
						{
							$output .= '<li>Attached css at '.$css_url.' to '.$unique_name.'.</li>';
						}
						else
						{
							$output .= '<li>Unable to attach css at '.$css_url.' to '.$unique_name.'; perhaps there is an unknown problem.</li>';
						}
					}
				}
				else
				{
					$output .= '<li>Css at '.$css_url.' already attached to '.$unique_name.'</li>';
				}
				$output .= '</ol></li>';
			}
		}
		if($all_ok)
		{
			$output .= '<li><strong>Everything OK.</strong> The theme '.$unique_name.' appears to be set up correctly.  No database changes are needed.</li>';
		}
		$output .= '</ol>';
		return array('success'=>true,'report'=>$output);
	}