⚠ 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

Use an array in edit.php



Thanasis Adamou
  • profile picture
  • Member

Posted 27 January 2016 - 10:14 AM

Hello everyone,

 

I have this code:

public function render() {

	// Apply filters if any
	$this->applyFilters();

	// Define Columns and Fields
	$columns = array('code', 'name', 'fname', 'family', 'status' );

	if ( $this->ci->uri->segment(3)=='edit' ){
		$fields = array('name', 'surname', 'fname', 'gender', 'birth', 'address', 'zip', 'city', 'region', 'section', 'district', 'citizenship', 'phone', 'email', 'identity', 'afm', 'amka',
					'family', 'education', 'education_reason', 'marital', 'protected_members', 'occupation', 'unemployment_card', 'social_security', 'income', 'income_from', 'allowances', 'profile_info',
					'vulnerable', 'guardianship', 'outcast', 'learning_difficulties', 'homeless_reverse', 'homeless_reverse_way', 'history', 
					'family_type', 'family_head', 'residence', 'residence_size', 'residence_rooms', 'electricity', 'hot_water', 'internet', 'family_info', 'status');
	}
	else{
		$fields = array('name', 'surname', 'fname', 'gender', 'birth', 'address', 'zip', 'city', 'region', 'section', 'district', 'citizenship', 'phone', 'email', 'identity', 'afm', 'amka',
			'family', 'education', 'education_reason', 'marital', 'protected_members', 'occupation', 'unemployment_card', 'social_security', 'income', 'income_from', 'allowances', 'profile_info');
	}

	$this->ci->uri->segment(3) == "export" ? $this->crud->columns($fields) : $this->crud->columns($columns);

	// Set Labels. We set labels upfront because we may need to overwrite a label using the traditional way
	$this->setLabels($fields);

	// Prepare CRUD
	$this->crud
		->fields($fields)
		->required_fields('gender', 'name', 'birth', 'status')
		->display_as('residence_size', 'Αριθμός Δωματίων:')

	/* Set Relations */
		->set_relation('gender', '_map_genders', 'title')
		->set_relation('citizenship', '_map_citizenship', 'title')
		->set_relation('family','beneficiaries','{name} {surname} ({fname})', NULL, 'id ASC')
		->set_relation('education', '_map_education', 'title', NULL, 'id ASC')
		->set_relation('marital', '_map_marital', 'title')
		->set_relation('occupation', '_map_occupation', 'title')
		->set_relation('unemployment_card', '_map_unemployment_card', 'title', NULL, 'id ASC')
		->set_relation('social_security', '_map_social_security', 'title')
		->set_relation('income_from', '_map_income_from', 'title')
		->set_relation_n_n('allowances', 'allowances_link', '_map_allowances', 'beneficiary_id', 'map_id', 'title', NULL)
		->set_relation_n_n('vulnerable', 'vulnerable_link', '_map_vulnerable', 'beneficiary_id', 'map_id', 'title', NULL)
		->set_relation('guardianship','beneficiaries','{name} {surname} ({fname})', NULL, 'id ASC')
		->set_relation('outcast', '_map_outcast', 'title')
		->set_relation('learning_difficulties', '_map_learning_difficulties', 'title')
		->set_relation_n_n('indirect_income', 'indirect_income_link', '_map_indirect_income', 'beneficiary_id', 'map_id', 'title', NULL)
		->set_relation_n_n('homeless_reason', 'homeless_reason_link', '_map_homeless_reason', 'beneficiary_id', 'map_id', 'title', NULL)
		->set_relation_n_n('homeless_reverse_way', 'homeless_reverse_way_link', '_map_homeless_reverse_way', 'beneficiary_id', 'map_id', 'title', NULL)
		->set_relation('status', 'statuses', '<span class="label {label}">{title}</span>', 'statuses.mode = "generic"');


	if ( $this->ci->uri->segment(3)=='edit' ){
		$this->crud
			->callback_update(array($this,'callback_update'))
			->callback_field('family_type', array($this,'callback_field_family_type'))
			->callback_field('family_head', array($this,'callback_field_family_head'))
			->callback_field('residence', array($this,'callback_field_residence'))
			->callback_field('residence_size', array($this,'callback_field_residence_size'))
			->callback_field('residence_rooms', array($this,'callback_field_residence_rooms'))
			->callback_field('electricity', array($this,'callback_field_electricity'))
			->callback_field('hot_water', array($this,'callback_field_hot_water'))
			->callback_field('internet', array($this,'callback_field_internet'))
			->callback_field('family_info', array($this,'callback_field_family_info'))
			->callback_field('status', array($this,'callback_field_status'));

		$tabs = '<ul class="nav nav-tabs" style="margin-bottom:-1px;">
					<li class="active"><a data-toggle="tab" href="#beneficiary">Στοιχεία Ωφελούμενου</a></li>
					<li><a data-toggle="tab" href="#social_worker">Στοιχεία Κοινωνικού Λειτουργού</a></li>
					<li><a data-toggle="tab" href="#family">Στοιχεία Οικογένειας</a></li>
				</ul>';
	}
	return $this->crud->render($tabs);
}

I'm struggling to transfer an array or object from this function to the assets/grocery-grud/themes/bootstrap/edit.php file.

 

Do you have any idea how I can do that?

 

 

I tried to create a new protected variable in grocery_crud class in libraries/grocery_crud.php but not successfully. Do you know if this is a correct way?

 

Thanks for the help in advance

 

 

 

 


moe2801
  • profile picture
  • Member

Posted 03 February 2016 - 16:07 PM

Hi!

 

To get an array or object to the edit.php you need to complete multiple steps:

 

1) You need to modify the Grocery_CRUD.php in the libraries folder. But you don't change the code directly, instead you create a new library and extend the old one. Like this:

<?php
require_once 'Grocery_CRUD.php';
class Grocery_CRUD_Edit_Field extends grocery_CRUD {

}

2) Then use the new library in your render function like this:

 $this->load->library('Grocery_CRUD_Edit_Field');
 $crud = new Grocery_CRUD_Views();

3) Now you edit the Grocery_CRUD_Edit_Field file. Create a new variable on the top like this:

protected $edit_array = NULL;

4) Create a function to set the array:

public function setThemeView($edit_array) {
	$this->edit_array = $edit_array;
}

5) Then copy the old showEditForm function from Grocery_CRUD.php to the new library, and add this line somewhere:

$data->edit_array = $this->edit_array;

6) Now you can use the array in the edit.php like this: $edit_array

 

Have fun!