Accessing Model from View - php

I have a foreach loop which generates links with different IDs. I have an ajax function to process this ID and the username (from session) to add a record to the table. (It's like enrolling for some events).
I want to make an additional statement which will check whether the user has already enrolled for the event with some ID and if so, the link will be deactivated or change color. I've tried creating another variable (which is passed to the view)
$data['myvariable'] = $this->mymodel->myfunction();
This function in model checks all the records from the database where user's username appears and insert to array all event IDs. I've tried adding an additional if statement before links in foreach loop which checked whether the ID from link is in array but there appeared some problems with the controller. (i think that I couldnt assign the array to the variable $data['myvariable'] ).
I know that accessing to model from view is not "proper"... Anyone know how to solve this problem?

Do you have a field in the DB to store whether or not the user has enrolled? Is so, run a select query on that table checking for that value then use a conditional statement to effect the link. Kinda like this
$enrolled = $this->your_model->your_method($param);
if($enrolled){
process links here;
}

I'd say it depends on the MVC your using also. You may have to load the model in the view to use it from within the view, not ideally the right approach as you could load it via your controller and set it as a variable to be passed into the view if the variable to be passed contains multiple outputs such as a query from a DB or something you can pass it to the view as an array object and then in the view do a while, for, foreach, whatever on that array.

Related

Accessing x-modal data and storing in database

I am having some trouble accessing x-model data in my laravel app and storing it into my database. Wondering if anyone can help.
So the issue seems to be coming from the fact that I have the category select as a separate blade that I call multiple times.
So ultimately, this code is called three times on my main page. I can then choose three separate categories - but when I try to call those selections and store them in database I run into an error.
Wondering if anyone has any ideas how to access the three separate categories being selected and add them to database. Some links or documentation to check out would be mighty helpful!
Using alpinejs, php, and laravel.
EDIT
In this particular case I am passing the information into the blade through the controller.
EDIT 2:this shows $selection_id
x-model takes the name of a variable inside x-data, not the value.
$selection_id here is a value.
You should do something like:
x-data="{ selection_id: {{$selection_id}} }"
on the component element
then inside x-model:
x-model="selection_id"
Note: Your code is missing, please add the part with the `x-data showing.

Typo3 Plugin Inline Records in Frontend Controller

I'm trying to create a Typo3 Extension using extbase and stumbled upon a problem.
What I have done so far is this:
I created a plugin editable via flexform for the backend. The plugin allows for dynamically changing some fields, so the flexform is using inline records that can be stored successfully in the database in their own table tx_jwfrontendusermanager_editorfields. The pid for the table entries is the same as for the content object they are attached to, and an additional field in the table holds the content object's id.
The backend works fine so far, I can create/edit/delete inline records for the content objects without any problem.
Unfortunately, I can't seem to figure out how to access the inline records in the ActionController in the frontend, and I can't find any documentation about how to do so. Could anyone point me into the right direction?
What I have tried so far:
First (primitive) approach: Accessing the flexform inline field like any other field from the form. This just returns the number "7". Obviously it's not meant to be used this way.
Second approach: I created a Model/Repository for the EditorFields table, and mapped the Model class to the table using Typoscript. The mapping itself seems to work fine. Unfortunately, I cannot select just the records that are connected to the current content object. A call to Repository->findAll() returns an empty array.
I debugged the SQL query for the findAll command and found out three problems:
The query filters by the tx_jwfrontendusermanager_editorfield.pid field, but using the wrong page id. The pid used is the one I selected in the plugin's 'Record Storage Page' field, not the one of the content object.
The query does not filter for the content object id, but would show all records for all content objects on the page.
The query filters by an additional
`tx_jwfrontendusermanager_editorfield`.`type` = "\Jw301\JwFrontendusermanager\Domain\Model\EditorField"
Which doesn't make any sense, as none of the records has the type field set to something like this. The type is used by the backend to distinguish between different kinds of records. That way, the query will never have any results.
So, how can this be changed in a way that the Repository finds the correct records? Is this even the right approach?
Third approach: As the Repository approach wasn't successful, I tried to get the records myself by creating a custom SQL query. This works in general, but I still have trouble filtering the right records for me.
I can get the current page id easily using
$GLOBALS['TSFE']->page['uid']
But there seems to be no way to get the content object id in the ActionController. I found many Internet sources that said I should use
$cObj = $this->configurationManager->getContentObject();
$cObj->data['uid']
from within the ActionController to access the current content object id, but for me that didn't work. I tried this in every ActionController in my project, yet the uid field was never there. The $cObj I get is of type ContentObjectRenderer, but the data array is always empty.
How can I access the content object uid from within an ActionController?
Thanks for any help or hints that lead me into the right direction.

Assign a value to a $_SESSION variable uppon clicking on a button

here is the story : I want to transfer an object from a page to an another (let's say from list.php to product_details.php)
I know you can achieve this using serialisation, and storing your object variable into a $_SESSION variable, assuming my product_details.php will load the class to retrieve methods.
However, I don't want to create as many $_SESSION variables as I have products in my list.php.
On my list.php, every product have its a href button that goes to product_details with a $_GET (like <a href="product_details.php?pid=15> )
I know I could just recreate a new object on my product_details.php with the $_GET value and an extra query on the database. I'm learning stuff and trying to handle new approaches.
My page list.php have only one object at a time, which is constructed and unset at every row. It can have as many objects as there are products if needed though.
The question is, how could I do (or which language should I use/learn) to make a given <a href="product_details.php?pid=15"> type button perform the following :
$_SESSION['product'] = serialize($objProduct);
before it actually jumps to my product_details.php?
Thanks

Link that passes parameters to Controller function

I currently have a view that is shared by two controller functions that pass in correct variables so i know where it is coming from.
I have a situation where I need URL references that can be clicked by the user that point to a function in both controllers that queries a Model and gets all the data from that model.
I am looking for some pointers on how to handle passing 2 parameters to the controller based on what the user clicked. I will post code to make this more understandable. Also, if I have a function that is querying the entire database, how can I use Query Scopes, so that if these parameters are passed to it, it doesnt query the entire database but instead uses my query scopes.
Here is the code for reference:
the View: http://pastebin.com/dWCEKztS
one of the controllers that I am working on first. The function that queries all of the rows of that Model: http://pastebin.com/LUbMLajs
In the Model, I tried to create these Query Scopes: http://pastebin.com/s30saqBL
So basically in the view, it displays certain names, and corresponding counts for each status. I have this all taken care of in that $totalCount data structure. I need to pass these to the index function so I can query only that certain data based off those parameters. The admin is in another table called admin, and in the advertiser application table there is an assigned_to field that links with that admin ID.
Please let me know if I need to clarify anything further!
Thanks
Set the URL to pass variable with it, in your route.
route::get('/pageName/{variableName}', array('as' => 'page-name', 'uses' => 'yourControllerName#functionName'));
in controller:
public function functionName($variableName){
$get = Model::where('columnName', $variableName)->first();
}
Hope this help.

Accessing user profile variables

Using the profile module I've created a textfield called profile_real_name which the user fills out when registering. How do I access this variable in the node.tpl.php?
I used the dsm($user) function to output the user variables and it contained everything except the data for the profile_real_name
I also ran dsm($vars) on the phptemplate_preprocess_user_profile and I could see it contained an object called account which did contain the info I needed but this object isn't available in the $user variable.
Many thanks
If you want to access the author's profile information in node.tpl.php, then you want to work with a phptemplate_preprocess_node function rather than the user_profile one. The node preprocess function doesn't have an $account object by default though, so you'll have to load it in:
This goes in the phptemplate_preprocess_node function in your template.php file:
if ($vars['uid']) {
$vars['account'] = user_load(array('uid' => $vars['uid']));
}
Then you would be able to access the author's profile values in your node.tpl.php. The value you asked about specifically would be:
print($account->profile_real_name);
However, it sounds like you might want the node author's name to appear as the profile_real_name value rather than their account name?
If so, a MUCH more efficient way would be to override the theme_username function.
That's not directly what you asked about so I won't go into it here, but this post on the drupal.org forums would be an excellent place to start for Drupal 5 or 6:
http://drupal.org/node/122303#comment-204277
$account is what you usually call a user that isn't the global user to avoid accidently overwriting the global user which would result in the user be get logged in as that user.
I just did a bit of checking and the easiest way to solve your problem is to use $account in the template instead of $user.
Using $user in the template or doing like WmasterJ suggests is faulty. You will post the wrong data. You will post the data of the logged in user not the data of the user who's profile is being watched. This bug will happen when you view all other users' profile than your own.
Preprocess functions is not hard to make, in your template.php file in your theme you just replace phptemplate with your theme's name defined the code. In this case you wont need to alter the preprocess function, since you already have what you need.
If you want to do this within for instance the user-profile.tpl.php all the information you need exists within the $account array.
Otherwise you can access user data by loading a user object based on it's id (of the currently logged in person that is, or if you can query the DB and get uid that way).
First get the uid of the current user:
$uid = $user->uid;
Then load the a user object:
// Create user objets based on uid ()
$user_obj = user_load($user->uid);
Then load that users profile variables:
// Load profile
profile_load_profile($user_obj);
Now the $user_obj variable (which is passed by reference to profile_load_profile) has an object with the the profile information that can be accessed like this:
$user_obj->profile_real_name
Hope it helps!

Categories