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

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();

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

How to update module parameters in J2.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

Regexp to find youtube url, strip off parameters and return clean video url?

imagine this url:
http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM
what is the cleanest and best regexp to do the following:
1.) i want to strip off every thing after the video URL. so that only http://www.youtube.com/watch?v=6n8PGnc_cV4 remains.
2.) i want to convert this url into http://www.youtube.com/v/6n8PGnc_cV4
Since i'm not much of a regexp-ert i need your help:
$content = preg_replace('http://.*?\?v=[^&]*', '', $content);
return $content;
edit: check this out! I want to create a really simple WordPress plugin that just recognizes every normal youtube URL in my $content and replaces it with the embed code:
<?php
function videoplayer($content) {
$embedcode = '<object class="video" width="308" height="100"><embed src="' . . '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="308" height="100" wmode="opaque"></embed></object>';
//filter normal youtube url like http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM
//convert it to http://www.youtube.com/v/6n8PGnc_cV4
//use embedcode and pass along the new youtube url
$content = preg_replace('', '', $content);
//return embedcode
return $content;
}
add_filter('the_content', 'videoplayer');
?>
I use this search criteria in my script:
/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/
You could just split it on the first ampersand.
$content = explode('&', $content);
$content = $content[0];
Edit: Simplest regexp: /http:\/\/www\.youtube\.com\/watch\?v=.*/
Youtube links are all the same. To get the video id from them, first you slice off the extra parameters from the end and then slice off everything but the last 11 characters. See it in action:
$url = "http://www.youtube.com/watch?v=1rnfE4eo1bY&feature=...";
$url = $url.left(42); // "http://www.youtube.com/watch?v=1rnfE4eo1bY"
$url = $url.right(11); // "1rnfE4eo1bY"
$result = "http://www.youtube.com/v/" + $url; // "http://www.youtube.com/v/1rnfE4eo1bY"
You can uniformize all your youtube links (by removing useless parameters) with a Greasemonkey script: http://userscripts.org/scripts/show/86758. Greasemonkey scripts are natively supported as addons in Google Chrome.
And as a bonus, here is a one (okay, actually two) liner:
$url = "http://www.youtube.com/watch?v=1rnfE4eo1bY&feature=...";
$result = "http://www.youtube.com/v/" + $url.left(42).right(11);
--3ICE
$url = "http://www.youtube.com/v/6n8PGnc_cV4";
$start = strpos($url,"v=");
echo 'http://www.youtube.com/v/'.substr($url,$start+2);

How to create threaded list in CakePHP?

I create table: id, name, thread_id
The mainly thread has thread_id = 0, but their children has theard_id = id of parent, and how is the best and simplest solution to create list with children, and looks like:
Category 1
Product 1
Product 2
Category 2
Product 3
etc...
Maybe You have better solution for such a list?
Sorry, for my english:)
I suspect that the easiest way may be to use Cake's own TreeBehavior. More on that at http://book.cakephp.org/view/91/Tree. I've never used it personally, but have heard good things. It should provide all of the tools (and instruction) you need.
One of easiest and efficient is to use Tree behavior as proposed by kicaj-pl.
But I suggest you to consider MultiTree Behavior. It's also using nested tree database model but allows you to create many trees with different root_id and independent left and right values (so update of one tree doesn't update any other).
Ah! I found this sentence: When You use find('threaded') you have to field 'parent_id' for creating structure like tree...
All works fine!
Thanks for replies, bye!
Try the MPPT logic for It. For that you need 3 fields in your database table viz parent_id , lft , rght. And to implement it using CakePHP, CakePHP already provided the function for it for that please refer http://book.cakephp.org/view/228/Basic-Usage :)
1.First, your model must use "Tree" behaviour in model file (Modelname.php - in my case Post.php)
public $actsAs = array('Tree');
2.Next you need to retrieve threaded results and pass them to a view (ModelnamesController.php - in my case PostsController.php).
$posts = $this->Post->find('threaded');
$this->set('posts', $posts);
3.Finally, here's a template you can use, that renders infinitely threaded list for the above results
<div id="posts_navi">
<? function renderPosts($postsArray, $tmpModel){
//set return for the first time
if(!isset($return)){ $return = ""; }
$return .= '<ul>';
//create list
foreach ($postsArray as $post){
$return .= '<li>';
if($post['Post']['content'] != null){
$return .= $tmpModel->link($post['Post']['title'], array('action' => 'view', $post['Post']['id']),array('escape'=>false));
}else{
$return .= $post['Post']['title'];
}
//if post has children, go deeper
if(!empty($post['children'])){
$return .= renderPosts($post['children'], $tmpModel);
}
$return .= '</li>';
}
$return .= '</ul>';
return $return;
} ?>
<? $tmpModel = $this->Html; // we have to pass html helper inside, I am not sure it this is best way but it works
echo renderPosts($posts, $tmpModel); //finally, we render the $result returned. ?>
</div>