My problem is when $tipeKampanye is p3, it's work well, but when $tipeKampanye is visitor $this->Click_model->insertClickDetail insert multiple row in my table and i don't know from where images value from like below:
here my controller code (and it is not in loop) :
if($this->session->userdata('ipUser')!==$_SERVER['REMOTE_ADDR'])
{
echo '1';
$this->session->set_userdata('ipUser',$_SERVER['REMOTE_ADDR']);
$this->session->set_userdata('idClick',$this->Click_model->insertClick());
$data=array(
'id_click'=>$this->session->userdata('idClick'),
'id_kampanye'=>$idKampanye,
'code'=>$tipeKampanye
);
$this->Click_model->insertClickDetail( $data);
}else{
echo '2' ;
$data=array(
'id_click'=>$this->session->userdata('idClick'),
'id_kampanye'=>$idKampanye,
'code'=>$tipeKampanye
);
$this->Click_model->insertClickDetail($data);
}
and this my Click_model.php
public function insertClick(){
$this->db->insert('click','id');
return $this->db->insert_id();
}
public function insertClickDetail($data=array()){
return $this->db->insert('click_detail',$data);
}
just adding some code to remove unwanted value from array
public function insertClickDetail($data=array()){
if(in_array('images',$data,true))
{
$data = array_splice($array, 0, 0);
}
return $this->db->insert('click_detail',$data);
}
Related
I want to set up a Controller following this guide:
https://webkul.com/blog/create-modules-admin-controllers-without-creating-tab-prestashop/
So in my custom module I do this:
....
public function install() {
return (parent::install()
&& $this->registerHook('header')
&& $this->registerHook('footer')
&& $this->installTab()
);
}
public function installTab() {
$tab = new Tab();
$tab->active = 1;
$tab->class_name = 'abandonedCartsAdminModuleController';
$tab->name = "test";
//If you don't want to create a tab for your admin controller then Pass id_parent value as -1.
$tab->id_parent = -1;
$tab->module = $this->name;
return $tab->add();
}
This is the Controller: abandonedCartsAdminModuleController.php
<?php
class abandonedCartsAdminModuleController extends AdminModuleController {
public function __construct() {
parent::__construct();
$this->context = Context::getContext();
}
public function init() {
$this->retrieve();
}
public function retrieve() {
...
}
}
What happens when I try to install my module is I have the PrestaShopException: "Property Tab->name is empty
at line 887 in file classes/ObjectModel.php"
$tab->name must be an array, one name by language.
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = 'test';
}
hi i want with sonata to have an item using 'name' label.
but not always the same.
i put this in admin class
public function toString($object) {
if (!is_object($object)) {
return '';
}
if (method_exists($object, '__toString') && null !== $object->__toString()) {
return (string) $object;
}
$cname = explode('\\', get_class($object));
return end($cname);
}
but it give always the same name. i want to have the label 'name' of each entity
If you want to get name of each entity, use:
public function __toString() {
return self::class;
}
You need to override the __toString() magic method in your entity class
public function __toString(){
return $this->name;
}
I was started my learing in OOP and I make a class but I have a problem now. This is my little code:
class User {
public $name;
public $age;
public $city;
public function setDate($setName, $setAge, $setCity)
{
$this->name = $setName;
$this->age = $setAge;
$this->city = $setCity;
}
public function pullDate()
{
return $this->name;
return $this->age;
return $this->city;
}
}
$newUser = new User;
$newUser->setDate('Adrian', '23', 'Poland');
echo $newUser->pullDate();
And when I get variable by echo $newUser->pullDate(); in response recerive only 'Adrian' ... How I can fix it?
Thanks for help!
A function can only return a single value. Placing multiple return statements in a row does not make any sense, the function will always return after the first such statement. Instead you have to combine the values, for example by creating an array out of them.
Take a look at this modified version, assuming that you are using php:
<?php
class User {
public $name;
public $age;
public $city;
public function setDate($setName, $setAge, $setCity)
{
$this->name = $setName;
$this->age = $setAge;
$this->city = $setCity;
}
public function pullDate() {
return [
'name' => $this->name,
'age' => $this->age,
'city' => $this->city
];
}
}
$newUser = new User;
$newUser->setDate('Adrian', '23', 'Poland');
$userValues = $newUser->pullDate();
echo sprintf(
"Name: %s, Age: %s, City: %s\n",
$userValues['name'],
$userValues['age'],
$userValues['city']
);
An alternative would be to implement separate "getters" for each property:
class User {
// ...
public function getName() {
return $this->name;
}
public function getAge() {
return $this->age;
}
public function getCity() {
return $this->city;
}
// ...
}
And then get the attributes one by one, just as desired, for example by doing:
echo sprintf(
"Name: %s, Age: %s, City: %s\n",
$newUser->getName(),
$newUser->getAge(),
$newUser->getCity()
);
I'm trying to implement a plugin that handles responses to the user on success or failure in a persistance transaction. When the response is false I use a _forward to the action that performed the form's submit and get my placeholder message shown but when the response is true I use a _redirect to the browse with the new record shown.
My problem is that when I use _redirect the browser doesn't show the placeholder message. I'll show the code here:
/**
* Plugin
*/
class Application_Plugin_PostMessage extends Zend_Controller_Plugin_Abstract
{
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
$message = $request->getParam('message');
$error = $request->getParam('error');
if (null !== $message || null !== $error) {
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$placeHolder = $view->placeholder('message');
$placeHolder->setPostfix('</div>');
if (null !== $error) {
$placeHolder->setPrefix('<div class="errorMessage">')
->append($error);
}
elseif (null !== $message) {
$placeHolder->setPrefix('<div class="infoMessage">')
->append($message);
}
}
}
}
/**
* Controller
*/
class My_FooController extends Zend_Controller_Action
{
public function init()
{
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Application_Plugin_PostMessage());
}
...
public function browseAction()
{
...
// No message is shown here on redirect
...
}
public function newAction()
{
...
// This code shows the placeholder on _forward call
...
}
public function insertAction()
{
if(true) {
return $this->_redirect('/my/foo/browse?message='
. urlencode("success message"));
}
else {
return $this->_forward('new', null, null, array(
'error' => 'error messsage'
));
}
}
}
I can't use _forward on success because I don't want the use of [F5] key repeats the insert action
Thanks in advance
This is what Flash Messenger is for:
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelper.flashmessenger.basicusage
It stores messages in your session removing the need for passing messages as you are.
I have a registration form with different input fields one of them being a multi checkbox so that the user can decide what countries he wants to receive information from. This last one is created like this:
$pais = $this->createElement('multiCheckbox', 'pais');
$pais->setLabel('Pais\es: ');
$pais->addMultioption('1', 'Argentina');
$pais->addMultioption('2', 'Espa?a');
$pais->addMultioption('3', 'Brasil');
$pais->addMultioption('4', 'USA');
$pais->addMultioption('5', 'Italia');
$this->addElement($pais);
In my UserController I have the following action to update the table 'users':
public function createAction()
{
$this->view->pageTitle = 'Create User';
require_once APPLICATION_PATH . '/models/Users.php';
$userForm = new Form_User();
if ($this->_request->isPost()) {
if ($userForm->isValid($_POST)) {
$userModel = new Model_User();
$userMode->createUser(
$userForm->getValue('email'),
$userForm->getValue('password'),
$userForm->getValue('url'),
$userForm->getValue('responsable'),
$userForm->getValue('role')
);
return $this->_forward('list');
}
}
$userForm->setAction('/user/create');
$this->view->form = $userForm;
}
which of course, right now is not contemplating the multicheckbox populatedn$pais variable, nor here nor in the model:
class Model_User extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
public function createUser($email, $password, $url, $responsable, $role)
{
// create a new row
$rowUser = $this->createRow();
if($rowUser) {
// update the row values
$rowUser->email = $email;
$rowUser->password = md5($password);
$rowUser->url = $url;
$rowUser->responsable = $responsable;
$rowUser->role = $role;
$rowUser->save();
//return the new user
return $rowUser;
} else {
throw new Zend_Exception("El usuario no se ha podido crear!");
}
}
}
I have also a 'pais' table, which contains the 5 different countries, and I'm working on a separate model for 'users_has_pais' which is the table I created in the workbench for this purpose...but I'm not getting any results with what I'm doing right now. Can someone point me in the right path to get to update 'users_has_pais' at the same time that I update the 'users' table?
Thanks a lot in advance to anyone with good advice on this.
EDIT: this is the db model in case anyone needs it to figure out what I'm saying
EDIT2:
public function createAction()
{
$this->view->pageTitle = 'Create User';
require_once APPLICATION_PATH . '/models/Users.php';
$userForm = new Form_User();
if ($this->_request->isPost()) {
if ($userForm->isValid($_POST)) {
$userModel = new Model_User();
$user = $userModel->createUser(
$userForm->getValue('email'),
$userForm->getValue('password'),
$userForm->getValue('url'),
$userForm->getValue('responsable'),
$userForm->getValue('role')
);
$paises = $this->getRequest()->getParam('pais');
$userId = intval($user['id']);
require_once APPLICATION_PATH . '/models/UserHasPais.php';
$paisesModel = new Model_UsersHasPais();
$paisesModel->updateUserPais($userId, $paises);
return $this->_forward('index');
}
}
and users_has_pais model:
class Model_UsersHasPais extends Zend_Db_Table_Abstract
{
protected $_name = 'users_has_pais';
public function updateUserPais($id, array $paises)
{
$row = ($r = $this->fetchRow(array('users_id = ?' => $id))) ? $r : $this->createRow();
foreach($paises as $pais){
$row->users_id = $id;
$row->pais_id = $pais;
$row->save();
}
}
}
One way would be to create user row first, and use it's ID when creating rows for 'user_has_pais'. A pseudo-code is below:
public function createAction()
{
$this->view->pageTitle = 'Create User';
require_once APPLICATION_PATH . '/models/Users.php';
$userForm = new Form_User();
if ($this->_request->isPost()) {
if ($userForm->isValid($_POST)) {
$userModel = new Model_User();
$newUserRow = $userMode->createUser(
$userForm->getValue('email'),
$userForm->getValue('password'),
$userForm->getValue('url'),
$userForm->getValue('responsable'),
$userForm->getValue('role')
);
$user_id = newUserRow->id;
$checkBoxValues = $userForm->getValue('pais');
// $checkBoxValues should be an array where keys are option names and
// values are values. If checkbox is not checked, than the value = 0;
// At this moment I'm not 100% sure of the real nature of the 'pais' value,
// but this is only an example.
// I also assume that the values of the checkboxfields correspond to IDs in
// 'pais'.
foreach ($checkBoxValues as $key => $pais_id) {
if (intval(pais_id) > 0) {
// if language was checked
// do insert into user_has_pais having $pais_id and $user_id.
}
}
return $this->_forward('list');
}
}
$userForm->setAction('/user/create');
$this->view->form = $userForm;
}
You could also put all of this in transaction if you want.
Hope this helps, or at least you point you in the right direction.