Chtml::link not getting ID of event attendee - php

I'm currently trying to redirect a user from a view in the events folder, going to the user folder with the id of the event attendee.
The issue I'm having is that the url is returning without the idea, in this case it should be 56. /user/InviteGuest/eventId but no ID
<p>You can also invite a guest to attend this event with you.
<?php echo CHtml::link("Click here to invite a guest", array('/user/InviteGuest', 'eventId' => EventAttendees::model()->id))?></p>
It doesn't seem to be getting the ID from the model and populating the field ID, it works fine when I link from within the event attendee views.
edit. for example in a view in the eventattendees folder
array('label' => 'Invite Guests','buttonType'=>'link', 'icon' => 'group', 'url' => array('/user/InviteGuest', 'eventId' => $data->id)),
work fine

'eventId' => EventAttendees::model()->id
This is not getting any data, unless your EventAttendees' id is set to 56 by default, which is really odd. "id" is usually used for primary keys.
I think what you're missing is to load your data. Maybe something like this:
// Exemple, not actual tested code
// In your controller
$attendee = EventAttendees::model()->findByAttributes( array(
'whatever'=>$something,
));
$this->render('view', array(
'attendee'=>$attendee,
));
// In your view
<?php echo CHtml::link("Click here to invite a guest", array('/user/InviteGuest', 'eventId' => $attendee->id))?></p>

Related

Drupal 7 hook_form_alter and submitting the form

I added some custom field so that i can get more information when someone registers into the website but i want to alter one field on the form. I created a module which has a hook_form_alter function so that i can alter the field_first_name of the registration form. here is the code
function user_registration_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'user_register_form'){
$form['field_first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#description' => t('Enter your first name.'),
'#maxlength' => 255,
'#required' => TRUE,
);
}
if (isset($form['actions']['submit'])) {
$form['actions']['submit']['#submit'][] ='user_registration_form_submit';
}
}
I also created a the function which handles the form submission.
function user_registration_form_submit(&$form, &$form_state)
{
$fe_id = db_insert('field_revision_field_first_name')
->fields(array(
'field_first_name_value' => $form_state['value']['field_first_name'],
))->execute();
drupal_set_message(t('your form entry has been added'));
}
My problem is that when i submit the form and i check the user details. i find that the first name details does not exist in the database or when i login as the administrator and click on the 'people' link. i find that all information is are submitted except the first name field which i am trying to alter. I also tried to submit the form without the form submit function but it still doesn't work.
and i get the following error message if i add the form_submit function
Notice: Undefined index: value in user_registration_form_submit() (line 37 of /var/www/html/lite/sites/all/modules/user_regestration/user_registration.module).
PDOException: SQLSTATE[HY000]: General error: 1364 Field 'entity_id'
doesn't have a default value: INSERT INTO
{field_revision_field_first_name} (field_first_name_value) VALUES
(:db_insert_placeholder_0); Array ( [:db_insert_placeholder_0] => ) in
user_registration_form_submit() (line 38 of
/var/www/html/lite/sites/all/modules/user_regestration/user_registration.module).
THIS IS LINE 37 AND 38 OF MY CODE
'field_first_name_value' => $form_state['value']['field_first_name'],
))->execute();
I am creating the module on localhost first before i push it to the live website
First, it's values not value.
Why don't you use a module providing this functionality, like profile2 ?
The user is not created in your submithandler, so either you save it:
function foo_user_register_form_submit($form, &$form_state){
$edit = array(
'name' => $form_state['values']['name'],
'pass' => user_password(),
'mail' => $form_state['values']['mail'],
'init' => $form_state['values']['mail'],
'status' => 1,
'access' => REQUEST_TIME,
'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
);
user_save(drupal_anonymous_user(), $edit);
}
For more details see the docs.
Or you could try to add a custom submit handler, which would be fired after the user is created, in form_alter:
$form['#submit'][] = 'your_custom_submit_handler_function';
In there the user should already be created. so:
function your_custom_submit_handler_function(&$form, &$form_state)
{
$fe_id = db_insert('field_revision_field_first_name')
->fields(array(
'field_first_name_value' => $form_state['values']['field_first_name'],
))->execute();
drupal_set_message(t('your form entry has been added'));
}
But keep in mind, that this custom inserting maybe needs some more
code, here you only add the revision entry ..
You're not passing in an entity_id, and your database schema isn't defining a default one. The database expects one, and so it is failing, and it is failing on insert - which is your lines 37 and 38. It has absolutely nothing to do with your first name part.
Basically, Drupal (technically MySQL, or whatever your database backend is) doesn't know what entity you're trying to associate that field with.
Please look at the table that you're actually putting the information into and ensure that the schema either has a default set
Something like:
db_insert('field_revision_field_first_name')
->fields(array(
'field_first_name_value' => $form_state['values']['field_first_name'],
'entity_id' => 1,
))->execute();
Would probably work better for you. There may be other required fields. I recommend taking a look at your database schema.
You also shouldn't have to define a custom form for this - you can add fields to users, and also there is profile2 as another poster mentioned.

Pass line id as argument in x-editable or TbEditableColumn to update a particular record

I am trying to use TbEditableColumn / X-Editable to make inline edits in the CGridView. I'm trying to use the usual $data variable to get row id by the intuitive "$data->id" to pass the line id in the dynamically generated url so that in the update action of the controller, I'd know which record to update.
However $data->id is just not working while creating URL. Interestingly the same $data variable is accessible while applying "Editable" property on the item. See below.
I don't want to go for JS:function way to fetch id of an element and pass it as argument in ajax call. This just doesn't seem elegant.
array(
'class' => 'editable.EditableColumn',
'name' => 'mrp',
'headerHtmlOptions' => array('style' => 'width:200px'),
'editable' => array(
'type' => 'text',
'apply' => '$data->id>10605',
'url' => '/product/changeMRP/id/$data->id',
),
),
In above code, this line works
'apply' => '$data->id>10605'
But this line doesn't
'url' => '/product/changeMRP/id/$data->id',
Why is it that? And how can i solve my problem?
I followed this question :
How can update db values using x-editable EditableColumn? but it didnt help
You can use createUrl() for creating URL and pass the id in array. Like this
'url' => "Yii::app()->createUrl('product/changeMRP', array('id'=>'$data->id'))",
Hope it will solve your problem.

A row associates with session is deleted when user logout in YII

I have a site that uses YII. One thing that it does is storing session information into a MySql database table YiiSession. Also in a separate table (users_sessions), a new row is inserted with the following information:
session id
user id
online status of a user
I create another table for session because YiiSession is part of YII. Also users_session keeps track online status of a user, whereas YiiSession doesn't.
I only make an insertion into users_session during user login.
But I don't understand that when user logs out, the session that associates with the user got deleted.
Note that the session_id in the users_session is a foreign key to the one in YiiSession. But the one in YiiSession still exists even though it has an expiration date.
What mechanism that possibly deletes the row? Please help.
You should extend CDbHttpSession and overwrite the method destroySession($id). There you append the code and delete your entry also.
Configure Yii to use your session class instead of it's own in config/main.php:
'session' => array(
'autoStart' => true,
'class' => 'application.components.<your session class name>',
'connectionID' => 'db',
'sessionTableName' => 'session',
'autoCreateSessionTable' => false,
'timeout' => 3600, // 60 min
),
However I would not do this with a separate table, just add some fields to the YiiSession table. That's what I did and it works pretty well.
EDIT:
You should create a file named MyDbHttpSession.php in protected/components.
In this file extend CDbHttpSession like so:
class MyDbHttpSession extends CDbHttpSession
{
protected function createSessionTable($db, $tableName)
{
// basically this is a copy of CDbHttpSession
$db->createCommand()->createTable($tableName, array(
'id' => 'CHAR(32) PRIMARY KEY',
'expire' => 'integer',
'data' => $blob,
// your additional fields
'idUser' => 'INT(10) NULL',
'online' => 'TINYINT(1)',
// more of your fields here if you have
));
}
// this sets the user ID by session ID
public function setIdUser($idUser)
{
$db = $this->getDbConnection();
$db->setActive(true);
$db->createCommand()->update($this->sessionTableName, array('idUser' => (int) $idUser),
'id=:id', array(':id' => $this->sessionId)
);
}
// and this sets the online status, pass true or false to $online
public function setOnline($online)
{
$db = $this->getDbConnection();
$db->setActive(true);
$db->createCommand()->update($this->sessionTableName, array('online' => (int) $online),
'id=:id', array(':id' => $this->sessionId)
);
}
}
Set Yii to use your session class instead of it's own like I wrote above.
You should already have a class WebUser. If not, create one in protected/components and extend CWebUser.
In this class add a method afterLogin:
public function afterLogin($fromCookie)
{
// store user ID and online status in session table
Yii::app()->session->setIdUser($this->id);
Yii::app()->session->setOnline(true);
return parent::afterLogin($fromCookie);
}
You can from wherever you are in Yii use Yii::app()->session->setOnline(true); or Yii::app()->session->setOnline(false);

Opencart adding a new field to the customer view of the admin section

I have added a new field to my database. I also have managed to add necessary codes and functions in catalog section. This new field is related to the customer. The data related to this new field gets added successfully to the database.
This new field belongs to Customer table.
Now, I want to know, when viewing customer's details in the admin section, how this new field should be retrieved from database? I mean which file should be edited for this purpose?
getCustomer($customer_id) and getCustomers($data = array()) are the functions used to get customer data.
Since they are SELECT * querys your field is being processed automatically.
Afterwards you need to go in the Controller section in the controller\sale folder and there you have customer.php, custom_field.php and and edit the ones that you need. For example:
$this->data['customers'][] = array(
'customer_id' => $result['customer_id'],
'name' => $result['name'],
'email' => $result['email'],
'customer_group' => $result['customer_group'],
'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
'approved' => ($result['approved'] ? $this->language->get('text_yes') : $this->language->get('text_no')),
'ip' => $result['ip'],
'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
'selected' => isset($this->request->post['selected']) && in_array($result['customer_id'], $this->request->post['selected']),
'action' => $action
);
add your field in this array (this is from customer.php).
And finally edit the .tpl files that are called from the view\template\sale folder so they appear where they want them to.
Hope i was clear enough.

Yii create CButton Column for field that exists in another model

I'm trying to add the following functionality, however I'm not sure where to start. Any advice, examples, or direction would be greatly appreciated.
I want to add button to the cgridview of the main model in this context. Each of the records available in the cgridview for this model, have a unique attribute called lot for example R3XSEF9
There is another secondary table/model in my database that has records with this same lot attribute. However, this table only has certain records out of all the possible records, sometimes duplicates, and has a set of different attributes.
What I'd like to do is, using the lot attribute, for example lot R3XSEF9 from my cgridview, search the secondary table to see if there is one ore more corresponding rows which contains that same lot R3XSEF9.
If so, I would like the button to be displayed in my CButtonColumn and link to the views for those corresponding models of the secondary table. If not, I would like no button to appear.
Thanks for any help. If any clarification is required, I would be happy to do so.
First of all you need to link tables using "relations" function in model class. If you use FOREIGN KEY constraint in DB relations already filled.
SQL statement:
CREATE TABLE Model1
(
...
FOREIGN KEY(lot) REFERENCES MainModel(lot) ON UPDATE CASCADE ON DELETE RESTRICT,
...
)
Model class:
class MainModel extends CActiveRecord
{
...
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'lots' => array(self::HAS_MANY, 'Model2', 'lot'),
);
}
Then you can use custom button column in your grid (view file) like this:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'main-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
...
array(
'class' => 'CButtonColumn',
'template' => '{lots}',
'header' => 'Lots',
'buttons' => array(
'lots' => array(
'label' => 'Lots',
'imageUrl' => Yii::app()->request->baseUrl.'/img/....png',
'url' => 'Yii::app()->createUrl("controller1/lotlistbymainid", array("id" => $data->id))',
'visible' => 'count($data->lots) > 0',
),
),
),
Explanation of button parameters to be passed thru "buttons" array you can find here. Especialy this part:
buttons property
public array $buttons;
the configuration for additional buttons. Each array element specifies a single button which has the following format:
'buttonID' => array(
'label'=>'...', // text label of the button
'url'=>'...', // a PHP expression for generating the URL of the button
'imageUrl'=>'...', // image URL of the button. If not set or false, a text link is used
'options'=>array(...), // HTML options for the button tag
'click'=>'...', // a JS function to be invoked when the button is clicked
'visible'=>'...', // a PHP expression for determining whether the button is visible
)

Categories