I have an array like this:
return array(
'User Management -> Role Management' => array(
array(
'permission' => 'role-management.view',
'label' => 'View',
),
array(
'permission' => 'role-management.create',
'label' => 'Create',
),
array(
'permission' => 'role-management.edit',
'label' => 'Edit',
),
array(
'permission' => 'role-management.delete',
'label' => 'Delete',
),
),
'User Management -> User Management' => array(
array(
'permission' => 'user-management.view',
'label' => 'View',
),
array(
'permission' => 'user-management.create',
'label' => 'Create',
),
array(
'permission' => 'user-management.edit',
'label' => 'Edit',
),
array(
'permission' => 'user-management.delete',
'label' => 'Delete',
),
),
);
I have accessed this array successfully like this:
$permissions = Config::get('permissions');
I want to add new index below label. I have tried this but could not add.
`foreach ($permissions as $permission) {
foreach ($permission as $eachPermission) {
$encodedPermission = base64_encode($eachPermission['permission']);
// $eachPermission['encodedPermission'] = $encodedPermission;
array_push($eachPermission, "encodedPermission", $encodedPermission);
}
}
var_dump($permissions); `
I have tried these but could not set the new index. My expected result was this:
'permission' => 'role-management.view',
'label' => 'View',
'encodedPermission' => 'someencodedstring'
Am I doing it wrong or missing something.
Your array is multi-level, so you need to do double-foreach to reach the level required for your changes.
And for your changes being saved in the original array, you need to use ampersand (&) before your iterators to keep the reference to the original array. Without it, you would insert the new data only to the copy of the array that was created for your foreach loop.
This works:
foreach ($permissions as &$eachPermission) {
foreach ($eachPermission as &$singlePermission) {
$encodedPermission = base64_encode($singlePermission['permission']);
array_push($singlePermission, "encodedPermission", $encodedPermission);
}
}
var_dump($permissions);
i would do it like this just because it is easier to understand ...
foreach($permissions as $name1=>$ar1){
foreach($ar1 as $name2=>$ar2){
$permissions[$name1][$name2]['encodedPermission'] = base64_encode($ar2['permission']);
}
}
Related
I'm using codeigniter 3, and I'm trying to use the form_validation library.
Basically, if validation fails, I'm catching the input data and then sending it back to the form.
So I'm sticking all form data in an array, like so:
// add input data to array
$org_data = array(
'org_id' => $this->input->post('org_id'),
'p_org_id' => $this->input->post('p_org_id'),
'account_ref' => $this->input->post('account_ref'),
'org_name' => $this->input->post('org_name'),
'address1' => $this->input->post('address1'),
'address2' => $this->input->post('address2'),
'address3' => $this->input->post('address3'),
'town' => $this->input->post('town'),
'county' => $this->input->post('county'),
'pcode' => $this->input->post('pcode'),
'phone' => $this->input->post('phone'),
'support_email' => $this->input->post('support_email'),
'notify_return' => $this->input->post('notify_return'),
'notify_email' => $this->input->post('notify_email'),
'email_interval' => $this->input->post('email_interval'),
'renewal_date' => $this->input->post('renewal_date'),
'login_reminder' => $this->input->post('login_reminder'),
'default_fireaware' => $this->input->post('default_fireaware'),
'open_training_url' => $this->input->post('open_training_url'),
);
All fine!
Now, to send the data back to the form, I am using the below.
$this->data['org_id'] = array(
'name' => 'org_id',
'id' => 'org_id',
'type' => 'text',
'value' => $this->form_validation->set_value('org_id'),
);
BUT
I don't want to create one of these for every input, so ideally I'd like to use a loop to create these. But I cant get it to work, I am getting undefined variable errors.
This is the loop in progress:
foreach($org_data as $key => $value){
$this->data['$key'] = array(
'name' => '$key',
'id' => '$key',
'type' => 'text',
'value' => $this->form_validation->set_value('$value'),
);
}
Can I use a loop to do this?
What are your thoughts?
use validation like this
$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'Password Confirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
);
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == FALSE) {
$data['errors'] = validation_errors();
$this->load->view('yourview', $data);
} else {
$userData = $this->input->post();
$this->load->view('yourview', $data);
}
Basically, if validation fails, I'm catching the input data and then
sending it back to the form.
yeah i think this is the part to clarify - you don't need to do that at all -- thats the advantage of using set_value('fieldName'), it automatically echoes out the value. Same - on the form - with form_error( 'fieldName' ) it will display the field specific error message.
When i try to add new documents to an index type , i loose existing documents which are overwritten by the new added ones . The problem can be related to the id of each added document ??
Here is the code :
$elasticaClient = new \Elastica\Client(array(
'host' => $this->container->getParameter('elastic_host'),
'port' => $this->container->getParameter('elastic_port')
));
$elasticaIndex = $elasticaClient->getIndex('app');
$elasticaIndex->create(
array(
'number_of_shards' => 4,
'number_of_replicas' => 1,
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('lowercase', 'mySnowball')
),
'searchAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('standard', 'lowercase', 'mySnowball')
)
),
'filter' => array(
'mySnowball' => array(
'type' => 'snowball',
'language' => 'German'
)
)
)
),
true
);
$elasticaType = $elasticaIndex->getType('type');
$mapping = new \Elastica\Type\Mapping();
$mapping->setType($elasticaType);
$mapping->setParam('index_analyzer', 'indexAnalyzer');
$mapping->setParam('search_analyzer', 'searchAnalyzer');
$mapping->setProperties(array(
'id' => array('type' => 'string'),
'title' => array('type' => 'string'),
'duration' => array('type' => 'string'),
'start' => array('type' => 'string'),
'end' => array('type' => 'string'),
));
// Send mapping to type
$mapping->send();
$documents = array();
foreach($medias as $media) {
$id = uniqid() ;
$documents[] = new \Elastica\Document(
$id,
array(
'id' => $id,
'title' => $media['title'],
'duration' => $media['duration'],
'start' => $media['start'],
'end' => $media['end'],
)
);
}
$elasticaType->addDocuments($documents);
$elasticaType->getIndex()->refresh();
Please i need your help . Thank you
PHP does not recommend using uniqid for this use case. Since you are wanting a random, safe id, let Elasticsearch do it for you. The Elastica Document construct method notes that the id field is optional. So don't pass it and let Elasticsearch issue the id.
Several things
$elasticaIndex->create (....) you only have to enter it once. Index is unique after creating the index that you can comment or generate a different index and other things. I leave an example that works.
class PersistencyElastic
{
private $conection;
public function __construct()
{
$this->conection = new \Elastica\Client(['host' => '127.0.0.1', 'port' => 9200]);
}
public function save($ msg)
{
// $ msg is an array with whatever you want inside
$index = $this->conection->getIndex('googlephotos');
// This is the index I created, it's called googlephotos
// $index->create(array (), true);
$type = $index->getType('googlephotos');
$type->addDocument(new Document (uniqid ('id _', false), $msg, $type, $index));
$index->refresh();
}
}
I am having trouble submitting/saving data to the CakePHP Model. I constructed the form as usual as I always do, but this time I am getting notices and warning an also data is not getting saved. Here is the form I have constructed and method in Controller:
educationaldetails.ctp
<?php
if (empty($Education)):
echo $this->Form->create('Candidate', array('class' => 'dynamic_field_form'));
echo $this->Form->input('CandidatesEducation.0.candidate_id', array(
'type' => 'hidden',
'value' => $userId
));
echo $this->Form->input('CandidatesEducation.0.grade_level', array(
'options' => array(
'Basic' => 'Basic',
'Masters' => 'Masters',
'Doctorate' => 'Doctorate',
'Certificate' => 'Certificate'
)
));
echo $this->Form->input('CandidatesEducation.0.course');
echo $this->Form->input('CandidatesEducation.0.specialization');
echo $this->Form->input('CandidatesEducation.0.university');
echo $this->Form->input('CandidatesEducation.0.year_started', array(
'type' => 'year'
));
echo $this->Form->input('CandidatesEducation.0.year_completed', array(
'type' => 'year'
));
echo $this->Form->input('CandidatesEducation.0.type', array(
'options' => array(
'Full' => 'Full',
'Part-Time' => 'Part-Time',
'Correspondence' => 'Correspondence'
)));
echo $this->Form->input('CandidatesEducation.0.created_on', array(
'type' => 'hidden',
'value' => date('Y-m-d H:i:s')
));
echo $this->Form->input('CandidatesEducation.0.created_ip', array(
'type' => 'hidden',
'value' => $clientIp
));
echo $this->Form->button('Submit', array('type' => 'submit', 'class' => 'submit_button'));
echo $this->Form->end();
endif;
CandidatesController.php
public function educationaldetails() {
$this->layout = 'front_common';
$this->loadModel('CandidatesEducation');
$Education = $this->CandidatesEducation->find('first', array(
'conditions' => array(
'candidate_id = ' => $this->Auth->user('id')
)
));
$this->set('Education', $Education);
// Checking if in case the Candidates Education is available then polpulate the form
if (!empty($Education)):
// If Form is empty then populating the form with respective data.
if (empty($this->request->data)):
$this->request->data = $Education;
endif;
endif;
if ($this->request->is(array('post', 'put')) && !empty($this->request->data)):
$this->Candidate->id = $this->Auth->user('id');
if ($this->Candidate->saveAssociated($this->request->data)):
$this->Session->setFlash('You educational details has been successfully updated', array(
'class' => 'success'
));
return $this->redirect(array(
'controller' => 'candidates',
'action' => 'jbseeker_myprofile',
$this->Auth->user('id')
));
else:
$this->Session->setFlash('You personal details has not been '
. 'updated successfully, please try again later!!', array(
'class' => 'failure'
));
endif;
endif;
}
Here is the screenshot of errors I am getting, Not able to figure out what is happening while other forms are working correctly. looks like something something wrong with view?
This error is coming out because you are not passing the proper arguments to the session->setFlash() method, you are doing like this:
$this->Session->setFlash('You personal details has not been updated successfully, please try again later!!', array(
'class' => 'failure'
));
In docs it is mentioned like:
$this->Session->setFlash('You personal details has not been updated successfully, please try again later!!',
'default', array(
'class' => 'failure'
));
Hi so if i understand :
array(
'CandidatesEducation' => array(
(int) 0 => array(
'candidate_id' => '5',
'grade_level' => 'Basic',
'course' => 'Masters of Computer Application',
'specialization' => '',
'university' => 'Mumbai University',
'year_started' => array(
'year' => '2011'
),
'year_completed' => array(
'year' => '2014'
),
'type' => 'Full',
'created_on' => '2014-11-27 15:44:36',
'created_ip' => '127.0.0.1'
)
)
),
Is your data , and you just need to save this in your candidates_educations table , so in this case i would do :
$this->Candidate->CandidatesEducation->save($this->request->data);
and your $data had to look :
array(
'CandidatesEducation' => array(
'candidate_id' => '5',
'grade_level' => 'Basic',
'course' => 'Masters of Computer Application',
'specialization' => '',
'university' => 'Mumbai University',
'year_started' => array(
'year' => '2011'
),
'year_completed' => array(
'year' => '2014'
),
'type' => 'Full',
'created_on' => '2014-11-27 15:44:36',
'created_ip' => '127.0.0.1'
)
);
the saveAssociated is used when you create your Model AND Model associated , not only your Associated model.
And : i m not sure for year_started and year completed rm of data, it depends of your table schema ; what's the type of year_completed and year_started ?
I have a problem with TbEditedableColumn in YiiBooster 4.0.1
View:
$this->widget(
'application.extensions.booster.widgets.TbGridView',
array(
'type' => 'striped bordered',
'dataProvider' => new CActiveDataProvider('Stats'),
'columns' => array(
'pid',
array(
'class' => 'application.extensions.booster.widgets.TbEditableColumn',
'name' => 'login',
'sortable' => false,
'editable' => array(
//'model' => $model,
//'attribute' => 'login',
'url' => $this->createUrl('stats/editableSaver'),
'placement' => 'right',
'inputclass' => 'span3'
)
)
),
)
);
Controller:
public function actionEditableSaver()
{
Yii::import('application.extensions.booster.components.TbEditableSaver');
$es = new TbEditableSaver('Stats');
$es->update();
}
When I try to save the edited fields, I got this exception: Property "attribute" should be defined.
$es->attributes is empty.
How to fix that? Thanks.
From the source code, TbEditableSaver::update() obtains the attribute from a post or get parameter name:
$this->attribute = yii::app()->request->getParam('name');
$this->value = yii::app()->request->getParam('value');
//checking params
if (empty($this->attribute)) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Property "attribute" should be defined.'));
}
In order for this parameter to be sent in the update request it needs to be defined in the editable array. To fix this:
'class' => 'application.extensions.booster.widgets.TbEditableColumn',
'name' => 'login',
'sortable' => false,
'editable' => array(
'name' => 'login',
'url' => $this->createUrl('stats/editableSaver'),
'placement' => 'right',
'inputclass' => 'span3'
)
I am trying to validate a multiply select using input filter, but every time I see a error. The error is "notInArray":"The input was not found in the haystack".(I use ajax but it doesn`t metter).
I will show part of my code to be more clear.
in Controller:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
//.... more code...
When I put print_r($post['positions']); I see:
array(0 => 118, 1 => 119)
in ..../form/UserForm.php I create the multiply element
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
and in the validation file the code is:
$inputFilter->add($factory->createInput(array(
'name' => 'positions',
'required' => false,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(118,119),
'messages' => array(
'notInArray' => 'Please select your position !'
),
),
),
),
What can be the reason every time to see this error, and how I can fix it?
By default selects have attached InArray validator in Zend Framework 2.
If you are adding new one - you will have two.
You should disable default one as follow:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
'disable_inarray_validator' => true, // <-- disable
),
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
And you should get rid of the additional error message.
Please let us know if that would helped you.