I use symfony 1.4 and I use Zend Lucene search like in Jobbet And I need to make Search Results Highlighting, I read this , but I do not understend how it make in my case with symfony(
$ highlightedHTML = $ query-> highlightMatches ($sourceHTML);
What is $sourceHTML? And is it all makes by only one row?
upd:
$ highlightedHTML = $ query-> highlightMatches ($sourceHTML);
It works in my model, but how it implement in my view?
I do not now , if it is right , but it is work :)
Just in view:
$query = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
$highlightedHTML = $query->highlightMatches($sourceHTML);
In my case for example:
echo $query->highlightMatches($ad->getCompany())
You need to store this highlighted HTML in your model. Or make a function that is accessible from the view. For example:
<?php
class Model {
private $content;
public function getContent(){
return $this->content;
}
public function getContentHighlighted(){
// Search term, usually in $_GET or $_POST
$term = $_GET['searchterm'];
// Parse query
$query = Zend_Search_Lucene_Search_QueryParser::parse($term);
// Return highlighted
return $query->highlightMatches($this->getContent());
}
}
?>
In your view (like in this case: Twig) you use:
<h1>The content</h1>
{{model.getContentHighlighted}}
Related
I'm having an issue displaying information returned from a custom class defined within a plugin's files, when using a shortcode. I'll write up some mock files that showcase my issue.
/wp-content/plugins/my-plugin/classes/my_class.php
<?php
class People {
public $api_url = "https://www.external-service.com/api";
private $api_key;
function __construct($key = null) {
if $(key) {
$this->api_key = $key;
}
function get_response() {
$path = $this->api_url . "?my_api_token=" . $this->api_key;
}
}
?>
/wp-content/plugins/my-plugin/my-plugin.php
<?php
/**
* all of the wordpress plugin comments
* ...
*/
require "myplg_options.php";
require "myplg_shortcodes.php";
The options page and menu is generated from myplg_options; it is functioning correctly (including using get_option to retrieve the saved option (in this case, the api key).
/wp-content/plugins/my-plugin/myplg_shortcodes.php
<?php
require "classes/my_class.php";
$options = get_option('myplg_settings');
$myplg = new People($options['myplg_api_key']);
$response = $myplg->get_response();
function myplg_list_result(){
echo "the shortcode is working!";
var_dump($options, $myplg, $respnose);
}
add_shortcode('myplg_list', 'myplg_list_result');
?>
Testing externally from wordpress, the class works and everything is fine and dandy. The plugin's option page sets and retains the single option perfectly; the shortcode actually registers and is usable from within a WordPress page/portfolio/etc.
The issue I'm having is that using var_dump, all three of those variables are dumped as NULL.
After doing some homework, I was able to determine that moving the three variable declarations inside the shortcode makes it work. It would seem to me, however, that doing that is not the best workflow, as I'd need to re-grab the option, instantiate a new class, and call the class' function for every shortcode.
Is there a way around this?
As mentioned in the comment it's because variables are function scoped. You may be better off using a Closure.
<?php
require "classes/my_class.php";
$options = get_option('myplg_settings');
$myplg = new People($options['myplg_api_key']);
$response = $myplg->get_response();
add_shortcode('myplg_list', function() use ($options, $response, $myplg) {
echo "the shortcode is working!";
var_dump($options, $myplg, $respnose);
});
Im trying to make an extension with Kickstarter that overrides the normal rendering of the page, and renders a PDF file. For this im using FPDF. But im not sure how to do it. I tried doing this, but it didnt work:
<?php
// require_once(PATH_tslib . 'class.tslib_pibase.php');
class tx_ishurkunde_pi1 extends tslib_pibase {
public $prefixId = 'tx_ishurkunde_pi1';
public $scriptRelPath = 'pi1/class.tx_ishurkunde_pi1.php';
public $extKey = 'ish_urkunde';
public $pi_checkCHash = TRUE;
public function main($content, array $conf) {
if (!t3lib_extMgm::isLoaded('fpdf')) return "Error!";
$pdf = new FPDF();
$pdf->AddPage();
$content = $pdf->Output('', 'S');
return $content;
}
}
?>
It still keeps rendering the normal web template. What am I missing?
FYI, Im not trying to render the HTML as PDF. Im trying to generate a PDF from scratch, using the URL parameters are text variables.
As far as I understood, your aim is to render a PDF instead of page elements.
Your current approach will not work since you are inserting a plugin onto the page. The plugin's return value is then given back to the TYPO3 content parser, and if the page has finished parsing, it is displayed. There is no part in it where you can throw over the whole page rendering; At least it is not intended to do, and you shouldn't to (albeit there are extensions that do this).
The eID approach would be to either create an eID script (have a look at dd_googlesitemap) which is called via GET param and renders only what you need. There you basically can do everything you want to.
In your extension's ext_localconf.php you register the eID script, like this:
$GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$_EXTKEY] = "EXT:{$_EXTKEY}/path/to/my/EIDHandler.php";
An Example eID handler structure:
class Tx_MyExt_EIDHandler
{
public function main()
{
// Your code here
}
}
$output = t3lib_div::makeInstance('Tx_MyExt_EIDHandler');
$output->main();
To call your eID script in the frontend, you append the appropriate GET params, like http://example.com/index.php?eID=tx_myext. This is the array key you defined in your ext_localconf.php (in my example, it is $_EXTKEY, but it can basically be any string).
The plugin/typoscript approach would be like e.g. TemplaVoila does it: You create a PAGE type and call a user_func which does your things. This would be the fastest approach because you already have a plugin. Important is that you render your own page type with only your plugin in it.
Example TypoScript:
specialPage = PAGE
specialPage {
type = 2
10 = USER
10.userFunc = tx_myextension_pi1->myFunc
}
After that, you can call your new page with http://example.com/index.php?type=2. However, headers etc are still rendered and you may need to remove them.
I'm trying to get started with Zend Framework , followed the quickstart project and am trying to start a new module of my own.
Am trying to implement view helpers and I keep getting the following message:
Message: Method formDate does not exist
Last entry at the stack trace:
0 D:\work\quickstart_zend\application\views\scripts\users\register.phtml(38): Zend_Form_Element->__call('formDate', Array)
I have the following file structure:
quickstart_zend
+ application
+ configs
+ controllers
[...]
+ views
+ helpers
+ scripts
[...]
+ library
+ Application
+ Form
+ Element
Date.php
+ View
+ Helper
FormDate.php
+ public
I have added in my public/Bootstrap.php this method:
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH.'/../library/Application/View/Helper', 'Application_View_Helper');
Zend_Controller_Action_HelperBroker::addPrefix('Application_View_Helper');
}
I have also added in my application.ini:
autoloaderNamespaces[] = "Application"
resources.view.helperPath.Application_View_Helper = APPLICATION_PATH "/../library/Application/View/Helper/"
And i have seen a version and also have tried with resources.view.helperPath.Application_View_Helper_, nothing seems to get it to work.
Of course, i have a Users.php form where i create a 'date' element:
// Add a dateOfBirth element
$element = new Application_Form_Element_Date('dateOfBirth');
$this->addElement($element);
Of course, i have a Users.php form where i create a 'date' element:
// Add a dateOfBirth element
$element = new Application_Form_Element_Date('dateOfBirth');
$this->addElement($element);
And in my view script, where the errors shows up:
<? echo $form->dateOfBirth->formDate() ?>
What am i missing to get it to work? :-( i've spent a day so far looking for solutions
You are receiving this error because there is no such method in Zend_Form_Element. I think you are trying to use your view helper to display in some way this form element, but if this is the case it is better to use form decorators. You can use the standard decorators or you can create a custom one. Check the documentation for more info - http://framework.zend.com/manual/en/zend.form.decorators.html
to properly use your view helper on that data you would use it like:
In your view (.phtml)
//a view helper should act on a piece of data and return something
//so I assume your formDate() helper takes a date value and reformats it.
<?php echo $this->formDate($this->form->dateOfBirth) ?>
assuming you assigned your form to the view in your controller using the standard:
$this->view->form = $form;
Is it possible to remove the 'id' attribute that Zend Framework adds to every Form Element by default?
I've looked at the documentation but don't seem to be able to find an answer to this rather straight forward question.
Possible Solution
Is there any cleaner way to do it other than setOption?
$submit = new Zend_Form_Element_Submit('submit');
$submit->setRequired(FALSE)
->setIgnore(TRUE)
->setDecorators($this->elementDecorators)
->setOptions(array('id' => ''));
A solution would be to override Zend_View_Helper_Form with your own View Helper.
But sincerely, don't take too much attention to this id attribute in your form, you will sooner or later need this id (if you're using Javascript for example) and the performance gain (to render the page) is too tiny to be taken into consideration. It will even be a performance loss since you're going to override a Helper.
If your purpose is different and you want to do that anyway, you need to write you own View Helper as follow:
class My_View_Helper_Form extends Zend_View_Helper_FormElement
{
public function form($name, $attribs = null, $content = false)
{
$info = $this->_getInfo($name, $content, $attribs);
extract($info);
$xhtml = '<form'
. $this->_htmlAttribs($attribs)
. '>';
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
return $xhtml;
}
}
Finally, you simply need to overload the default view helper using a plugin loader. Read the manual for more information about plugin loader.
Is there some method that accepts inserting custom html without having to actually add form controls, even if they're hidden and making my html a decorator?
I'm looking for something like:
$this->addCustomElement( array(
'div',
'body' => '<p>inner text</p>'
) );
I need something short and quick, I don't want to create a new class or something overkill.
Well it's really as simple as this:
$note = new Zend_Form_Element('note');
$note->helper = 'formNote';
$note->setValue('<b>hi</b>');
$form->addElement($note);
But the problem is that when you submit the form, the form calls $note->isValid(), which overrides the value, so if there are errors with the form, the next time you display it, the custom HTML won't be shown. There are two easy ways to fix this, the first is to override isValid() in your Form class like this:
public function isValid($data)
{
$note = $this->note->getValue();
$valid = parent::isValid($data);
$this->note->setValue($note);
return $valid;
}
But personally I find this kinda hackish way, and prefer the second option. That is to write a very simple class (this should really be part of Zend itself, I have no idea why it isn't, since it includes a formNote view helper, but no element that uses it):
class My_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
public function isValid($value, $context = null) { return true; }
}
Then you just have to do:
$note = new My_Form_Element_Note('note');
$note->setValue('<b>hi</b>');
$form->addElement($note);
And everything will just work.
Other options include doing some black magic with decorators, but I really recommend you to not go down that path.
Also note the AnyMarkup Decorator.