コード例 #1
0
 /**
 * Allows you to load a view and pass data to it
 *
 * Renders a navigation structure using the <a href="[user_guide_url]libraries/menu">Menu class</a>.
 *
 * The <a href="[user_guide_url]helpers/fuel_helper">fuel_block helper</a> function is an alias to this method.
 *
 	<ul>
 		<li><strong>view</strong> - the name of the view block file. Also can be the first parameter of the method</li>
 		<li><strong>vars</strong>: an array of variables to pass to the block. Also can be the second parameter of the method.</li>
 		<li><strong>scope</strong>: a string value used for placing the variables into a certain scope to prevent conflict with other loaded variables. Default behavior will load variables in to a global context. The value of TRUE will generate one for you.</li>
 		<li><strong>view_string</strong> - a string variable that represents the block</li>
 		<li><strong>model</strong> - the name of a model to automatically load for the block</li>
 		<li><strong>find</strong> - the name of the find method to run on the loaded model</li>
 		<li><strong>select</strong> - select parameters to run for the find method</li>
 		<li><strong>where</strong> - where parameters to run for the find method</li>
 		<li><strong>order</strong> - the order the find method should return</li>
 		<li><strong>limit</strong> - the limit number of results to be returned by the find method</li>
 		<li><strong>offset</strong> - the find results returned offset value</li>
 		<li><strong>return_method</strong>: the return method the find query should use</li>
 		<li><strong>assoc_key</strong>: the column name to be used as the associative key in the find method</li>
 		<li><strong>data</strong>: the data values to be passed to the block. This variable get's automatically set if you specify the model and find method</li>
 		<li><strong>editable</strong>: css class styles to apply to menu items... can be a nested array</li>
 		<li><strong>parse</strong>: determines whether to parse the contents of the block. The default is set to 'auto'</li>
 		<li><strong>cache</strong>: determines whether to cache the block. Default is false</li>
 		<li><strong>mode</strong>: explicitly will look in either the CMS or the views/_blocks folder</li>
 		<li><strong>module</strong>: the name of the module to look in for the block</li>
 		<li><strong>language</strong>: the language version to use for the block. Must be a value specified found in the 'languages' options in the FUEL configuration</li>
 		<li><strong>use_default</strong>: determines whether to find a non-language specified version of a block with the same name if the specified language version is not available in the CMS</li>
 	</ul>
 	* @access	public
 * @param	mixed	Array of parameters
 * @param	array	Array of variables
 * @param	boolean	Determines whether to check the CMS for the block or not (alternative to using the "mode" parameter)
 * @param	boolean	Determines whether to scope the variables. A string can also be passed otherwise the scope value will be created for you
 * @return	string
 */
 public function render($params, $vars = array(), $check_db = TRUE, $scope = NULL)
 {
     $this->CI->load->library('parser');
     $valid = array('view' => '', 'view_string' => FALSE, 'model' => '', 'find' => 'all', 'select' => NULL, 'where' => '', 'order' => '', 'limit' => NULL, 'offset' => 0, 'return_method' => 'auto', 'assoc_key' => '', 'data' => array(), 'editable' => TRUE, 'parse' => 'auto', 'vars' => array(), 'scope' => $scope, 'cache' => FALSE, 'mode' => 'auto', 'module' => '', 'language' => NULL, 'use_default' => TRUE);
     // for convenience
     if (!is_array($params)) {
         $new_params = array();
         if (strpos($params, '=') === FALSE) {
             $new_params['view'] = $params;
         } else {
             $this->CI->load->helper('array');
             $new_params = parse_string_to_array($params);
         }
         $params = $new_params;
     }
     $p = array();
     foreach ($valid as $param => $default) {
         $p[$param] = isset($params[$param]) ? $params[$param] : $default;
     }
     // pull from cache if cache is TRUE and it exists
     if ($p['cache'] === TRUE) {
         $this->CI->load->library('cache');
         $cache_group = $this->CI->fuel->config('page_cache_group');
         $cache_id = !empty($p['view_string']) ? $p['view_string'] : $p['view'];
         $cache_id = md5($cache_id);
         $cache = $this->CI->cache->get($cache_id, $cache_group);
         if (!empty($cache)) {
             return $cache;
         }
     }
     // load the model and data
     $p['vars'] = (array) $p['vars'];
     $vars = (is_array($vars) and !empty($vars)) ? array_merge($p['vars'], $vars) : $p['vars'];
     if (!empty($p['model'])) {
         $data = fuel_model($p['model'], $p);
         $module = $this->CI->fuel->modules->get($p['model']);
         if ($module) {
             $model_name = $module->model()->friendly_name(TRUE);
             if (!empty($model_name)) {
                 $var_name = $module->model()->friendly_name(TRUE, FALSE);
                 $vars[$var_name] =& $data;
                 // for convenience
                 $vars['data'] =& $data;
             }
         }
     } else {
         $vars['data'] = $p['data'];
     }
     $output = '';
     // load proper view to parse. If a view is given then we first look up the name in the DB
     $view = '';
     if (!empty($p['view_string'])) {
         $view = $p['view_string'];
     } else {
         if (!empty($p['view'])) {
             $is_module_block = FALSE;
             $view_path = 'views/_blocks/';
             if (!empty($p['module']) and defined('MODULES_PATH')) {
                 if ($p['module'] == 'app' or $p['module'] == 'application') {
                     $view_path = APPPATH . $view_path;
                 } else {
                     $view_path = MODULES_PATH . $p['module'] . '/' . $view_path;
                 }
                 $is_module_block = TRUE;
             } else {
                 $view_path = APPPATH . $view_path;
             }
             // get language value
             if ($this->fuel->language->has_multiple()) {
                 $language = !empty($p['language']) ? $p['language'] : $this->fuel->language->detect();
             } else {
                 $language = $this->fuel->language->default_option();
             }
             // test that the file exists in the associated language
             if (!empty($language) and !$this->fuel->language->is_default($language)) {
                 $view_tmp = 'language/' . $language . '/' . $p['view'];
                 if (file_exists($view_path . $view_tmp . EXT)) {
                     $view_file = $view_path . $view_tmp . EXT;
                 }
             }
             if (empty($view_file)) {
                 $view_file = $view_path . $p['view'] . EXT;
             }
             $p['mode'] = strtolower($p['mode']);
             // only check database if the fuel_mode does NOT equal 'views, the "only_views" parameter is set to FALSE and the view name does not begin with an underscore'
             if ($check_db and ($p['mode'] == 'auto' and $this->mode() != 'views' or $p['mode'] == 'cms') and substr($p['view'], 0, 1) != '_') {
                 $this->fuel->load_model('fuel_blocks');
                 // find the block in FUEL db
                 $block = $this->CI->fuel_blocks_model->find_one_by_name_and_language($p['view'], $language);
                 // if there is no block found with that language we will try to find one that may not have a language associated with it
                 if (!isset($block->id) and $p['use_default']) {
                     $block = $this->CI->fuel_blocks_model->find_one_by_name($p['view']);
                 }
                 if (isset($block->id)) {
                     if (strtolower($p['parse']) == 'auto') {
                         $p['parse'] = TRUE;
                     }
                     $view = $block->view;
                     if ($p['editable'] === TRUE) {
                         $view = fuel_edit($block->id, 'Edit Block: ' . $block->name, 'blocks') . $view;
                     }
                 } else {
                     if (file_exists($view_file)) {
                         // pass in reference to global CI object
                         $vars['CI'] =& $this->CI;
                         // pass along these since we know them... perhaps the view can use them
                         $view = $is_module_block ? $this->CI->load->module_view($p['module'], '_blocks/' . $p['view'], $vars, TRUE, $p['scope']) : $this->CI->load->view('_blocks/' . $p['view'], $vars, TRUE, $p['scope']);
                     }
                 }
             } else {
                 if (file_exists($view_file)) {
                     // pass in reference to global CI object
                     $vars['CI'] =& $this->CI;
                     // pass along these since we know them... perhaps the view can use them
                     $view = $is_module_block ? $this->CI->load->module_view($p['module'], '_blocks/' . $p['view'], $vars, TRUE, $p['scope']) : $this->CI->load->view('_blocks/' . $p['view'], $vars, TRUE, $p['scope']);
                 }
             }
         }
     }
     // parse the view again to apply any variables from previous parse
     $output = $p['parse'] === TRUE ? $this->CI->parser->parse_string($view, $vars, TRUE) : $view;
     if ($p['cache'] === TRUE) {
         $this->CI->cache->save($cache_id, $output, $cache_group, $this->CI->fuel->config('page_cache_ttl'));
     }
     return $output;
 }
コード例 #2
0
ファイル: fuel_helper.php プロジェクト: rodrigowebe/FUEL-CMS
/**
 * Creates a menu structure
 *
 * @access	public
 * @param	mixed
 * @return	string
 */
function fuel_nav($params = array())
{
	$CI =& get_instance();
	$CI->load->library('menu');
	$valid = array( 'items' => array(),
					'file' => 'nav',
					'var' => 'nav',
					'root' => NULL,
					'group_id' => 1,
					'parent' => NULL, 
					'render_type' => 'basic', 
					'active_class' => 'active', 
					'active' => (uri_path(FALSE) !== '') ? uri_path(FALSE) : 'home',
					'styles' => array(),
					'first_class' => 'first', 
					'last_class' => 'last', 
					'depth' => NULL, 
					'use_titles' => TRUE,
					'container_tag' => 'ul',
					'container_tag_attrs' => '',
					'container_tag_id' => '',
					'container_tag_class' => '',
					'cascade_selected' => TRUE,
					'include_hidden' => FALSE,
					'item_tag' => 'li',
					'item_id_prefix' => '',
					'item_id_key' => 'id',
					'pre_render_func' => '',
					'delimiter' => FALSE,
					'arrow_class' => 'arrow',
					'display_current' => TRUE,
					'home_link' => 'Home',
					'order' => 'asc',
					'exclude' => array(),
					'return_normalized' => FALSE,
					);

	if (!is_array($params))
	{
		$CI->load->helper('array');
		$params = parse_string_to_array($params);
	}

	$p = array();
	foreach($valid as $param => $default)
	{
		$p[$param] = (isset($params[$param])) ? $params[$param] : $default;
	}
	
	if (empty($p['items']))
	{
		// get the menu data based on the FUEL mode or if the file parameter is specified use that
		if ($CI->config->item('fuel_mode', 'fuel') == 'views' OR !empty($params['file']))
		{
			if (file_exists(APPPATH.'views/_variables/'.$p['file'].'.php'))
			{
				include(APPPATH.'views/_variables/'.$p['file'].'.php');
			}
			else
			{
				$$p['var'] = array();
			}
		}

		// using FUEL admin
		else
		{
			if ($CI->config->item('fuel_mode', 'fuel') != 'cms')
			{
				// load in navigation file as a starting poing
				if (file_exists(APPPATH.'views/_variables/'.$p['file'].'.php'))
				{
					$p['root_value'] = NULL;
					include(APPPATH.'views/_variables/'.$p['file'].'.php');
				}
			}
			// now load from FUEL and overwrite
			$CI->load->module_model(FUEL_FOLDER, 'navigation_model');
			
			// grab all menu items by group
			$menu_items = $CI->navigation_model->find_all_by_group($p['group_id']);

			// if menu items isn't empty, then we overwrite the variable with those menu items and change any parent value'
			if (!empty($menu_items)) 
			{
				$$p['var'] = $menu_items;
				
				// if parent exists, then assume it is a uri location and you need to convert it to a database id value
				if (!empty($p['parent']) AND is_string($p['parent']))
				{
					// WARNING... it is possible to have more then one navigation item with the same location so it's best not to location values but instead use ids
					$parent = $CI->navigation_model->find_by_location($p['parent']);
					if (!empty($parent['id']))
					{
						$p['parent'] = $parent['id'];
					}
				}

				// if active exists, then assume it is a uri location and you need to convert it to a database id value
				if (!empty($p['active']) AND is_string($p['active']))
				{
					// WARNING... it is possible to have more then one navigation item with the same location so it's best not to location values but instead use ids'
					$active = $CI->navigation_model->find_by_location($p['active'], $p['group_id']);
					if (!empty($active['id']))
					{
						$p['active'] = $active['id'];
					}
				}
				$p['root_value'] = 0;
			}
		}
	}
	else
	{
		$$p['var'] = $p['items'];
	}
	if (!empty($params['root'])) $p['root_value'] = $params['root'];

	$CI->menu->reset();
	$CI->menu->initialize($p);
	
	$items = (!empty($$p['var'])) ? $$p['var'] : array();
	if (!empty($p['exclude']))
	{
		$p['exclude'] = (array) $p['exclude'];
		foreach($items as $key => $item)
		{
			if (is_int($key) AND !empty($item['location']))
			{
				$check = $item['location'];
			}
			else
			{
				$check = $key;
			}
			if (in_array($check, $p['exclude']))
			{
				unset($items[$key]);
			}
			
		}
	}
	
	if ($p['return_normalized'] !== FALSE)
	{
		return $CI->menu->normalize_items($items);
	}
	return $CI->menu->render($items, $p['active'], $p['parent']);
}
コード例 #3
0
ファイル: Fuel_modules.php プロジェクト: huayuxian/FUEL-CMS
 /**
  * Loads a module model and creates a variable in the view that you can use to merge data 
  *
  * @access	public
  * @param	mixed  A string value of "all", "one", "key", "find" or "by" OR A key value array of options which include "find", "select", "where", "order", "limit", "offset", "return_method", "assoc_key", "var", "module", and "params"(optional)
  * @param	mixed  Where condition (since it's most common parameter) (optional)
  * @return	string
  */
 public function find($params = array(), $where = NULL)
 {
     $valid = array('find' => 'all', 'select' => NULL, 'where' => '', 'order' => '', 'limit' => NULL, 'offset' => 0, 'return_method' => 'auto', 'assoc_key' => '', 'var' => '', 'module' => '', 'params' => array());
     $model = $this->model();
     $native = TRUE;
     if (is_string($params)) {
         if (preg_match('#^(all|one|key|find|by)#', $params)) {
             $find = $params;
             $params = array();
             $params['find'] = $find;
             $params['where'] = $where;
         } elseif (method_exists($model, 'find_' . $params)) {
             $find = $params;
             $args = $where;
             $native = FALSE;
         } else {
             $this->CI->load->helper('array');
             $params = parse_string_to_array($params);
         }
     }
     if ($native) {
         foreach ($valid as $p => $default) {
             ${$p} = isset($params[$p]) ? $params[$p] : $default;
         }
     }
     // to get around escapinng issues we need to add spaces after =
     if (is_string($where)) {
         $where = preg_replace('#([^>|<|!])=#', '$1 = ', $where);
     }
     // run select statement before the find
     if (!empty($select)) {
         $model->db()->select($select, FALSE);
     }
     // retrieve data based on the method
     if ($native) {
         $data = $model->find($find, $where, $order, $limit, $offset, $return_method, $assoc_key);
     } else {
         $args = array_merge(array($find), $args);
         $data = call_user_func_array(array($model, 'find'), $args);
     }
     if ($data !== FALSE) {
         if (is_array($data) and key($data) === 0) {
             $var = $model->friendly_name(TRUE);
         } else {
             $var = $model->singular_name(TRUE);
         }
     }
     $vars[$var] = $data;
     // load the variable for the view to use
     $this->CI->load->vars($vars);
     // set the model to readonly so no data manipulation can occur
     $model->readonly = TRUE;
     return $data;
 }
コード例 #4
0
 /**
  * Uploads a static'navigation structure which is most like the <span class="file">fuel/application/views/_variables/nav.php</span> file
  *
  * @access	public
  * @param	array	config preferences (optional)
  * @return	boolean
  */
 public function upload($params = array())
 {
     $this->CI->load->library('menu');
     $this->CI->load->helper('file');
     $this->CI->load->helper('security');
     $valid = array('file_path' => APPPATH . 'views/_variables/nav.php', 'group_id' => 'main', 'var' => 'nav', 'clear_first' => TRUE, 'language' => 'english');
     if (!is_array($params)) {
         $this->CI->load->helper('array');
         $params = parse_string_to_array($params);
     }
     $p = array();
     foreach ($valid as $param => $default) {
         $p[$param] = isset($params[$param]) ? $params[$param] : $default;
     }
     // no longer needed
     unset($params);
     // extract out params to make it easier below
     extract($p);
     $error = FALSE;
     // read in the file so we can filter it
     $file = read_file($file_path);
     if (empty($file)) {
         return FALSE;
     }
     // strip any php tags
     $file = str_replace('<?php', '', $file);
     // run xss_clean on it
     $file = xss_clean($file);
     // now evaluate the string to get the nav array
     @eval($file);
     if (!empty(${$var})) {
         $nav = $this->CI->menu->normalize_items(${$var});
         if (is_true_val($clear_first)) {
             $this->model()->delete(array('group_id' => $group_id));
         }
         // save navigation group
         $group = $this->group($group_id);
         // set default navigation group if it doesn't exist'
         if (!isset($group->id)) {
             $group->name = 'main';
             $id = $group->save();
             $group_id = $group->id;
         }
         // convert string ids to numbers so we can save... must start at last id in db
         $ids = array();
         $i = $this->model()->max_id() + 1;
         foreach ($nav as $key => $item) {
             // if the id is empty then we assume it is the homepage
             if (empty($item['id'])) {
                 $item['id'] = 'home';
                 $nav[$key]['id'] = 'home';
             }
             $ids[$item['id']] = $i;
             $i++;
         }
         // now loop through and save
         $cnt = 0;
         foreach ($nav as $key => $item) {
             $save = array();
             $save['id'] = $ids[$item['id']];
             $save['nav_key'] = empty($key) ? 'home' : $key;
             $save['group_id'] = $group_id;
             $save['label'] = $item['label'];
             $save['parent_id'] = empty($ids[$item['parent_id']]) ? 0 : $ids[$item['parent_id']];
             $save['location'] = $item['location'];
             $save['selected'] = !empty($item['selected']) ? $item['selected'] : $item['active'];
             // must be different because "active" has special meaning in FUEL
             $save['language'] = !empty($item['language']) ? $item['language'] : $language;
             // fix for homepage links
             if (empty($save['selected']) and $save['nav_key'] == 'home') {
                 $save['selected'] = 'home$';
             }
             $save['hidden'] = is_true_val($item['hidden']) ? 'yes' : 'no';
             $save['published'] = 'yes';
             $save['precedence'] = !empty($item['precedence']) ? $item['precedence'] : $cnt;
             if (is_array($item['attributes'])) {
                 $attr = '';
                 foreach ($item['attributes'] as $key => $val) {
                     $attr .= $key . '="' . $val . '" ';
                 }
                 $attr = trim($attr);
             } else {
                 $save['attributes'] = $item['attributes'];
             }
             if (!$this->model()->save($save)) {
                 $error = TRUE;
                 break;
             }
             $cnt++;
         }
     } else {
         $error = TRUE;
     }
     return !$error;
 }