How to update module parameters in J2.5? - joomla2.5

i am trying to update the value of parameter . I have a hidden field in xml file, i need to update its value , value is dynamic.
I got the parameters using
jimport('joomla.application.module.helper');
$module = JModuleHelper::getModule('mod_name');
I want to avoid the use database query method .
Is there joomla predefined function to achieve this task?
thanks in advance
-Neil

Unfortunately Joomla doesn't have an in-built API feature to set a parameter without using a database query. Try the following:
<?php
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule( 'mod_name'); // change module name here
$params = new JRegistry();
$params->loadString($module->params);
$params->set('param_name', 'value'); // change parameter name and the value
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->update('#__extensions AS a')
->set('a.params = ' . $db->quote((string)$params))
->where('a.element = "mod_name"'); //change module name here
$db->setQuery($query);
$db->query();
?>
I have tested this code so let me know if it works

Related

How to get Joomla template style name?

I have a joomla template that has two styles. The name of the template is default, and the styles are cats and arts. is there a way to return the name of the current style in use.
the code below only return the name of the template
$template = $app->getTemplate();
if I do an echo $template; I get default. But what I would like to get, is whether I am using the style cats or the style arts
Thank you
The template object doesn't contain the name of the a templates style variations (as it's only really used for human administrators as a mnemonic).
The only way to tell which "style" is being used is to look at the id value of the template… this value will correspond to the one you see in the ID column of the "Template Manager - Styles" view.
// Get the Joomla Application
$app = JFactory::getApplication();
// Get the template
$template = $app->getTemplate(true);
// Echo the ID
echo $template->id;
If you really need the "name" I think you're probably making a design mistake, having said that you could try loading the style model for the $template->id and retrieving it that way. e.g. something like this (warning typed directly into SO, NOT TESTED!)
// Initialise some vars
$name = 'Style';
$prefix = 'TemplatesModel';
$config = array();
// Get the model
$templateStyleModel = JModelLegacy::getInstance($name, $prefix, $config);
// Load the specific style instance.
$templateStyleModel->load($template->id);
// Echo out the style name
echo $templateStyleModel->title;
$params = $app->getTemplate(true)->params;
Use $params->get() to get the particular style's params.
The solution look are looking for is:
$app = Factory::getApplication();
$template = $app->getTemplate(true);
$styleModel = new Joomla\Component\Templates\Administrator\Model\StyleModel();
$style = $styleModel->getItem($template->id);
$style->alias = Joomla\CMS\Filter\OutputFilter::stringURLSafe($style->title);

Getting the Tag name of a element crawled with DomCrawler in PHP

I am crawling my html view with PHPUnit, using DomCrawler
$element = $crawler->filter("#myElement");
Once I have the element, how could I know the kind of tag that it is? (<input>, <select>, ...)
I know I could do this:
$element = $crawler->filter("input#myElement");
but I need to extract the name of the tag, and store it in a variable
As far as I can tell it, this should work:
$element = $crawler->filter("#myElement");
$name = $element->getNode(0)->tagName;
The Crawler::getNode(index) returns a DOMElement that has the tagName read only field.
Update: nowadays it is
$element = $crawler->filter("#myElement");
$name = $element->nodeName();
... which is actually a wrapper for
$crawler->getNode(0)->nodeName

Doctrine paginator

I'm running into a problem with the Doctrine Paginator.
In my repository I have a function to retrieve a specific dataset.
I use the querybuilder for this:
{myEntityRepository}->createQueryBuilder($alias)
In order to select only specific fields I use the following:
if (count($selectFields) > 0) {
$qb->resetDQLPart('select');
foreach ($selectFields as $selectField) {
$qb->addSelect($alias . '.' . $selectField);
}
}
This works fine when I retrieve the whole set like this:
$query = $qb->getQuery();
$data = $query->getResult(AbstractQuery::HYDRATE_ARRAY);
But it fails when I use the paginator:
$paginator = new Paginator($qb, $fetchJoinCollection = false);
$total = $paginator->count(),
$data = $paginator->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY)
I get the error:
Not all identifier properties can be found in the ResultSetMapping:
relationID\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Exec\SingleSelectExecutor.php(38)
Question: Why does the paginator fail when I select only specific fields?
Am I overlooking something? Or am I doing it wrong all together?
i am using this solution.
add use statements
use Zend\Paginator\Paginator;
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter;
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
in your action
$viewModel = new ViewModel();
$entityManager = $this->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
$queryBuilder = $entityManager
->createQueryBuilder();
$queryBuilder->add('select', new Expr\Select(array('t.id', 't.name')));
$queryBuilder->add('from', 'Application\Entity\Table t');
$adapter = new DoctrineAdapter(
new ORMPaginator(
$queryBuilder
)
);
$paginator = new Paginator($adapter);
$paginator->setDefaultItemCountPerPage(20);
$page = (int)$this->params()->fromQuery('page');
if($page) $paginator->setCurrentPageNumber($page);
$viewModel->results = $paginator;
return $viewModel;
Doctrine is trying to hydrate a relationship outlined by your YAML file, using a field that doesn't exist because you've excluded it from your SELECT statement. Take a look at your mapping file to figure out what you need to add back in.
I would think that it's only complaining with the Paginator because the field is not being accessed (and therefore not being lazy-loaded) when you don't use the Paginator.
As an aside (and with zero understanding of your stack, so YMMV) I would avoid making a habit of SELECTing reduced result sets, as you'll find yourself running into odd issues like this all the time. If you do need extra performance, you'd be better off putting a good old caching layer in place...

Opencart how to add everything from the language file in php loop

It there a way to read everything from within a language with Opencart?
At the moment I have to:
Controller
$this->load->language('help');
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['tab1'] = $this->language->get('tab1');
Language File
<?php
// Heading
$_['heading_title'] = 'Help';
$_['tab1'] = 'Account';
?>
The easiest thing to do is to use array merge at the top of your controller
$this->data = array_merge($this->data, $this->language->load('language/file'));
or simply
$this->data += $this->language->load('language/file');
Edit
For 2.x, use
$data = array_merge($this->data, $this->language->load('language/file'));
3.x does this automatically
In system/library/language.php is a
function to get everything called all().
This is how to get a single item:
$var = $this->language->get('heading_title');
This returns an array with all language entries:
$var = $this->language->all();

Show Joomla module only in specific articles?

Is it possible to show modules in Joomla only in a specific article (not per menu item), but in standard module position?
For example somehow get the current article id in a template and insert the modules with according id suffix in the name?
I would advise you not to hardcode things like this in the template. My question is, why don't you want to use a menu item? You can create a hidden menu item for that article and use it, then assign the module to that menu item.
If you still want to do it without using a menu item, a possible workaround would be to use something like "mod_php" (some module that allows you to use php code) and do something more or less like this:
Create the module and assign it to a position that is not used anywhere (you can type whatever you want in the module position)
In your php module, put this code:
$option = JRequest::getVar( 'option', '' );
$view = JRequest::getVar( 'view', '' );
$id = JRequest::getInt( 'id', 0 );
if ( $option == "com_content" && $view == "article" && $id == YOUR_ARTICLE_ID ) {
$module = JModuleHelper::getModule('your_module_type', 'module_title');
if ( ! empty( $module ) ) {
$attribs = array( 'style' => 'xhtml' );
echo JModuleHelper::renderModule( $module, $attribs );
}
}
I'm sorry if the code snippet is not showing properly, but I hope you can read it ok. Just one thing, when you fill in the part saying 'your_module_type', don't include the "mod_" part of the name. For example, if you want to output a module of type "mod_article_list", you should write "article_list" in "your_module_type".
I hope it helps!