bp_activity_add() not working in theme ajax hooks - ajax

I am trying to add an activity for the current user through a function hooked to wp_ajax
add_action("wp_ajax_add_pair", "some_function");
function some_function(){
$args = array("action"=>"action string");
bp_activity_add($args);
}
some_function is called through ajax but I get bp_activity_add does not exist. This makes me think that BP hasnt loaded yet, but var_dump($bp) shows the variable is set. So need help to record activity for a user through ajax

Don't forget to enable activity streams from the backend! worked for me!

Related

laravel - if the database changes, send notification

if any data has been inserted into the database, I want to alert message to another page. How can I do it?
This is the controller page:
public function insertFunction(Request $request) {
$insert = New ExampleTable;
$insert->test_id = $request->id;
$insert->save();
// after this i want to send notification to the another page(panel.blade.php) without refreshing
}
This is the panel.blade.php where I want to see the notification:
<script>
// if notification comes, do it
alert("notification came");
</script>
I'm using mysql. If you help me I will be glad, thanks.
Assuming that you want real time notification, the best practice would be to use the broadcast functionality of Laravel.
Most likely, you will end up using something like pusher.
However, you could make a custom java-script loop that continuously asks your endpoint if there has been any updates, but that would be bad practice and is not recommended.
This is an article that I found helpful. Good luck!

Magento 2 session unsetting itself

I need to be able to store some custom session variables that exist for the custom, regardless of whether they're signed in or not, but for some reason my sessions keep deleting themselves.
I have used this example to help me add my session code in.
Here's my code
Block file
<?php
namespace MyVendor\MyModel\Block;
use Magento\Framework\View\Element\Template;
class ProductSearch extends Template {
protected $_customSession;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Model\Session $customSession,
array $data = []
){
parent::__construct($context, $data);
$this->_customSession = $customSession;
}
//Get the car model from the session
public function getSessionCarModel(){
return $this->_customSession->getCarModel();
}
//Unset the car model from the session
public function unsetSessionCarModel(){
return $this->_customSession->unsCarModel();
}
}
and heres the top of my template file that sorts the session when its loaded
productsearchbanner.phtml
<?php
//If the user has selected a new model, unset our session then start a new one
if(isset($_POST['modelSelect'])){
//Unset the other sessions
$block->unsetSessionCarModel();
//Set the model session
$block->setSessionCarModel($_POST['model']);
}
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
?>
The way the code is meant to work is, if the $_POST['modelSelect'] is set, the user has come from the model select page so we need to start the process again and reset their session, but if they haven't, the session should remain the same.
My issue is, when I come from the model select page, my session variable shows in the var dump no problem, as shown below.
But then as soon as I go to any another page on my site (for example, the homepage) and then back to the product search page, the session has cleared?
What am I doing wrong? Why does my session clear every time I load a page? I just need to be able to set the equivalent of $_SESSION['carModel'] and it be persistent for that user, regardless of if they're logged in or not, or where on the site they go.
Can someone please point me in the right direction?
Setting sessions in Blocks or Template files is a problem. This is because of full page cache. Magento's execution cycle changes with FPC turned on.
Controllers or Models are the best places to update session data.
But, if you need to update your session in a template / block, then you can call a custom action via AJAX and have it update the session info.
Generally, one would need to take these steps in Magento 2:
create a new controller / action pair in an existing or a new module, that would update session info. This controller should ideally accept AJAX requests only.
have a template rendered in container before_body_end and toss some jQuery code in there, that will query the controller / action pair to have the session info updated.
This way, whenever the page will load, it will trigger a session update ( or you can have it trigger on any other event, say when a user clicks something etc. ) by requesting your controller / action, say, /my-module/my-controller/my-session-updater-action.

user->setParam in onUserAfterSave plugin has no effect

I have a (currently working) plugin which creates a user on a third party system when a user registers. This is working fine so far.
I'm trying now to add a param to the user to store the third party id but this doesn't seem to be working:
function onUserAfterSave($user,$isNew,$success,$msg=''){
if(!$isNew || ! $success){
return;
}
jimport('joomla.log.log');
$res = someThirdPartyCall();
//Res is valid here
JLog::add("Res ".print_r($res,true), JLog::WARNING, 'jerror');
$userOb = JUser::getInstance($user['id']);
$userOb->setParam('sugarid', $res['id']);
//User ob is valid here
JLog::add("UserOb ".print_r($userOb,true), JLog::WARNING, 'jerror');
$saveRes = $userOb->save();
//Result is true. Error array is empty.
JLog::add("Result ".print_r($saveRes,true), JLog::WARNING, 'jerror');
JLog::add("Errors ".print_r($userOb->getErrors(),true), JLog::WARNING, 'jerror');
}
Everything looks great, no errors or the like. The only thing not working is that the params aren't set in the db. Is this because I'm trying to save the user in onUserAfterSave?
You have forgot to import the user library in to your plugin to use setParam. So At the beginning of your file do not forget to include user library. Use this line of code.
jimport( 'joomla.user.user' );
Hope this will help.
If save is finished you can't go back and add to it, the save is finished and door is shut. You need to do set things up when you set up the paramters by making a form plugin to add another field to the params. Then you don't need to save at all because params will just save as part of the normal process. I'm assuming this is not something that needs to be encrypted, right? It's just the user name?
Also I should mention that there is Juser::defParam($key, $value) that lets you add parameters via code.

I need a global event when data binding occurs in Knockout

I am a newbie to knockoutjs. I have searched examples and so far no luck. I have a page that is a data collection form with the values bound using knockout. What I am trying to do is provide the user with a flag letting him know data is modified and that it needs to be saved. In the app a user may pull down the form and display the data from the server and use it only as information. In other cases he may modify that data. I want to display a label that says something like "data has been modified" to the user once any binding has changed plus if he tries to navigate away from the page I want to warn him the changes will be lost. Is there some event I can subscribe to that tells me when any value has been changed in the model?
Thanks,
Terry
Take a look at Ryan Niemeyer's Dirty Flag. It might be what you are looking for. An example of his method can be seen in this jsFiddle.
this.dirtyItems = ko.computed(function() {
return ko.utils.arrayFilter(this.items(), function(item) {
return item.dirtyFlag.isDirty();
});
}, this);
More info can be found in this SO thread: Knockout isDirty example, using dynamic viewmodule from mapping plugin

Ember Ajax Calls running in serial

Setup:
blah is my ember app
find essentially calls Ember.$.ajax(url, params, method)
the find method isn't a blocking call
Neither of the routes nor controllers have dependencies (needs) on each other
Problem:
I'm trying to figure out why the setupController (I tried activate as well) in ApplicationRoute isn't being execute until after my ajax call returns from my CowRoutes model.
Thing's I've Tried:
If I move the code from the setupController into the model of CowRoute they all run in parallel (they don't belong here at all, especially since they are header footer and I might hit a different route beside CowRoute).
I tried using Ember.RSVP.resolve on my find method, everything still works, it's just still running in serial
ApplicationRoute
blah.ApplicationRoute = Ember.Route.extend({
// setupController runs If a route handler's context changes
setupController: function () {
this.controllerFor('meta_property').set('model', blah.MetaProperty.find('meta_property'));
this.controllerFor('header').set('model', blah.User.find("user"));
}
CowRoute
blah.CowRoute = blah.Route.extend({
model: function (params) {
//this.controllerFor('meta_property').set('model', blah.MetaProperty.find('meta_property'));
//this.controllerFor('header').set('model', blah.User.find("user"));
return blah.Cow.find('cow', params);
//return Ember.RSVP.resolve(blah.Cow.find('cow', params));
}
I guess Ember tries to execute all model hooks of all Routes available af first. It waits for the model hooks to finish before executing all setupController hooks.
Why could this make sense?
Let's have a look at the interface of setupController:
setupController: function(controller, model) {
...
}
Ember is passing the model it retrieved via the model hook into the setupController hook. This is why it has to wait.
But why does it wait for your CowRoutes model hook before running setupController on your ApplicationRoute?
I guess this is because, you might call controllerFor(name) inside setupController and Ember wants all models "to be in place".
But i think this behaviour should not hurt too much, since the model hook is just executed when the App is entered via URL for deserialization of parameters.
Note: This answer is just a guess from my side, but it seems to make sense to me. At least it should be in the right direction :-)
According to convention, If you have a model that is not in the url and across multiple routes, its ideal to put those in the application route ;)

Resources