Drupal ajax_command_after function - php

Regarding the ajax_command_after() function in Drupal 7
Currently I have it working and outputting some content.
however it wraps the content in an unlabelled DIV (no class, no id ect).
Is there a way to make it add an ID to this DIV so that I can target the DIV later?
The functions documentation says that its final argument is an array of settings but the only thing it says about the settings is...
"$settings: An optional array of settings that will be used for this command only".
It does not say what settings are available or how to use them.
Can the required ID for the wrapper DIV be inserted here
if yes
What would the syntax be?
This is the relevant documentation
https://api.drupal.org/api/drupal/includes%21ajax.inc/function/ajax_command_after/7
I also could not find an example of this in Drupals examples module.
my code is
function view_file_page(){
$args = func_get_args();
$connection = connect();
$ID = $args[0];
$row = $args[2];
$stream = $connection->getContentStream($ID);
// Create a new AJAX command that replaces the #page text with our own text.
$ajax_commands[] = ajax_command_after('#' . $row, $stream);
$ajax_commands[] = ajax_command_remove('#name-and-slogan');
// $ajax_commands[0]['method'] = 'replaceWith';
// $sam = $ajax_commands[0]['method'];
// Return our commands in JSON.
ajax_deliver(array('#type' => 'ajax', '#commands' => $ajax_commands));
}

Related

Get page number of pdf form field using php

I am currently using SetaPDF to get the form fields located in a document and saving those form field names in a DB. However, I'm trying to get the page number of those form fields. I can't find anything in the Seta documentation that will help with this. Is there another PDF library I can use to accomplish this?
$document = SetaPDF_Core_Document::loadByFilename($file);
$formFiller = new SetaPDF_FormFiller($document);
$fields = $formFiller->getFields();
foreach ($fields->getNames() as $fieldName) {
$field = $fields->get($fieldName);
$is_read_only = 0;
if ($field->isReadOnly()) {
$is_read_only = 1;
}
$is_text = 1;
$field_name = DB::Scrub($fieldName);
$base_field_name = $field->getOriginalQualifiedName();
if (strpos($base_field_name,"#") !== false) {
$arr_field = explode("#", $base_field_name);
$base_field_name = $arr_field[0];
}
if (strpos($base_field_name,"*") !== false) {
$is_text = 0;
$base_field_name = str_replace("*","",$base_field_name);
}
$sql = "INSERT INTO [cust].[PDF_Fields] (file_name,field_name,base_field_name,is_read_only,is_text)
VALUES ('$new_file','$field_name','$base_field_name',$is_read_only,$is_text)";
DB::Query($sql);
}
As Ryan already wrote form fields are not directly related to a page but their representations as Widget Annotations are. This is done by adding the reference to the individual Widget Annotations in the /Annots array of the page. Sadly it is optional to have this the other way round (from the annotation to the page).
You can get the page and its number through some low level methods of the SetaPDF-Core component: First you need an instance of the Widget Annotation of a form field. This can be done with the getAnnotation() method of a form field instance.
Then you can use this instance to search the page via the getPageByAnnotation() method of the main pages instance. To get only the page number you can pass this result to the getPageNumberByPageObject() then.
The written above in code could look like:
$pages = $document->getCatalog()->getPages();
$annotation = $field->getAnnotation();
// Method name is a bit vague and accepts an annotation instance starting with revision > 1371 only
$page = $pages->getPageByAnnotation($annotation->getIndirectObject($document));
// $page = $pages->getPageByAnnotation($annotation); // works with revision > 1371
$pageNumber = $pages->getPageNumberByPageObject($page);

Get hard-coded values from component into plugin in Joomla 3.x

I have a custom component, in fact several. Each will have raw and hard-coded html at the beginning and end of their /view/default.php
I have a system plugin that needs to get this html and in some cases change it to something else, that can be managed in the back end. As a content plugin this works fine on all com_content articles, but it is ignored by components, my understanding is system plugins can do this but i can't get the data into the plugin and return it
example of component text ($text1, $text2 are defined at the top of the document)
JPluginHelper::importPlugin( 'system' );
JPluginHelper::importPlugin('plgSystemMyplugin');
$dispatcher =& JDispatcher::getInstance();
$data = array($text1, $text2); // any number of arguments you want
$data = $dispatcher->trigger('onBeforeRender', $data);
<article>
<div class="spacer" style="height:25px;"></div>
<div class="page_title_text">
<h1>title</h1>
<?php var_dump($data); ?>
</div>
<section>
my plugin:
jimport( 'joomla.plugin.plugin' );
class plgSystemMyplugin extends JPlugin {
function onBeforeRender() {
if (JFactory::getDocument()->getType() != 'html') {
return;
}
else {
$document=JFactory::getDocument();
$document->addCustomTag('<!-- System Plugin has been included (for testing) -->');
$document=JResponse::getBody();
$bob=JResponse::getBody();
$db = &JFactory::getDbo();
$db->setQuery('SELECT 1, 2 FROM #__table');
$results = $db->loadRowList();
$numrows=count($results);
if($numrows >0) {
foreach($results as $regexes) {
$document = str_replace($regexes[0],$regexes[1],$document);
}
return $document;
}
else {
$document = 'error with plugin';
}
JResponse::setBody($document);
return $document;
}
}
}
at the moment $data returns an array with a key 1 and value (string) of "" (blank/empty).
but not the data from the database I am expecting.
in simple terms I have {sometext} in my file and my database and it should return <p>my other text</p>
can you help?
thanks
Ok. Well looking at this deeper there is a couple of issues that jump out. The biggest being that you save getBody into a variable named $bob but then switch everywhere to using $document which is the object form above, not the content.
Also, you had a return $document hanging out in the middle of the code that prevented you from seeing that you were going to set $document as the new body. Probably should be more like below:
$bob=JResponse::getBody();
$db = &JFactory::getDbo();
$db->setQuery('SELECT 1, 2 FROM #__table');
$results = $db->loadRowList();
$numrows=count($results);
if($numrows >0) {
foreach($results as $regexes) {
$bob = str_replace($regexes[0],$regexes[1],$bob);
}
}
else {
$bob = 'error with plugin';
}
JResponse::setBody($bob);
return $document;
}
Original Thoughts:
Two thoughts to get you started. I'm not sure that this will actually fully answer the question, but should get you moving in the right direction.
First, you should not have to trigger the system plugin. They are system plugins, so the system will take care of that for you. If you wanted to use content plugins in your component (which you can definitely do!) then you would have to trigger them like your first set of code. In this case, don't bother with the entire dispatch section.
Second, your plugin looks set up to grab the body from the JDocument correctly, so that should work.
The likely issue is that the entire system plugin is just not being triggered. Make sure that it is installed and everything is named correctly. It has to be at plugins/system/myplugin/myplugin.php based on this name and make sure that the xml file with this also references myplugin as the plugin name. If not, the system won't find the class but likely won't throw an error. It will just skip it. This gives me trouble every time.
To do some checking just to make sure it gets called, I usually throw an echo or var_dump near the top of the file and just inside the function. Confirm that the function is at least getting called first and you should be most of the way to getting this to work.

Joomla 2.5 get component configuration in config.xml ALWAYS return null

Please help me with this
I am creating a component. There is a config.xml in my component
I write my custom JFormFieldUserCheck
in userCheck.php I want to load parameter or field from config.xml
I used
$param = JComponentHelper::getParams('com_my-component');
var_dump($param);
Resul is
object(stdClass)#214 (0) { }
But when I change com_my-component to com_content (Joomla default component).
then var_dump, result is fine.
Thanks in advance
I have added an excerpt from the blog entry:
Plugin parameters from inside a plugin
$param = $this->params->get('paramName', 'defaultValue');
Plugin parameters from outside a plugin
$plugin = &JPluginHelper::getPlugin('exampleType', 'example');
$pluginParams = new JParameter($plugin->params);
$param = $pluginParams->get('paramName', 'defaultValue');
Module parameters from inside a module
$param = $params->get('paramName', 'defaultValue');
Module parameters from outside a module
$module = &JModuleHelper::getModule('example');
$moduleParams = new JParameter($module->params);
$param = $moduleParams->get('paramName', 'defaultValue');
Component parameters from inside a component
$componentParams = &JComponentHelper::getParams('com_example');
$param = $componentParams->get('paramName', 'defaultValue');
Component parameters from outside a component
$componentParams = &JComponentHelper::getParams('com_example');
$param = $componentParams->get('paramName', 'defaultValue');
Template parameters from inside a template
$param = $this->params->get('paramName');
Template parameters from outside a template
jimport('joomla.filesystem.file');
$mainframe = &JFactory::getApplication();
$params = $mainframe->getParams(JFile::read(JURI::root() .'/templates/template_name/params.ini'));
$param = $params->get('paramName', 'defaultValue');
Template parameters from an included file outside the Joomla framework
// Get params.ini relative to the current file location (use your own relative path here)
$paramsFile = dirname(__FILE__) . '/../../params.ini';
// Only continue if the file exists
if(file_exists($paramsFile)) {
// Get contents from params.ini file
$iniString = file_get_contents($paramsFile);
// Escape double quotes in values and then double-quote all values (because Joomla doesn't do that for us..)
$iniQuoted = preg_replace('/=(.*)\\n/', "=\"$1\"\n", addcslashes($iniString, '"'));
// Parse the ini string to an associative array
$iniParsed = parse_ini_string($iniQuoted);
} else {
$iniParsed = '';
}
// Set params to obtained values or empty array
$params = (!empty($iniParsed)) ? $iniParsed : array();
// Get param value from array
$param = $params['paramName'];
Clearly, Joomla isn't understanding your component. Insure that it's properly installed in Joomla, and that the xml file for the component is accurate and formed properly. If Joomla does find your component, or is unable to load the XML, then the parameters cannot be made available to your PHP. These steps are done with the valid entries in the database, and the XML, both of which are typically done with the component installation, but can be done manually as long as you get them all correct.
Had the same problem. The result was empty until i went to the configuration of my component and saved it (though there were default values printed in the textfields).

How to get results from "Elastica_ResultSet" object

I am using the "elastica" php client for ElasticSearch.
I'm a bit new to OO-programming, especially in php.
However, I have managed to search my elasticsearch server using the elastica php client and store the response in an "Elastica_ResultSet" object. I have had no luck accessing the contents of that object whatsoever.
I would like to be able to list the total number of results, find an elasticsearch record id of a result and get the full content of an elasticsearch record for that result.
The Elastica class reference can be found here http://ruflin.github.com/Elastica/api/index.html , although I don't know what to do with it.
Here is the php code I have been using to get this far:
<?php
function __autoload_elastica ($class) {
$path = str_replace('_', '/', $class);
if (file_exists('extentions/' . $path . '.php')) {
require_once('extentions/' . $path . '.php');
//echo "$path EXISTS!!!";
}
}
spl_autoload_register('__autoload_elastica');
// New ES Client
$client = new Elastica_Client();
// Set Index
$index = $client->getIndex('test1');
// Set Document Type
$type = $index->getType('user');
// Perform Search
$resultSet = $index->search('halo');
?>
So basicaly you can use var_export to output your resultset
But in general the elastica search returns a Elastica_ResultSet object which has several attributes you can use like count, totalHits facets and so on.
and also holds an array of Elastica_Result objects these can be accessed either by calling the Elastica_ResultSet getResults() method or by using the current() and next() methods or by simply using the php foreach function
The Elastica_Result the data of the results and also has several methods you can use.
getId(), getVersion(), getData() and so on.
// Set Document Type
$type = $index->getType('user');
// Perform Search
$resultSet = $index->search('halo');
// Get IDs
$resultIDs = array();
foreach($resultSet as $result){
$resultIDs[] = $result->getId();
}
I would like to let you know something that was a bit hard for me to get.
The query and the sorting of results
// Set the query terms for your search
$queryTerm = new Elastica_Query_Terms();
$queryTerm->setTerms('user', array("test", "test1"));
// Create the sorting array
$sort = array("user" => array("order" => "desc"));
// Create the query
$query = Elastica_Query::create($queryTerm);
// Set the sorting to the query
$query->setSort($sort);
// Perform the search
$resultSet = $index->search($query);
Hope this helps
After a couple of months OO practise, it seemed performing a simple var_dump($resultSet) would have provided me with the structure and contents of the returned object... can't believe that nobody made any suggestions for such a basic question ;)

PHP Wordpress gallery not finding the XMLl file

The code is below - it uses a wordpress shortcode which is [my_hmg=widget.xml] but if you try change the xml file like this [my_hmg=example_gallery.xml] it just always reverts to the default widget.xml
The problem is in the function my_hmg_filter_Callback in particular these 2 lines;
#$my_hmg_file = #$output['filename'];
if($my_hmg_file==""){$my_hmg_file = "widget.xml";}
For some reason it always thinks the file name is blank so always reverts to widget.xml.
The files can be downloaded from here - http://www.gopiplus.com/work/2010/07/18/horizontal-motion-gallery/
function my_hmg_show_filter($content){
return preg_replace_callback('/\[my_hmg=(.*?)\]/sim','my_hmg_filter_Callback',$content);
}
function my_hmg_filter_Callback($matches)
{
$my_hmg_package = "";
$var = $matches[1];
parse_str($var, $output);
#$my_hmg_file = #$output['filename'];
if($my_hmg_file==""){$my_hmg_file = "widget.xml";
}
Firstly change the short code to [my_hmg file='file.xml']
Then if you have a quick read of Wordpress's short code API you'll see that the first argument in the callback function are the attributes of the short code.
This way you can the reference the attribute 'file' in the array and get the proper url.

Categories