Modx placing placeholders after ajax request - php

I've the page, which loads content by click on the button from user database via jquery ajax.
File, which handles ajax request, has this code:
return $modx->runSnippet('pdoPage',array(
'class' => 'LibraryContent',
'tpl' => 'tpl.lib-main',
'element' => 'Ajax_test'
));
Sender of request receives answer, snippet works ok, I see data, which snippet returns. But, what about placeholders? For example, I want use pagination by this snippet(pdoPage). I cannot send placeholders via ajax, because modx parser have been worked and placeholder will be a plain text. Other path is paste placeholder to resource, which send ajax request, but no way — there is placeholder, but there is no results of pdoPage's work. Finally, placeholder is empty.
So, my question is how to "alive" placeholder for snippet, which loads by ajax request?
thanks.

1'st way:
$output = $modx->runSnippet('pdoPage',array(
'class' => 'LibraryContent',
'tpl' => 'tpl.lib-main',
'element' => 'Ajax_test'
));
$pagination = $modx->getPlaceholder('page.nav');
return $output.$pagination;
2'nd way:
$content = $modx->runSnippet('pdoPage',array(
'class' => 'LibraryContent',
'tpl' => 'tpl.lib-main',
'element' => 'Ajax_test'
));
$pagination = $modx->getPlaceholder('page.nav');
$output = array(
'content' => $content,
'pagination' => $pagination
);
return $modx->toJSON($output);

Related

Inline multiple images Mailgun API Batch

I am trying to pass multiple images via Mailgun's inline API-parameter. I have no problem with only one image, but when I try with multiple images - as in the code below - only the last image in the array is displayed in the email.
$template = View::make('emails.template')->render();
$result = $mgClient->sendMessage($domain, array(
'from' => $sender,
'to' => implode(',',$emailAddresses),
'subject' => '%recipient.subject%',
'text' => $messageText,
'recipient-variables' => json_encode($credentials),
'html' => $template
), array(
'inline' => array(
'path/to/image1.png',
'path/to/image2.png',
'path/to/image3.png',
'path/to/image4.png')
));
The code above works as if the last element in the array is the only element.
The documentation for sending inline images with Mailgun is found here and it's said here that "You can post multiple inline values" which means that I'm definitely doing something wrong.
Try this once:
$result = $mgClient->sendMessage($domain, array(
'from' => $sender,
'to' => implode(',',$emailAddresses),
'subject' => '%recipient.subject%',
'text' => $messageText,
'recipient-variables' => json_encode($credentials),
'html' => $template
), array(
'inline' => array(
array('path/to/image1.png'),
array('path/to/image2.png'),
array('path/to/image3.png'),
array('path/to/image4.png')
)));
Basically wrapping each image path in an array.
Also what is the contents of $template?
This was actually a recently introduced bug. A new pull request has been submitted to the official Mailgun PHP SDK, for more info see here.
So to answer the question: the code is working fine, as soon as the SDK is updated according to above pull request. For now I edited my local copy of mailgun-php accordingly and it worked fine. Many thanks to Travis Swientek on Mailgun for quick response.

How to update/replace a field in an ElasticSearch document using PHP?

I want to update my Elasticsearch indexed document's field. In my case its the tags field.
This is the code I currently have:
// Index tags in the page document
$es_client->update([
'index' => 'myappname',
'type' => 'page',
'id' => $page_id,
'body' => [
'doc' => [
'tags' => $tagsArray
]
]
]);
So, this would update my document by adding the tags array to it, but it won't remove the old tags.
How can I make sure that the old tags get removed when I add the new tags?
I did look in the documentation, but as we all know, the Elasticsearch docs can be very confusing and all-over-the-place. Hence I am asking here after days of searching.
Any help or advice would be greatly appreciated.
Standard update behavior is to merge array/object fields as explained in the update API documentation .
...objects are merged together, existing scalar fields are overwritten
and new fields are added.
So instead you would use a script to modify the document source directly. You can make it generic and thus cacheable, and pass in params for better performance. Php API documentation
// Index tags in the page document
$es_client->update([
'index' => 'myappname',
'type' => 'page',
'id' => $page_id,
'body' => [
'script' => 'ctx._source.tags=tags',
'params' => ['tags' => $tagsArray]
]
]);

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.

Send model attributes to Yii controller using ajax

I'm currently using the following code to send an Ajax get request to my controller:
echo CHtml::ajaxLink('clickMe', array('ajax'), array('update'=>'#results'));
This works fine, the controller receives the request and updates the view accordingly.
Now, I want to send in this request attributes of the model, i.e. from model->getAttributes();
How should I do this? Create a JSON object of the attributes and send that with the request?
Just pass 'data' attribute and 'type' if needed:
echo CHtml::ajaxLink('clickMe', array('ajax'), array(
'update' => '#results'
'data' => CJSON::encode($model->attributes),
'type' => 'post',
));
This code just replaces #results contents with json. If you need something different, use 'success' instead of 'update' like this:
echo CHtml::ajaxLink('clickMe', array('ajax'), array(
'success' => 'function (response) {
// do everything you need
}',
'data' => CJSON::encode($model->attributes),
'type' => 'post',
));
Take a look at jquery ajax options for more information.

How do i pass the params to the zend_navigation which references the other page

$pages = array(
array(
'label' => 'Search',
'title' => 'Please Search',
'uri' => 'Search.php',
'params' => array('stcode'=>$stcode)
));
now the pages array will be passed to the zend_naviagtion
$container = new Zend_Navigation($pages);
$view->getHelper('navigation')->setContainer($container);
I have a couple of items in the pages array which will help me displaying the menu
when the menu is generated when i click on that search page and
it is taking me to the index.php as the code is written if stcode is not present then
it will redirect to index.php .Now my problem is how do i pass the stcode to that page
Do you mean doing it via the ZF Request Object, like so:
$this->getRequest()->setParam("stcode", $the_value);
Which can then be accessed like so
$this->getRequest()->getParam("stcode");
// You can also pass an optional default value
$this->getRequest()->getParam("stcode", "wibble");
Or like so
// This has no default value and may return an error depending on your configuration
$this->getRequest()->stcode;

Categories