Cake PHP Update single fields in View - Undefined Variable - variables

i search a long time, but i dont find a solution for my problem.
The case is, that i have a few buttons integrated in my view and clicking on them affected updating single input fields. The problem is that i have warnings, that variables are undefined in view. I understand why and how i suppress them, but i`m not sure, if this is a good solution. Is there a better way to solve this? What is best practice?
Here is my code from the view file:
<?php
echo $this->Form->create('Excel', array('type' => 'file'));
echo $this->Form->file('File');
echo $this->Form->submit('Upload xls File');
echo $this->Form->end();
echo $this->Form->create('Config');
//echo $this->Form->input('Name');
echo $this->Form->input('vlanNumber');
echo $this->Form->input('description', array('value' => $description));
echo $this->Form->input('preSharedKey', array('value' => $preSharedKey));
echo $this->Form->button('generate', array('name'=>'generateButton'));
echo $this->Form->input('customerPeerIp', array('default' => 'id_of_default_val','value' => $cusPeerIp));
The generate button affect a new preSharedKey. And the upload of the csv affected an update of the other fields.
The relevant code of the controller is this:
public function inputData() {
if ($this->request->is('post')) {
$post_data = $this->request->data;
if (isset($this->request->data['show'])) { //Submit Button was clicked
$this->Session->write('Configuration',$post_data); //Store the input fields in the session
return $this->redirect(array('action' => 'showPreview'));
} else if (isset($this->request->data['cancel'])) { // Cancel button was clicked: Go back to index site
return $this->redirect(array('action' => 'index'));
} else if (isset($this->request->data['generateButton'])) {
return $this->set('preSharedKey', $this->getRandomString(20)); //Set a Pre Shared Key with 30 signs
}
if (!empty($this->data) && is_uploaded_file($this->data['Excel']['File']['tmp_name'])) {
$this->importData($this->data['Excel']['File']['tmp_name']);
$excel=new Excel();
$values=$excel->getParams($this->data['Excel']['File']['tmp_name']);
$this->set('description',$values['description']);
$this->set('cusPeerIp',$values['cust_peer']);
return;
//this calls the Excel Class function
}
//print_r($post_data);
//echo $post_data['Config']['Name'];
//echo $this->request['Config']['task_1'];
}
$this->set('description','');
$this->set('cusPeerIp','');
$this->set('preSharedKey', '');
}
Can you please help me?

Related

Could not find RETS data in TREB VOW Data Access

This is connection file
date_default_timezone_set("Canada/Eastern");
require_once("vendor/autoload.php");
$config = new \PHRETS\Configuration;
$config->setLoginUrl('http://retsau.torontomls.net:6103/rets-treb3pv/server/login')
->setUsername('XXXX1')
->setPassword('76XXXXX')
->setRetsVersion('1.7');
$rets = new \PHRETS\Session($config);
$connect = $rets->Login();
echo '<pre>';
var_dump($connect);
echo '</pre>';
if($connect)
{ echo "connetced";
else{
echo 'not connected';
}
After connection could not find any data
$results = $rets->Search('Property', 'A', '*', ['Limit' => 3, 'Select' => 'LIST_1,LIST_105,LIST_15,LIST_22,LIST_87,LIST_133,LIST_134']);
foreach ($results as $r) {
echo '<pre>';
var_dump($results);
echo '</pre>';
}
I need to display this data after connection
First you should check TREB metadata, find correct field names and use them in the search. Moreover, you should send a query as 3rd argument of Search function.

How can I change the checkboxlist to look similar to on/off switch?

Is there any way to change the checkboxlist widget to look similar to the on/off switch presented in Kartik's Switch Input Widget, but in a group?
I'm using this code:
echo $form->field($model, 'blocked_list')->checkboxList($array_list);
Which is very simple to use, but produces "simple" list...
I've tried with Karitk's like this:
foreach ($array_list as $category_id=>$category_name) {
echo '<label class="control-label">' . $category_name . '</label>';
echo SwitchInput::widget([
'name'=>'blocked_list',
'value'=>in_array($category_id, $model->blocked_list),
'pluginOptions' => [
'size' => 'mini',
],
]);
But it does not link with the model, and the Form looses it format
Any ideas, please?
I've got it!
foreach ($category_array as $category_id=>$category_name) {
if ( isset($model->blocked_list[$category_id]) )
$model->blocked_list[$category_id] = true;
else
$model->blocked_list[$category_id] = false;
echo $form->field($model, 'blocked_list[' . $category_id . ']')
->label($category_name)
->widget(SwitchInput::classname(), []);
}
I just added a validate "if" to ask if the variable to display exists, and then set the correct value for the model
Hope it helps someone

Undefined Variable $action

I have the following two simple scripts. $action being detected as "Not Set". If isset statement is not used, warning "Undefined variable $action" is popped. I don't know what is the problem, please help.
prog01.html :
Press to add record
prog02.php :
<?php
if (isset($action)){
if ($action == 'add') echo "action is to add <br>";
}
else
echo "\$action is not set <br>";
?>
You want to specify that action value is submitted using GET or POST method like this
<?php
if (isset($_POST['action'])){
$action = $_POST['action'];
}
else if (isset($_GET['action'])){
$action = $_GET['action'];
}
if ($action == 'add')
{
echo "action is to add <br>";
}
else
{
echo "\$action is not set <br>";
}
?>

YII Call to a member function getErrors() on a non-objec

Following this tutorial; When I try to get the data to display in the form and update
For my error was:
$n = $this->loadModel($id);
I want to make two models with one form, this is my code for update:
Controller:
public function actionUpdate($id)
{
$n = new Noticias;
$m = new Multimedia;
$this->performAjaxValidation(array($n,$m));
$n=$this->loadModel($id);
if(isset($_POST['Noticias'],$_POST['Multimedia']))
{
$n->attributes=$_POST['Noticias'];
$m->attributes=$_POST['Multimedia'];
$m->ID_NOT=$n->ID;
$m->setIsNewRecord(false);
if($n->save())
$this->redirect(array('admin','id'=>$n->ID));
}
$this->render('update',array('n'=>$n,'m'=>$m));
}
update.php
<?php echo $this->renderPartial('_form', array('n'=>$n, 'm'=>$m)); ?>
some view
//get Multimedia FK
<?php if ($n->isNewRecord==false) {
$m=Multimedia::model()->findByPk($n->ID);
} ?>
//Validation
<?php echo $form->errorSummary(array($n,$m)); ?>
//Field FOTO between other
<div class="row">
<?php echo $form->labelEx($m,'FOTO_URL'); ?>
<?php echo $form->textField($m,'FOTO_URL',array('size'=>25,'maxlength'=>100)); ?>
<?php echo $form->error($m,'FOTO'); ?>
</div>
First - do not load models in view
<?php if ($n->isNewRecord==false) {
$m=Multimedia::model()->findByPk($n->ID);
} ?>
This part should be in controller action.
Make sure $m and $n are really models. If findByPk statement above fails, $m will be null, so you will get error from errorSummary which calls $m->getErrors() and $n->getErrors(). In short - make sure $n and $m are properly initialized - both must be instances of model, either empty, or filled from db, but in your case one is null. Most probably $m
this error occur when Noticias (ie $n is newrecord) or $n->ID is not found in Multimedia
you can solve this if you need to work even if its a new record you can use.
if ($n->isNewRecord==false) {
$m=Multimedia::model()->findByPk($n->ID);
}
else
$m=new Multimedia;
You can try to load both models in Controller (ex. in actionUpdate) :
public function actionUpdate($id)
{
$n = new Noticias;
$m = new Multimedia;
$this->performAjaxValidation(array($n,$m));
$n=$this->loadModelNoticias($id);
$m=$this->loadModelMultimedia($n->ID); //just put it here..
if(isset($_POST['Noticias'],$_POST['Multimedia']))
{
$n->attributes=$_POST['Noticias'];
$m->attributes=$_POST['Multimedia'];
$m->ID_NOT=$n->ID;
$m->setIsNewRecord(false);
if($n->save())
$this->redirect(array('admin','id'=>$n->ID));
}
$this->render('update',array('n'=>$n,'m'=>$m));
}
Please be noticed that you have to define both loadModel functions in the same controller too.
function loadModelNoticias($id){
$model = Noticias::model()->findByPk($id);
return $model;
}
function loadModelMultimedia($id){
$model = Multimedia::model()->findByPk($id);
return $model;
}

Best way to display current logged-on user in default.ctp?

I am in the process of customizing the default.ctp file and I am trying to display the currently logged on user's name on the top of the page.
In app_controller.php, I have the following:
function beforeFilter()
{
$user = $this->Auth->user();
if($user != null)
{
$this->Session->write('user_name',$user['User']['username']);
}
}
And in default.ctp, I have:
$user = $this->Session->read('Auth.User');
if(!empty($user))
{
echo 'Hello, ' . $user['user_name'];
}
However, it seems like the value $user_name is not getting set anywhere.
What am I doing wrong? Is there a better way to accomplish this?
Update: I've modified it as described in the answer, but it still doesn't work. I get an error:
Undefined index: user_name [APP/views/layouts/default.ctp, line 21]
you can also use the SessionHelper directly in the view / layout
$user = $this->Session->read('Auth.User');
if(!empty($user)) {
echo 'Hi ', $user['user_name'];
}
Cakephp 2.x:
<?php if (AuthComponent::user('id')): ?>
<p class="navbar-text pull-right">
Logged in as <?= AuthComponent::user('name') ?>
</p>
<?php endif; ?>
$user = $this->Session->read('Auth.User');
if(count($user))
echo $user['name'];