⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Default field values (for add form)



bianconeri
  • profile picture
  • Member

Posted 22 May 2013 - 23:34 PM

Guys,

 

My solution.

 

1. protected function get_string_input($field_info, $value) {
2.        $value = !is_string($value) ? '' : str_replace('"', """, $value);
3.        if (empty($value))
4.            $value = $field_info->default;
5.               ...    }
 

I've add two lines 3 and 4.
I dont understand the param extra in field_type($field, $type, $extras)


davidoster
  • profile picture
  • Member

Posted 23 May 2013 - 03:52 AM

I made a simple post a while back about getting the default values from the database, the easy way.

Check it here, /topic/1680-how-to-get-default-values-from-the-database/


superdan
  • profile picture
  • Member

Posted 13 August 2013 - 17:13 PM

hi all

 

custom_grocery_crud is a great addon!
 

just the dropdown option seems wont work

 

i write

$this->custom_grocery_crud->field_set_defaults('IDNAZIONE' , 'dropdown', '1');

and the error is

Fatal error: Unsupported operand types in /var/www/........./crud/application/libraries/Grocecery_CRUD.php on line 2313

anyone can help me?
Thanks!


Roeland W
  • profile picture
  • Member

Posted 04 April 2014 - 20:10 PM

If it is some value that you don't need to show in the field list, you can create a hidden default method:

 

Create this in a helper:

function default_hidden_field($name, $value)
{
    return "<input id='field-$name' type='hidden' name='$name' value='$value' />";
}

Then use this helper in a callback function

    public function _callback_ins_type($value, $row)
    {
        return default_hidden_field('ins_fk_ins_type_id', '3');
    }

Set the callback:

$this->crud->callback_field('ins_fk_ins_type_id', array($this, '_callback_ins_type'));

larasmith
  • profile picture
  • Member

Posted 22 July 2014 - 05:44 AM

 

 

Hi again

 

I've made another enhancement to my subclass.

 

The facility to have dynamic where clauses.

 

here's an explanation :

 

Say I cave a catalogue item (table 1) that has images (table 2)

 

I want all my images - site wide - to be in my image table (table 2)

 

I may have other tables (collections) that have images

 

so clearly I need to have "image id" and "foreign_id" in my image table.

 

"Image id" identifies a unique image and "foreign_id" identifies a set of images.

 

I also have "collection_name" in my images table (table 2) so that I dont get any id collisions.

 

and I have an image title in the image table.

 

I also want to have an image id of my default image in the catalogue table

 

In this case my "foreign_id" would be the "catalogue_id" of my catalogue item.

and "collection_name" would be 'catalogue"

 

You still with me ??

 

so when I edit a catalogue item I want also to access a relationship 

show me all of the images in (table 2) where "collection_name" == 'catalogue' and "foreign_id" == "catalogue_id"

 

getting clearer??

 

we've all had to do this - in grocerycrud - using callbacks

 

because we cant set the "where part" in set_relation() to a value from the table in a controller

unless we do it in a callback - (which is fine - but this is easier)

 

so - to summarize:

 

when I edit a catalogue item - I want to be able to add images to it

I want to be able to set a default image

 

so what if for the above example we could do this :

 

for example:

   $crud->set_relation('Catalogue_Default_Image_id','Images','Images_Title',array('Images_Foreign_ID'=>'{Catalogue_ID}'));

 

The actual set_relation reads like this:

 

   $crud->set_relation('IMG_ID','Images','Title',array('F_ID'=>'{ID}'));

 

just like in the docs http://www.grocerycrud.com/documentation/options_functions/set_relation

 

 

you can have the system replace values that are surrounded by curly braces "{}" in the where clause.

but ONLY in the value field - it doesnt make sense to change the key field at all.

 

 

so  example    $crud->set_relation('IMG_ID','Images','Title',array('F_ID'=>'{ID}','Image_Type'=>"Catalogue"));

 

and a row in the catalogue like this

 

ID=>2, IMG_ID->11, Text->'something about a catalogue item'

 

at run time the resulting where part would end up 

 

array('F_ID'=>'2','Image_Type'=>"Catalogue") 

 

Hope my explanation make sense to everyone

 

so here's my updated subclass

note -

I make the mods in a subclass so that I NEVER have to change the underlying library.

Also, I have to do some more reading to find out if I can do all this in a model which would take the hassle out of subclassing

 

I've also made some minor changes to the set_css and set_js functionality so that I can use them in my controller and other 

libraries.

 

anyway here you go - enjoy

 

its kinda big - hope thats ok  - maybe next time I'll just post a file

<?php
/**
 * PHP grocery CRUD
 *
 * A Codeigniter library that creates a CRUD automatically with just few lines of code.
 *
 * Copyright (C) 2010 - 2012  John Skoumbourdis. 
 *
 * LICENSE
 *
 * Grocery CRUD is released with dual licensing, using the GPL v3 (license-gpl3.txt) and the MIT license (license-mit.txt).
 * You don't have to do anything special to choose one license or the other and you don't have to notify anyone which license you are using.
 * Please see the corresponding license file for details of these licenses.
 * You are free to use, modify and distribute this software, but all copyright information must remain.
 *
 * @package    	grocery CRUD
 * @copyright  	Copyright (c) 2010 through 2012, John Skoumbourdis
 * @license    	https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
 * @version    	1.3
 * @author     	John Skoumbourdis <scoumbourdisj@gmail.com>
 */

// ------------------------------------------------------------------------
include_once(dirname(__FILE__)."/grocery_crud.php");


/**
 * PHP grocery CRUD - Customized
 *
 * Creates a full functional CRUD with few lines of code.
 *
 * @package    	grocery CRUD 
 * @author     	John Skoumbourdis <scoumbourdisj@gmail.com>
 * @license     https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
 * @link		http://www.grocerycrud.com/documentation
 *
 * @hacker      Ez (ezgoen@gmail.com) - email me for my real identity
 */

class custom_grocery_crud extends grocery_CRUD{

	protected $field_default_values = array();
	protected $inline='';
	
	function __construct()
	{
		parent::__construct();
		
		//I need these BEFORE render - so we can add include files
		$this->_initialize_variables();
	}
	
	
	/**
	 * override get_layout
	 *
	 * Created by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				I've added functionality to add inline code - like 
	 *				<style type=\"text/css\" > Blah </style>
	 *				<script type=\"text/javascript\"> Blah </script>";
	 *				and so on
	 *
	 *				The gc only has this at the time of the call to render
	 *				so we have to handle it separately
	 *				This is the last function called by render()
	 *					 
	 */
	protected function get_layout()	{		
		//call the parent method
		$result = parent::get_layout();
		//add our stuff
		$result->inline=$this->inline;
		return $result;
	}
	
	/**	 * Helper Function
	 *
	 * Created by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 */
	protected function get_field_default_values($field){
		return array_key_exists($field,$this->field_default_values) ? $this->field_default_values[$field] : null;
	}

	/**
	 * Use this function exactly the same way you would use field_type()
	 * see : http://www.grocerycrud.com/documentation/options_functions/field_type
	 *
	 * Created by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				in the case of an ADD data form I want to provide default values to the fields.
	 *				I may also want to make the fields with default values readonly or completely hidden
	 *				The parent object does not allow/facilitate addidtion of default values to visible 
	 *				fields other than:
	 *					hidden
	 *					invisible
	 *					password
	 *					enum
	 *					set
	 *					dropdown
	 *					multiselect
	 *
	 *				I havent fooled around with invisible - so I cant comment on anything to do with that.
	 *				however I want my other fields - most commonly string and integer to have default values
	 *				additionally I may also want thos values to either editable OR readonly and visible at 
	 *				the same time AND I want my default values (if they are readonly) to go into the database
	 *				
	 */
	public function field_set_defaults($field,$type,$value){
		  $this->field_default_values[$field]=array('type'=>$type,'value'=>$value);
	}

	
	//for backward compatibility
	public function set_css($css_file)	{
		$this->add_include($css_file);
	}

	//for backward compatibility
	public function set_js($js_file){
		$this->add_include($js_file);
	}
	/**
	 * Use this function exactly the same way you would use set_js() or set_css
	 *
	 * Created by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				I wanted my controller to be able to do this too
	 *				I also use a menu library that I kinda adapted 
	 *				it uses some jquery as well - so its better everything uses the same function
	 *				to add js and script files so we dont :
	 *					end up with duplicates includes
	 *					degrade page load times
	 *				
	 *				This function also allows us to add files that are outside the grocerycrud 
	 *				dircetory tree
	 */
	public function add_include($inc_file){
		$fn = (strpos($inc_file,"/") === 0 ) ?  substr($inc_file, 1)  : $inc_file;
		if (strripos($fn,'.js',-3) !== False)  {  
			if (! file_exists("$fn")){
				$fn = $this->default_javascript_path."/$fn" ;
			}
			if(file_exists("$fn")){
				$this->js_files[sha1($fn)] = base_url().$fn;
			} else {
				throw new Exception("Missing JavaScript (.js) file. Tried : $inc_file and $fn");
			}
		} elseif (strripos($inc_file,'.css',-4) !== False) { 
			if (! file_exists("$fn")){
				$fn = $this->default_css_path."/$fn" ;
			}
			if(file_exists("$fn")){
				$this->css_files[sha1($fn)] = base_url().$fn;
			} else {
				throw new Exception("Missing Stylesheet (.css) file. Tried: $inc_file and $fn");
			}
		} else {
			throw new Exception("Request to include unknown file type: $inc_file");
		}
	}

	
	public function add_inline($str){
		$this->inline.="\n$str";
	}
	
	
	/**
	 * Work out what to do with input fields for add record
	 * 
	 *
	 * Modified/Hacked by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				in the case of an ADD data form I want to provide default values to the fields.
	 *				I may also want to make the fields with default values readonly or completely hidden
	 */
	protected function get_add_input_fields($field_values = null)
	{
		$fields = $this->get_add_fields();
		$types 	= $this->get_field_types();
		
		$input_fields = array();
		
		foreach($fields as $field_num => $field)
		{	
			$field_info = $types[$field->field_name];
			
			$field_value = !empty($field_values) && isset($field_values->{$field->field_name}) ? $field_values->{$field->field_name} : null;
	

			$field_default_values=$this->get_field_default_values($field->field_name);
			if (! empty($field_default_values) and empty($field_value) ){
				//if $field_info is not set at this point we need to construct one
				if (!isset($field_info)){
						$field_info = (object)array();
				}
				$field_info->custom_default=True; //this is what we look for to ensure we only change the behaviour we want to
				$field_info->crud_type=$field_default_values['type'];
				$field_info->type=$field_default_values['type'];
				$field_info->default=$field_default_values['value'];
				$field_info->extras=$field_default_values['value'];
				$field_value=$field_default_values['value'];
			}
			
			//no changes below here
			if(!isset($this->callback_add_field[$field->field_name]))
			{
				$field_input = $this->get_field_input($field_info, $field_value);
			}
			else
			{
				$field_input = $field_info;
				$field_input->input = call_user_func($this->callback_add_field[$field->field_name], $field_value, null, $field_info);
				//make our little change
				//allow default field rendering behaviour if user returns false
				//in the callback
				if ($field_input->input === False) {
					$field_input = $this->get_field_input($field_info, $field_value);
				}
			}
			
			switch ($field_info->crud_type) {
				case 'invisible':
					unset($this->add_fields[$field_num]);
					unset($fields[$field_num]);
					continue;
				break;
				case 'hidden':
					$this->add_hidden_fields[] = $field_input;
					unset($this->add_fields[$field_num]);
					unset($fields[$field_num]);
					continue;
				break;
			}			
			
			$input_fields[$field->field_name] = $field_input; 
		}
		
		return $input_fields;
	}
	
	/**
	 * Get the html input for the specific field with the 
	 * current value
	 * 
	 * @param object $field_info
	 * @param string $value
	 * @return object
	 *
	 * Modified/Hacked by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				In the case of an ADD data form I want my readonly default values to go into
	 *				the database.
	 *				The default behaviour of 'readonly' fields is to not actually "be a form field"
	 *				they are instead just text in a div like this :
	 *					<div id="field-YOUR_FIELD" class="readonly_label">Your Default Value</div>
	 *				this modification adds a hidden form field that will put the default values you 
	 *				want into the database like this :
	 *					<div id="field-YOUR_FIELD" class="readonly_label">Your Default Value</div>
	 *					<input id='field-YOUR_FIELD' type='hidden' name='YOUR_FIELD' value='Your Default Value' />
	 *
	 * Dynamic Where Clause MOD 
	 * @explanation:
	 *	 			To Facilitate a call to set_relation like: 
	 *
	 *				$crud->set_relation('OUR_ID','Other_Table','Other_Table_Field1',
	 *									array('Other_Table_Field2'=>'{This_Table_Some_Field_Value}'));
	 *																 ^^ notice the curly braces  ^^
	 *
	 *				Changed the argument lists to include current row data
	 *				that we then pass on to get_relation_input()
	 */
	protected function get_field_input($field_info, $value = null,$row_values = null)
	{
			$real_type = $field_info->crud_type;
			
			$types_array = array(
					'integer', 
					'text',
					'true_false',
					'string', 
					'date',
					'datetime',
					'enum',
					'set',
					'relation', 
					'relation_n_n',
					'upload_file',
					'hidden',
					'password', 
					'readonly',
					'dropdown',
					'multiselect'
			);
			if (in_array($real_type,$types_array)) {
				/* A quick way to go to an internal method of type $this->get_{type}_input . 
				 * For example if the real type is integer then we will use the method
				 * $this->get_integer_input
				 *  */
				 
				//Dynamic Where Clause : added $row_values  to the call to get_relation_input()
				//here I'm just handling the relation type - could do more types later
				//not sure why I'd want to tho
				if (in_array($real_type,array('relation'))){
					$field_info->input = $this->{"get_".$real_type."_input"}($field_info, $value, $row_values ); 
				} else {
					$field_info->input = $this->{"get_".$real_type."_input"}($field_info,$value ); 
				}
				//Default Value mod here...
				if ((property_exists($field_info,'custom_default')) and ( $real_type=="readonly" )) {
					$field_info->crud_type='hidden';
					$field_info->type='hidden';
					$field_info->input .= $this->{"get_hidden_input"}($field_info,$value); 
					$field_info->crud_type=$real_type;
					$field_info->type=$real_type;
				}
				//no changes below here
			}
			else
			{
				$field_info->input = $this->get_string_input($field_info,$value);
			}
		
		return $field_info;
	}


	/**
	 * get_edit_input_fields()
	 * 
	 * @param object $field_values
	 * @return object
	 *
	 * Modified/Hacked by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				extend the functionality - so that a person can do processing
	 *				using the current row data - but still alow default rendering
	 *				by returning boolean False instead of a string.
	 *
	 * Dynamic Where Clause MOD 
	 * @explanation:
	 *	 			To Facilitate a call to set_relation like: 
	 *
	 *				$crud->set_relation('OUR_ID','Other_Table','Other_Table_Field1',
	 *									array('Other_Table_Field2'=>'{This_Table_Some_Field_Value}'));
	 *																 ^^ notice the curly braces  ^^
	 *
	 *				Changed the argument lists for get_field_input() to include current row data
	 *
	 *
	 */
	protected function get_edit_input_fields($field_values = null)
	{
		$fields = $this->get_edit_fields();
		$types 	= $this->get_field_types();
		
		$input_fields = array();
		
		foreach($fields as $field_num => $field)
		{
			$field_info = $types[$field->field_name];			
			
			$field_value = !empty($field_values) && isset($field_values->{$field->field_name}) ? $field_values->{$field->field_name} : null;
			if(!isset($this->callback_edit_field[$field->field_name]))
			{			
			    // Dynamic Where Clause MOD - ADDDED $field_values to the call to get_field_input()
				$field_input = $this->get_field_input($field_info, $field_value, $field_values );
			}
			else
			{
				$primary_key = $this->getStateInfo()->primary_key;
				$field_input = $field_info;
				$field_input->input = call_user_func($this->callback_edit_field[$field->field_name], $field_value, $primary_key, $field_info, $field_values);
				//Default Values Mod
				//allow default field rendering behaviour if user returns false
				//in the callback
				if ($field_input->input === False) {
					// Dynamic Where Clause MOD - ADDDED $field_values to the call to get_field_input()
					$field_input = $this->get_field_input($field_info, $field_value, $field_values);
				}
			}
			
			switch ($field_info->crud_type) {
				case 'invisible':
					unset($this->edit_fields[$field_num]);
					unset($fields[$field_num]);
					continue;
				break;
				case 'hidden':
					$this->edit_hidden_fields[] = $field_input;
					unset($this->edit_fields[$field_num]);
					unset($fields[$field_num]);
					continue;
				break;				
			}			
			
			$input_fields[$field->field_name] = $field_input; 
		}
		
		return $input_fields;
	}	
	
	
	
	/**
	 * override get_relation_input
	 *
	 * Created by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				Dynamic Where clause Mod
	 *				This is where the magic (well, not really) happens 
	 *				we alter the where clause just before 
	 *				the call to th eparent get_relation_input()
	 *					 
	 */
	
	protected function get_relation_input($field_info, $value, $row_values){
		
		list($_field_name , $_related_table, $_related_title_field, $_where_clause , $_order_by ) = $field_info->extras;
		
		if (!empty($row_values) and !empty($_where_clause)){
			$wc=(array) $_where_clause;
			foreach ($wc as $where_key => $where_value){
				foreach ($row_values as $key => $value){
					if ($where_value=="{".$key."}") {
						$_where_clause[$where_key]=$value;
					}					
				}
			}
		}
		$field_info->extras= array($_field_name , $_related_table, $_related_title_field, $_where_clause , $_order_by ) ;
		
		//for some reason that still eludes me $value comes in as a different field value when there is a where clause
		//completely bewildered and no time to dig any deeper - is it a bug ????
		if ( !empty($row_values) ) {
			//this forces $value to be what its supposed to be
			$value = $row_values->$_field_name ;
		}
		
		return parent::get_relation_input($field_info,$value);
	}

	
	/**
	 * change_list_value
	 *
	 * Patched by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				There was a bug where images with uppercase file extensions
	 *				were'nt being displayed inline.
	 *
	 *				This, along with a change to get_upload_file_input
	 *				and /assets/grocery_crud/js/jquery_plugins/config/jquery.fileupload.config.js
	 *
	 *				fixes the behaviour					 
	 */
	protected function change_list_value($field_info, $value = null)	{
		$real_type = $field_info->crud_type;
		
		switch ($real_type) {
			case 'hidden':
			case 'invisible':
			case 'integer':
				
			break;
			case 'true_false':
				if(isset($this->default_true_false_text[$value]))
					$value = $this->default_true_false_text[$value];
			break;
			case 'string':
				$value = $this->character_limiter($value,$this->character_limiter,"...");
			break;
			case 'text':
				$value = $this->character_limiter(strip_tags($value),$this->character_limiter,"...");
			break;
			case 'date':
				if(!empty($value) && $value != '0000-00-00' && $value != '1970-01-01')
				{
					list($year,$month,$day) = explode("-",$value);
					
					$value = date($this->php_date_format, mktime (0, 0, 0, (int)$month , (int)$day , (int)$year));
				}
				else 
				{
					$value = '';
				}
			break;
			case 'datetime':
				if(!empty($value) && $value != '0000-00-00 00:00:00' && $value != '1970-01-01 00:00:00')
				{
					list($year,$month,$day) = explode("-",$value);
					list($hours,$minutes) = explode(":",substr($value,11));
					
					$value = date($this->php_date_format." - H:i", mktime ((int)$hours , (int)$minutes , 0, (int)$month , (int)$day ,(int)$year));
				}
				else 
				{
					$value = '';
				}
			break;
			case 'enum':
				$value = $this->character_limiter($value,$this->character_limiter,"...");
			break;

			case 'multiselect':
				$value_as_array = array();
				foreach(explode(",",$value) as $row_value)
				{
					$value_as_array[] = array_key_exists($row_value,$field_info->extras) ? $field_info->extras[$row_value] : $row_value;
				}
				$value = implode(",",$value_as_array);
			break;			
			
			case 'relation_n_n':
				$value = $this->character_limiter(str_replace(',',', ',$value),$this->character_limiter,"...");
			break;						
			
			case 'password':
				$value = '******';
			break;
			
			case 'dropdown':
				$value = array_key_exists($value,$field_info->extras) ? $field_info->extras[$value] : $value; 
			break;			
			
			case 'upload_file':
				if(empty($value))
				{
					$value = "";
				}
				else
				{
					//uppercase file extension fix :
					$is_image = !empty($value) &&
					(strripos($value,'.jpg',-4) !== False
							|| strripos($value,'.png',-4) !== False
							|| strripos($value,'.jpeg',-5) !== False
							|| strripos($value,'.gif',-4) !== False
							|| strripos($value,'.tiff',-5) !== False)
							? true : false;
					//: uppercase file extension fix
								
					$file_url = base_url().$field_info->extras->upload_path."/$value";
					
					$file_url_anchor = '<a href="'.$file_url.'"';
					if($is_image)
					{
						$file_url_anchor .= ' class="image-thumbnail"><img src="'.$file_url.'" height="50px">';
					}
					else
					{
						$file_url_anchor .= ' target="_blank">'.$this->character_limiter($value,$this->character_limiter,'...',true);
					}
					$file_url_anchor .= '</a>';
					
					$value = $file_url_anchor;
				}
			break;
			
			default:
				$value = $this->character_limiter($value,$this->character_limiter,"...");
			break;
		}
		
		return $value;
	}
	
	
	/**
	 * get_upload_file_input
	 *
	 * Patched by Ez (ezgoen@gmail.com) - email me for my real identity
	 *
	 * @explanation:
	 *				There was a bug where images with uppercase file extensions
	 *				were'nt being displayed inline.
	 *
	 *				This, along with a change to change_list_value
	 *				and /assets/grocery_crud/js/jquery_plugins/config/jquery.fileupload.config.js
	 *
	 *				fixes the behaviour					 
	 */
	
	protected function get_upload_file_input($field_info, $value){
		$this->set_css($this->default_css_path.'/ui/simple/'.grocery_CRUD::JQUERY_UI_CSS);
		$this->set_css($this->default_css_path.'/jquery_plugins/file_upload/file-uploader.css');
		$this->set_css($this->default_css_path.'/jquery_plugins/file_upload/jquery.fileupload-ui.css');

		$this->set_js($this->default_javascript_path.'/jquery_plugins/ui/'.grocery_CRUD::JQUERY_UI_JS);
		$this->set_js($this->default_javascript_path.'/jquery_plugins/tmpl.min.js');
		$this->set_js($this->default_javascript_path.'/jquery_plugins/load-image.min.js');

		$this->set_js($this->default_javascript_path.'/jquery_plugins/jquery.iframe-transport.js');
		$this->set_js($this->default_javascript_path.'/jquery_plugins/jquery.fileupload.js');
		$this->set_js($this->default_javascript_path.'/jquery_plugins/config/jquery.fileupload.config.js');
		
		//Fancybox
		$this->set_css($this->default_css_path.'/jquery_plugins/fancybox/jquery.fancybox.css');
		
		$this->set_js($this->default_javascript_path.'/jquery_plugins/jquery.fancybox.pack.js');
		$this->set_js($this->default_javascript_path.'/jquery_plugins/jquery.easing-1.3.pack.js');	
		$this->set_js($this->default_javascript_path.'/jquery_plugins/config/jquery.fancybox.config.js');		
		
		$unique = uniqid();
		
		$allowed_files = $this->config->file_upload_allow_file_types;
		$allowed_files_ui = '.'.str_replace('|',',.',$allowed_files);
		$max_file_size_ui = $this->config->file_upload_max_file_size;
		$max_file_size_bytes = $this->_convert_bytes_ui_to_bytes($max_file_size_ui);
		
		$this->_inline_js('
			var upload_info_'.$unique.' = { 
				accepted_file_types: /(\\.|\\/)('.$allowed_files.')$/i, 
				accepted_file_types_ui : "'.$allowed_files_ui.'", 
				max_file_size: '.$max_file_size_bytes.', 
				max_file_size_ui: "'.$max_file_size_ui.'" 
			};
				
			var string_upload_file 	= "'.$this->l('form_upload_a_file').'";
			var string_delete_file 	= "'.$this->l('string_delete_file').'";
			var string_progress 			= "'.$this->l('string_progress').'";
			var error_on_uploading 			= "'.$this->l('error_on_uploading').'";
			var message_prompt_delete_file 	= "'.$this->l('message_prompt_delete_file').'";
			
			var error_max_number_of_files 	= "'.$this->l('error_max_number_of_files').'";
			var error_accept_file_types 	= "'.$this->l('error_accept_file_types').'";
			var error_max_file_size 		= "'.str_replace("{max_file_size}",$max_file_size_ui,$this->l('error_max_file_size')).'";
			var error_min_file_size 		= "'.$this->l('error_min_file_size').'";

			var base_url = "'.base_url().'";
			var upload_a_file_string = "'.$this->l('form_upload_a_file').'";			
		');

		$uploader_display_none 	= empty($value) ? "" : "display:none;";
		$file_display_none  	= empty($value) ?  "display:none;" : "";
		
		//uppercase file extension fix :
		$is_image = !empty($value) &&
					(strripos($value,'.jpg',-4) !== False
							|| strripos($value,'.png',-4) !== False
							|| strripos($value,'.jpeg',-5) !== False
							|| strripos($value,'.gif',-4) !== False
							|| strripos($value,'.tiff',-5) !== False)
							? true : false;
		//: uppercase file extension fix
		$image_class = $is_image ? 'image-thumbnail' : '';
		
		$input = '<span class="fileinput-button qq-upload-button" id="upload-button-'.$unique.'" style="'.$uploader_display_none.'">
			<span>'.$this->l('form_upload_a_file').'</span>
			<input type="file" name="'.$this->_unique_field_name($field_info->name).'" class="gc-file-upload" rel="'.$this->getUploadUrl($field_info->name).'" id="'.$unique.'">
			<input class="hidden-upload-input" type="hidden" name="'.$field_info->name.'" value="'.$value.'" rel="'.$this->_unique_field_name($field_info->name).'" />
		</span>';
		
		$this->set_css($this->default_css_path.'/jquery_plugins/file_upload/fileuploader.css');
		
		$file_url = base_url().$field_info->extras->upload_path.'/'.$value;
		
		$input .= "<div id='uploader_$unique' rel='$unique' class='grocery-crud-uploader' style='$uploader_display_none'></div>";
		$input .= "<div id='success_$unique' class='upload-success-url' style='$file_display_none padding-top:7px;'>";
		$input .= "<a href='".$file_url."' id='file_$unique' class='open-file";
		$input .= $is_image ? " $image_class'><img src='".$file_url."' height='50px'>" : "' target='_blank'>$value";
		$input .= "</a> ";
		$input .= "<a href='javascript:void(0)' id='delete_$unique' class='delete-anchor'>".$this->l('form_upload_delete')."</a> ";
		$input .= "</div><div style='clear:both'></div>";
		$input .= "<div id='loading-$unique' style='display:none'><span id='upload-state-message-$unique'></span> <span class='qq-upload-spinner'></span> <span id='progress-$unique'></span></div>";
		$input .= "<div style='display:none'><a href='".$this->getUploadUrl($field_info->name)."' id='url_$unique'></a></div>";
		$input .= "<div style='display:none'><a href='".$this->getFileDeleteUrl($field_info->name)."' id='delete_url_$unique' rel='$value' ></a></div>";
		
		return $input;
	}
	
/*
 * /assets/grocery_crud/js/jquery_plugins/config/jquery.fileupload.config.js
 *
 * Patched by Ez (ezgoen@gmail.com) - email me for my real identity
 *
 * @explanation:
 *				There was a bug where images with uppercase file extensions
 *				were'nt being displayed inline.
 *
 *				This, along with a change to change_list_value
 *				and  get_upload_file_input
 *
 *				fixes the behaviour					 
 *
 *

function show_upload_button(unique_id, uploader_element)
{
	$('#upload-state-message-'+unique_id).html('');
	$("#loading-"+unique_id).hide();

	$('#upload-button-'+unique_id).slideDown('fast');
	$("input[rel="+uploader_element.attr('name')+"]").val('');
	$('#success_'+unique_id).slideUp('fast');	
}

function load_fancybox(elem)
{
	elem.fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});		
}

$(function(){
	$('.gc-file-upload').each(function(){
		var unique_id 	= $(this).attr('id');
		var uploader_url = $(this).attr('rel');
		var uploader_element = $(this);
		var delete_url 	= $('#delete_url_'+unique_id).attr('href');
		eval("var file_upload_info = upload_info_"+unique_id+"");
		
	    $(this).fileupload({
	        dataType: 'json',
	        url: uploader_url,
	        cache: false,
	        acceptFileTypes:  file_upload_info.accepted_file_types,
			beforeSend: function(){
	    		$('#upload-state-message-'+unique_id).html(string_upload_file);
				$("#loading-"+unique_id).show();
				$("#upload-button-"+unique_id).slideUp("fast");
			},
	        limitMultiFileUploads: 1,
	        maxFileSize: file_upload_info.max_file_size,			
			send: function (e, data) {						
				
				var errors = '';
				
			    if (data.files.length > 1) {
			    	errors += error_max_number_of_files + "\n" ;
			    }
				
	            $.each(data.files,function(index, file){
		            if (!(data.acceptFileTypes.test(file.type) || data.acceptFileTypes.test(file.name))) {
		            	errors += error_accept_file_types + "\n";
		            }
		            if (data.maxFileSize && file.size > data.maxFileSize) {
		            	errors +=  error_max_file_size + "\n";
		            }
		            if (typeof file.size === 'number' && file.size < data.minFileSize) {
		            	errors += error_min_file_size + "\n";
		            }			            	
	            });	
	            
	            if(errors != '')
	            {
	            	alert(errors);
	            	return false;
	            }
				
			    return true;
			},
	        done: function (e, data) {
				if(typeof data.result.success != 'undefined' && data.result.success)
				{
					$("#loading-"+unique_id).hide();
					$("#progress-"+unique_id).html('');
		            $.each(data.result.files, function (index, file) {
		            	$('#upload-state-message-'+unique_id).html('');
		            	$("input[rel="+uploader_element.attr('name')+"]").val(file.name);
		            	var file_name = file.name;
						//uppercase file extension fix :
		            	var is_image = (file_name.toLowerCase().substr(-4) == '.jpg'  
		            						|| file_name.toLowerCase().substr(-4) == '.png' 
		            						|| file_name.toLowerCase().substr(-5) == '.jpeg' 
		            						|| file_name.toLowerCase().substr(-4) == '.gif' 
		            						|| file_name.toLowerCase().substr(-5) == '.tiff')
							? true : false;
						//: uppercase file extension fix
		            	if(is_image)
		            	{
		            		$('#file_'+unique_id).addClass('image-thumbnail');
		            		load_fancybox($('#file_'+unique_id));
		            		$('#file_'+unique_id).html('<img src="'+file.url+'" height="50" />');
		            	}
		            	else
		            	{
		            		$('#file_'+unique_id).removeClass('image-thumbnail');
		            		$('#file_'+unique_id).unbind("click");
		            		$('#file_'+unique_id).html(file_name);
		            	}
		            	
						$('#file_'+unique_id).attr('href',file.url);
						$('#hidden_'+unique_id).val(file_name);

						$('#success_'+unique_id).fadeIn('slow');
						$('#delete_url_'+unique_id).attr('rel',file_name);
						$('#upload-button-'+unique_id).slideUp('fast');
		            });
				}
				else if(typeof data.result.message != 'undefined')
				{
					alert(data.result.message);
					show_upload_button(unique_id, uploader_element);
				}
				else
				{
					alert(error_on_uploading);
					show_upload_button(unique_id, uploader_element);
				}
	        },
	        autoUpload: true,
	        error: function()
	        {
	        	alert(error_on_uploading);
	        	show_upload_button(unique_id, uploader_element);
	        },
	        fail: function(e, data)
	        {
	            // data.errorThrown
	            // data.textStatus;
	            // data.jqXHR;	        	
	        	alert(error_on_uploading);
	        	show_upload_button(unique_id, uploader_element);
	        },	        
	        progress: function (e, data) {
                $("#progress-"+unique_id).html(string_progress + parseInt(data.loaded / data.total * 100, 10) + '%');
            }	        
	    });
		$('#delete_'+unique_id).click(function(){
			if( confirm(message_prompt_delete_file) )
			{
				var file_name = $('#delete_url_'+unique_id).attr('rel');
				$.ajax({
					url: delete_url+"/"+file_name,
					cache: false,
					success:function(){
						show_upload_button(unique_id, uploader_element);
					},
					beforeSend: function(){
						$('#upload-state-message-'+unique_id).html(string_delete_file);
						$('#success_'+unique_id).hide();
						$("#loading-"+unique_id).show();
						$("#upload-button-"+unique_id).slideUp("fast");
					}
				});
			}
			
			return false;
		});		    
	    
	});
});
*/
}
?>

 

 

Cheers

 

 

Ez

 

This serves my purpose.  :)  However there is a side effect... clicking the "view" button in the action column displays the preview but the values with relation (the foreign keys) displays the id rather than the related value.... Is there any workaround for this? Thanks!


DrPaul
  • profile picture
  • Member

Posted 10 August 2014 - 12:36 PM

I've been able to get defaults on add forms with one helper function and a change to one line of GC code - details are at the end of this thread:

 

/topic/1680-how-to-get-default-values-from-the-database/


mmeadway
  • profile picture
  • Member

Posted 07 April 2015 - 23:10 PM

I had the same problem -- needed to set a default field value when adding.  The solution I came up with, while not terribly elegant, appears to work fine.  I wanted to avoid altering the core code, so this appeared to be a quick and dirty way to achieve the desired goal:

 

First, when setting up the CI_Controller object, add the following in the object variables at the top:

 

  public $DefaultFieldValues = array();

 

 

Next, create a callback function for formatting the field you wish to set up:

 

function format_field()

{

  if (isset($this->DefaultFieldValues["fieldname"]) == true)

  {

    $Buffer = "<input name='fieldname' type='text' value='".$this->DefaultFieldValues["fieldname"]."'>";

  }

  else

  {

    $Buffer = "<input name='fieldname' type='text'>";

  }

 

  return($Buffer);

}

 

And last, the following code to the function where you set up your CRUD screen:

 

if (preg_match("/add/",$LocalCrud->getState()) != false)

{

  $this->DefaultFieldValues["fieldname"] = "Default value";

  $LocalCrud->callback_field("fieldname",array($this,"format_field"));
}
 
The idea is that the object DefaultFieldValues array will contain the default value required, and if the CRUD setup is done in "add" mode, then the callback will be invoked to format the field and set the default value.  Hope this helps someone....

titu
  • profile picture
  • Member

Posted 14 May 2015 - 02:59 AM

I've just posted another aproach to default values:

 

/topic/3048-default-values-select-and-text/


Php Power Arts
  • profile picture
  • Member

Posted 14 June 2016 - 08:40 AM

Just very simple solution  :)

 

 

Example: if i need to clear the password field on edit state:

$vars['js_output'] = '<script>
                       $(document).ready(function() {
                             $("#field-password").val("");
                       });
                   </script>';
$this->load->vars($js_output);

And in the view page just print the variable like this 

echo $js_output;

Regards