Dynamic Header Line with CakePHP - cakephp

I have a cakePHP application with a header line. This header contains things like the name of the owner, phone number, email, ...
The owner can change this information, so this header has to be dynamic.
Is it possible to include this header into the layout, so I don't have to include it in every view?
thx
EDIT:
I see, I didn't describe my problem right.
The informations in the header aren't user informations. There should be informations like the name of the owner of the website (or companies name), phone number of the owner, and so on. These informations are the same for all logged in users.
Thanks anyway. The problem seems to be solved in an other Question

yes
assumning the User data is stored in session you can - in your layout - do something like
<div id="#header">
<?php
echo $this->Session->read('Auth.User.name');
// echo other User information
?>
</div>

User can edit his information even after logged in. in that case session data won't be helpful in displaying correct information.
For displaying dynamic data
so better you can set the information from controller and display it in view as like this
Controller
public function beforeFilter() {
parent::beforeFilter();
if ($this->Auth->user('id')) {
$this->loadModel('User');
$this->set('user', $this->User->findById($this->Auth->user('id')));
}
}
View
<div class='header'>
<?php
if (!empty($user)) {
echo $user['User']['name']; // display the info whatever you want..
}
?>
</div>

Related

Wordpress addon to add data to a database and then call the data

I know this question is probably going to get downvoted and I will probably get into trouble but I am hoping someone may be able to help me with my situation.
On my site I use json to download data from an external source, and then I style it beautifully.
Within the json data is an individual ID for each data set.
What I want to accomplish is to have a database where I can insert the ID and a url link.
I have created the table within the wordpress database via phpMyAdmin, but I want to create a page within the admin section where I can simply add the data in.
For displaying the json data I use a php insert addon, within that php clip i want to do a piece of code that checks the database for the id within my custom database and displays the link.
I will be honest I don't know where to start on this, even if its just a link to a source that shows me how to create an admin page and submit data to the database within wordpress dashboard.
I really appreciate any help given and like I say I know I should try harder, but when ever I do a search all I get is 100's of references to add an admin to the database manually.
Thanks,
Adam
Edit I just realized I never put any table information in my question.
The table name within wordpress is called: wp_home_tickets
within that are 3 fields: id (auto increasement), gameid (numeric) and ticketlink (text)
thanks.
For adding a custom settings page in your admin, use the Settings API https://codex.wordpress.org/Settings_API
Here is a nice tutorial using it https://deliciousbrains.com/create-wordpress-plugin-settings-page/#wp-settings-api
To fetch data from your custom table, use the wpdb class https://developer.wordpress.org/reference/classes/wpdb/. More specifically, you can use wpdb::get_results if you will have multiple rows sharing the same id https://developer.wordpress.org/reference/classes/wpdb/get_results/
Or wpdb::get_row if you will ever only have one https://developer.wordpress.org/reference/classes/wpdb/get_row/
Hope this helps you out!
For anyone wishing to see how it was done, here is how I did it.
I created a file in my theme called db_admin_menu.php and added the following to it:
<?php
function ticket_admin_menu() {
global $team_page;
add_menu_page( __( 'Tickets', 'sports-bench' ), __( 'Tickets', 'sports-bench' ), 'edit_posts', 'add_data', 'ticket_page_handler', 'dashicons-groups', 6 ) ;
}
add_action( 'admin_menu', 'ticket_admin_menu' );
function ticket_page_handler() {
$table_name = 'wp_home_tickets';
global $wpdb;
echo '<form method="POST" action="?page=add_data">
<label>Team ID: </label><input type="text" name="gameid" /><br />
<label>Ticket Link: </label><input type="text" name="ticketlink" /><br />
<input type="submit" value="submit" />
</form>';
$default = array(
'gameid' => '',
'ticketlink' => '',
);
$item = shortcode_atts( $default, $_REQUEST );
$gameid = $item['gameid'];
if ($wpdb->get_var("SELECT * FROM wp_home_tickets WHERE gameid = $gameid ")) { echo 'Ticket already exists for this game.'; goto skip; }
if (!empty($_POST)) { $wpdb->insert( $table_name, $item ); }
skip:
}
?>
I then put this code in my script that fetches and displays the json:
$matchid = $match['id'];
$ticket_url = $wpdb->get_var("SELECT ticketlink FROM wp_home_tickets WHERE gameid = '$matchid' ");
if ($ticket_url) { echo 'Get Tickets'; }
I hope someone does find it of use, i did have to use a wordpress plugin called `Insert PHP Code Snippet' by xyzscripts to be able to snippet the php to a shortcode, but that is not the purpose of this post.
Thanks again for your help.

View link if depended on user rights

I am working with CakePhp 2.x. I have three Columns:
User | Course | UserCourseRole
Each user can edit multiple courses and one course can be edited by multiple users. So far so good.
If a user wants to see an index of all the courses i want to show a 'edit'-link only next to the courses which he can in fact edit.
How can i realize this? I figured i would have to set some sort of extra field inside the CourseController and check for this field inside the view. Is this the right way to go?
My current Code is
CourseController.php
...
public function index() {
$courses = $this->Course->find('all', array('recursive' => 2));
$this->set('courses', $courses);
}
...
Courses/index.ctp
<!-- File: /app/View/Courses/index.ctp -->
...
<?php foreach ($courses as $course):?>
...
<?php
echo $this->Html->link('edit', array('action' => 'edit', $course['Course']['id']));
?>
...
In beforeRender() or beforeFilter() set $this->Auth->user() as a variable to the view, for example as userData.
$this->set('userData', $this->Auth->user());
Implement a (auth)helper that uses that variable (you can make it configurable as a helper setting) and do your checks like:
if ($this->Auth->hasRole($course['Course']['role']) { /* ... */ }
if ($this->Auth->isLoggedIn() { /* ... */ }
if ($this->Auth->isMe($course['Course']['user_id']) { /* ... */ }
Implement the hasRole() method according to whatever your specific requirements are.
Doing this as helper as a bunch of advantages, it is easy to reuse, overload and adapt to whatever your checks are and you don't use a component in a view plus that you should avoid calling statics and singletons a lot in your app. Also it is pretty easy to read and understand what the code does.
I think the good idea is set some variable or constans after logged (if user has privileges) and uses if statement for check.
if($allow === true) {
echo $html->link('Edit',...
}
or use AuthComponent::user() in Views.
This idea it's not good if we can many kind of admins (admin, moderator, reviewier, etc.)
Maybe someone will have a better solution

blackhole cakephp 2 associated entities

My Goal:
Reuse of a contact form to be related to several different entities I call "Parents" ie Group has contact information, Member has contact info etc....
The way I tried doing it was:
1. Creating a view file for contact, named "form.ctp" which doesn`t create a new form, nor submits, just echo's the contact's fields.
2. Calling this file using requestAction
My Problem:
The form's _Token get crumbled.
Parent add.ctp example
<?php echo $this->Form->create('Group');?>
<fieldset>
echo $this->Form->input($field_prefix.'contact_id',array('type'=>'hidden'));
<?php echo $this->requestAction(array('controller' => 'contacts', 'action' => 'form'), array('named' => array('index'=>'0','parent'=>'Group',
'fields'=>array(
'email'=>array('value'=>'xx#yy.com','hidden'=>1)
))));
inside the form.ctp I have:
//Associated Model
echo $this->Form->input('Contact.0.city',array('type'=>'hidden'));
echo $this->Form->input('Contact.0.postcode');
echo $this->Form->input('Contact.0.phone');
echo $this->Form->input('Contact.0.cellphone');
echo $this->Form->input('Contact.0.email',array('value'=>""));
echo $this->Form->input('Contact.0.id',array('type'=>'hidden'));
?>
Looking at the HTML source code that is generated, I see that whether I use the request action or just copy the contect of the form.ctp into the "Parent's" add file, I get the same HTML result.
HOWEVER!!! when I use the form.ctp Action Request, I get the blackhole, the tokens are being messed up!!!
Any Ideas?
Thanks in advance
Orly
If your problem is solely reusing a form, you can use the form as a Element, and then you could call it multiple times, substituting in the exact values you need.
As for SecurityComponent, I would recommend (at least as a temporary fix) disabling SecurityComponent for that specific action by using $this->Security->unlockedActions(); in your controller's beforeFilter()

CakePHP $this->Auth->user('id') returns incorrect user in controller

I've got a really bizarre bug occurring in a CakePHP app I've been working on.
I've got the following method in my users_controller.php that reads the currently signed in user and sends the data to a view and sets the title_for_layout to the user's name:-
function account() {
$this->User->id = $this->Auth->user('id');
$this->set('User', $this->User->read());
$this->set('title_for_layout', 'Welcome '.$this->User->Contact->field('name'));
}
In my view I've got (among other things):-
<?php echo $User['Contact']['name'] ?>
Everything in the view looks fine. It is outputting the fields for the correct user (the one I am currently signed in as). However the title_for_layout is using a completely different user's details. So $this->User->Contact->field('name') is not the same as $User['Contact']['name']!
I can't spot what is going wrong here so hoping someone out there can point out my mistake.
Model read() simply does a find('first'), sets the result as the Model $data, and returns it. It doesn't set $ids of associated models.
So in your case, $this->User->id is set, but $this->User->Contact->id isn't.

How to save multiple individual records for a single model

I have a model(friends), where user can export his friend's info from facebook. I want save friends info(id and name) into the mysql database.
I am having trouble creating a form for the same using form helper as i don't know the exact number of friends for each user.
Is there a easier way to save user's friends into the database? (save multiple records for the same model)
Basically you should know how much is the number of friends for the current user, right?
If so, do a loop with following in your view:
echo $this->Form->create('User');
for($i=0;$i<$number_of_current_user_friends;$i++){
echo $this->Form->input('Friend.'.$i.'user_id', array('value'=>$current_user_id));
echo $this->Form->input('Friend.'.$i.'friend_name', array('value'=>$friends[$i]['name']));
echo $this->Form->input('Friend.'.$i.'fb_id', array('value'=>$friends[$i]['fb_id']));
}
echo $this->Form->end('Submit');
then in the controller save it with:
$this->Friend->saveAll($this->data);

Resources