How to define a user (author) - OctoberCMS - php

I need help. I only need to be able to view one user for a post in OctoberCMS.
But all users will always show up to me.
Here's the code that works, but I don't know how to define it to work the way I need it.
idea: https://ibb.co/VM2Rrr3
public function getUserOptions()
{
$result = [0 => 'indikator.news::lang.form.select_user'];
$users = Db::table('backend_users')->orderBy('login', 'asc')->get()->all();
foreach ($users as $user) {
$name = trim($user->first_name.' '.$user->last_name);
$name = ($name != '') ? ' ('.$name.')' : '';
$result[$user->id] = $user->login.$name;
}
return $result;
}

Use this as user who is creating project is user who is currently logged in so we can use current logged-in user that.
use BackendAuth;
public function getUserOptions()
{
// placeholder
$result = [0 => 'indikator.news::lang.form.select_user'];
// only back-end user which is currently logged in
// it will be available always as we are in back-end :)
$user = BackendAuth::getUser();
$result[$user->id] = $user->login; // or $user->email;
return $result;
}
It will get logged in user and show in drop-down for selection [ or even you skip place-holder to have only 1 selected option]
if any doubts please comment.

Related

Compare laravel query results

Inside the $get_user and $get_code queries they both have a group_id.
I have dd(); them Both and made 100% sure.
the $get_user query has multiple group_id's and the $get_code only has one group_id which is equal to one of the $get_user group_id's.
The goal at the moment is to create a group_id match query.
Get the code that has a group ID equal to one of the $get_user group_id's
public function getCodesViewQr($code_id)
{
$userid = Auth::id();
$get_user = GroupUser::all()->where('user_id',$userid);
$get_code = Code::all()->where('id',$code_id);
$group_match = GroupUser::where('group_id', $get_code->group_id);
$view['get_users'] = $get_user;
$view['get_codes'] = $get_code;
$view['group_matchs'] = $group_match;
return view('codes.view_qr_code', $view);
}
The group match query does not work. $get_code->group_id does not get the code group_id.
If there is a match then set $match equal to rue. else $match is False
$group_match = GroupUser::where('group_id', $get_code->group_id);
I'm using two Models Code and GroupUser
My Code table is like this :
-id
-group_id (This is the only on important right now)
-code_type
My GroupUser table is like this :
-id
-group_id (This is the only on important right now)
-user_id
-user_role
I have linked the Models
Inside my Code Model I have the relationship to GroupUser
public function group_user()
{
return $this->belongsto('App\GroupUser');
}
And Inside my GroupUser Model I have the relationship to Code
public function code()
{
return $this->belongsto('App\Code');
}
Inside My Code controller I have included my models.
use App\Code;
use App\GroupUser;
Hi guys so I had some help from a guy I work with and this is the solution he came up with. We made a few adjustments. all the Databases and results stayed the same. we just changed the method we used to get the results.
I really appreciate all the help from #linktoahref
public function view_code($random)
{
$code = Code::where('random', $random)->first();
$view['code'] = $code;
if ($code->code_type == 1)
{
// Its a coupon
if (!empty(Auth::user()))
{
// Someones is logged in
$user = Auth::user();
$view['user'] = $user;
$user_groups = GroupUser::where('user_id',$user->id)->pluck('group_id')->toArray();
if (in_array($code->group_id, $user_groups))
{
// The user is an admin of this code
return view('view_codes.coupon_admin', $view);
}else
{
// Save the code to that users account
return view('view_codes.generic_code', $view);
}
}else
{
// Anon
return view('view_codes.coupon_anon', $view);
}
}elseif ($code->code_type == 2)
{
// Voucher..
}else
{
// We don't know how to deal with that code type
}
}
$get_code = Code::find($code_id);
// Check if the code isn't null, else give a fallback to group_id
$group_id = 0;
if (! is_null($get_code)) {
$group_id = $get_code->group_id;
}
$group_match = GroupUser::where('group_id', $group_id)
->get();
$match = FALSE;
if ($group_match->count()) {
$match = TRUE;
}

How to merge multiple Eloquent collections and sort them by timestamp?

So I have two collections one has posts made by the user that's logged in the other is posts made by users that he is following. I want to merge these two collections together and then sort them as one collection.
this is what I tried:
$feed = $friendPosts->merge($posts);
$feed->sortByDesc('created_at');
The problem is that they do get merged together, but the sort function seems to not work. instead of them being mixed together they come in parts. so all the $friendPosts posts come and the other posts come after wards
I'm using slim framework along with twig and eloquent. here's the entire controller that this is being used in, just for some context:
public function getSessionProfile($request, $response)
{
//return to sign in if not signed in
if ($_SESSION['user'] == null) {
return $this->view->render($response, 'templates/signin.twig');
}
//grab current user
$user = User::where('id', $_SESSION['user'])->first();
//grab the user's followers
$followers = Follow::where('follower_id', $user->id)->get();
//user posts
$posts = $user->posts;
//get the posts made by friendllowers
foreach ($user->follows as $follow) {
$follow = User::where('id', $follow->follower_id)->first();
//posts by people that user is following
$friendPosts = $follow->posts;
}
//append author to each post made by friendllowers
$friendPosts->map(function ($post) {
$postAuthor = User::where('id', $post->user_id)->first();
$post['authorName'] = $postAuthor->name;
$post['authorPicture'] = $postAuthor->avatar;
return $post;
});
//append an author to each post made by user
$posts->map(function ($post) {
$postAuthor = User::where('id', $post->user_id)->first();
$post['authorName'] = $postAuthor->name;
$post['authorPicture'] = $postAuthor->avatar;
return $post;
});
$feed = $friendPosts->merge($posts);
$feed->sortByDesc('created_at');
$this->container->view->getEnvironment()->addGlobal('User', $user);
//posts by user
$this->container->view->getEnvironment()->addGlobal('Posts',$user->posts);
//feed
$this->container->view->getEnvironment()->addGlobal('Feed',$feed);
// TODO: make two twig templates one for user only one for feed
$this->container->view->getEnvironment()->addGlobal('Followings', $user->follows);
$this->container->view->getEnvironment()->addGlobal('Followers', $followers);
return $this->view->render($response, 'home.twig');
}
I fixed the problem:
needed to change $feed->sortByDesc('created_at'); to
$feed = $feed->sortByDesc(function($post)
{
return $post->created_at;
});

Setting default value to user extended field in Modx

Basically I want the value of a TV (a.k.a. Template Variable) to default to the value of a user's extended field. I tried making a snippet named get_author with the following content:
$user = $modx->getUser();
if (!$user) return '';
$profile = $user->getOne('Profile');
if (!$profile) return '';
$extended = $profile->get('extended');
return(isset($extended['author_page'])) ? $extended['author_page'] : '';
And add the following code to a TV's default value property:
#EVAL return $modx->runSnippet('get_author');
It looks like the code itself works, but the value doesn't default to the desired value until the user clicks the 'set to default' button in the manager. But when I fill in a static value like 8 (so without the #eval) the value immediately defaults on form load. Can anyone tell me how this has to be done?
Thanks in advance!
It is not possible, the way you trying this. The default value is calculated during rendering the template variable and not during saving the resource, only clicking on set to default will help as you mentioned above.
There are two solutions possible: Set the template variable value to the value in a plugin running on OnDocFormSave
$user = $modx->getUser());
($user) {
$profile = $user->getOne('Profile');
if ($profile) {
$extended = $profile->get('extended');
if (isset($extended['author_page'])) {
$resource->setTVValue('my_tv', $extended['author_page']);
}
}
}
or run your code in a snippet 'AuthorPage' that uses the value of createdby or editedby as userId parameter.
$output = '';
$userId = $modx->getOption('userId', $scriptProperties, 0);
$user = $modx->getUser($userId);
($user) {
$profile = $user->getOne('Profile');
if ($profile) {
$extended = $profile->get('extended');
if (isset($extended['author_page'])) {
$output = $extended['author_page'];
}
}
}
return $output;
Call that snippet like this:
[[AuthorPage? &userId=`[[*createdby]]`]]

How to access a certain GET data in CakePHP?

I am currently writing an adress book and using a framework (CakePHP) an MVC for the first time. Unfortunately I have some trouble.
I want to realize the following:
In case the URL is
/contacts/view/
I want to show all contacts in a list. In case there is an id given after /view/, e.g.
/contacts/view/1
I just want to display the contact with the id 1. (complete different view/design than in the first case)
My ContactsController.php is the following
public function view($id = null){
if(!$this->id){
/*
* Show all users
*/
$this->set('mode', 'all');
$this->set('contacts', $this->Contact->find('all'));
} else {
/*
* Show a specific user
*/
$this->set('mode','single');
if(!$this->Contact->findByid($id)){
throw new NotFoundException(__('User not found'));
} else {
$this->set('contact', $this->Contact->findByid($id));
};
}
}
But "$this->mode" is always set as "all". How can I check whether the id is set or not?
I really want to avoid "ugly" URL-schemes like ?id=1
Thanks in advance!
Your code is only meeting the if part and its not going to else part. Use (!$id)..
$_GET data is retrieved through the URL. In CakePHP this means it's accessed through that method's parameters.
I'm arbitrarily picking names, so please follow! If you're in the guests controller and posting to the register method you'd access it like this
function register($param1, $param2, $param3){
}
Each of these params is the GET data, so the URL would look something like
www.example.com/guests/param1/param2/param3
So now for your question How can I check whether the id is set or not?
There are a couple of possibilities. If you want to check if the ID exists, you can do something like
$this->Model->set = $param1
if (!$this->Model->exists()) {
throw new NotFoundException(__('Invalid user'));
}
else{
//conduct search
}
Or you can just search based on whether or not the parameter is set
if(isset($param1)){ //param1 is set
$search = $this->Model->find('all','conditions=>array('id' => $param1)));
}
else{
$search = $this->Model->find('all');
}
You should only change the conditions not the whole block of code like
public function view($id = null){
$conditions = array();
$mode = 'all';
if($id){
$conditions['Contact.id'] = $id;
$mode = 'single';
}
$contacts = $this->Contact->find('all', array('conditions' => $conditions));
$this->set(compact('contacts', 'mode'));
}

Using flashdata while posting same controller twice in Codeigniter

I am trying to submit a EDIT form which edits Users Academics Details,
These Details have unique id in DB and my Code in Short Looks like below :
class edit extends ci_controller
{
function user_academics($id = NULL)
{
if(isset($id) == FALSE) //if link is ./edit/user_academics
{
$id = NULL;
$link = site_url('profile');
show_error("Invalid Page Request! <a href='$link' Go to Profile </a>");
}
$user_id = $this->session->userdata('user_id');
$data['fill'] = $this->edit_model->get_user_academics($id);
if($user_id != $data['fill']['user_id']) // check if logged in user is accessing his record or others
{
$link = site_url('profile');
show_error("This is an Invalid Request ! <a href='$link'>Go to Profile </a>");
}
else // actual work starts here
{
$this->session->set_flashdata('ua_id',$id); // update_academics will get this data
$this->load->view('edit/edit_3_view',$data);
}
}
function update_academics()
{
$ua_id = $this->session->flashdata('ua_id'); // flash data used here .
if( !$ua_id )
{
show_error('Sorry, This request is not valid!');
}
$academics = array(
// All post values
);
$this->edit_model->update_user_academics($academics,$ua_id);
//print_r($academics);
redirect('profile');
}
}
Now the problem is
- If I open two different records to edit, then It will set only one Session Flash value.
- And No matter what I edit , the existing values of the last flash value gets updated.
Please Suggest me another way or Correct me if I am wrong in above code . Thanks
save that flashdata in array, like:
$myArr = array('value 1', 'value 1');
//set it
$this->session->set_flashdata('some_name', $myArr);
And in view:
$dataArrs = $this->session->flashdata('some_name');
//loop thru $dataArrs to show the flashdata
Flash data is simply like variable which is available only in next request, you can bypass this behavior by using two different keys with record id in it, so that when you use flash data for showing message you can access key with particular record id.

Categories