How do I extend default joomla class / function? - joomla

I want to extend the submit file function in Joomla in an extension I'm making to custom handle files, how do I do that? Can I get some code samples please.
I've been told to extend them in the Model, but whenever I try to extend anything I get an error, even if I have an empty function that just says "..extends xxxx{}.

see below url and read :-
http://docs.joomla.org/How_Joomla_pieces_work_together
Or
class <name>Controller extends JController
{
}

Related

Magento - Block is not rendered

I'm developing my second Magento module, which should retrieve some data and render a block containing it. Such block would then be used by other pages.
At the moment, I have almost the whole module in place, but, for some reason, the block is not rendered when I call the controller method. I put some debug information, and I could see that the block's __construct() method is called correctly, but then the template doesn't seem to be loaded and the returned page is blank.
Here's the code for the block, which I copied from another module and modified:
class Company_CustomerData_Block_CustomerSummary extends Mage_Core_Block_Template {
const _TEMPLATE = 'customerdata/customersummary.phtml';
public function __construct() {
// This method is called correctly
parent::_construct();
$this->setTemplate(self::_TEMPLATE);
}
}
The file customersummary.phtml is in app/design/frontend/base/default/template/customerdata, which should be the correct place (or, at least, I think it is). Its content is the following:
It works!
Just some plain text. No tags, no code, nothing. I don't mind that it's a static text, it will be populated with data once complete.
In case of need, here's the code for the Controller (I removed the parts where the data is retrieved, as they don't make a difference):
public function dashboardAction() {
// Customer Data to render in the block
$CustomerData = array(); // Data is retrieved elsewhere
$this->getResponse()->setBody(
$this->getLayout()->createBlock('customerdata/customersummary')
->toHtml()
);
}
What could I be doing wrong? I'm afraid I made some stupid mistake again, but I really can't see it.
Finally two more questions:
How do I pass to the template the data I retrieve in the Controller? Specifically, variable $CustomerData.
Once the block renders, how do I render its content from within a page? The block should return a with some stuff in it, I'd like to render it inside the Customer Dashboard, just below the information that is already there.
Thanks in advance for the help.
Most classes on Magneto derive from Varien_Object. Varien_Object (and it's descendents) claim PHP's constructor (__construct) for themselves, and provide an _construct callback which you can use for whatever you like. What this means is that if you override the native PHP constructor (__construct) you need to remember to do a few things:
Accept the same number of parameters as the base class, and...
Call the parent constructor (parent::__construct) with the parameters your constructor was supplied.
Or, alternatively, use the _construct callback supplied by Varien_Object and you're done. There's no need to remember to call parent::_construct if you're using the Magento callback.
So to fix your code snippet above, you can either change...
public function __construct() {
... to ...
public function _construct() {
This will switch you over the using the Magento callback. Or you can change...
parent::_construct();
.. to ...
parent::__construct();
... to call the parent class' constructor. Remembering to add in the parameters that the parent class supplies.
Alan Storm write a great article about Magento's Block Lifecycle and call back methods recently, which might be of assistance.
To your other questions:
Passing information from the controller to a block is generally done via Magento's registry.
Rendering your template as part of a page requires that you create layout instructions (written in XML) which load your block and place it inside a parent block.
Although Jim's Answer is correct, I'm adding a second part because I found out what else was wrong: symbolic links. As I usually do when I develop plugins for a framework, I use symlinks to avoid copying the files over and over again. However, Magento uses function RealPath(), which resolves the symlink to its full path. As a result, the Template file to be loaded resides in a path outside Magento installation directory, and it can't be loaded for security reasons. This makes perfectly sense, pity it's just not very visible.
To fix the issue, I enabled Allow Symbolic Links in configuration on my Development PC, and now the Template is loaded and rendered correctly.
I think that Magento could do with a Log Viewer in the Admin interface. If there isn't a module that does it already, perhaps I should create one myself.
Thanks everybody for the help.

CodeIgniter - where to put functions / classes?

Am having problems understanding where classes should be kept in CI. I am building an application that describes / markets mobile phones.
I would like for all of my functions (i.e. getphone, getdetails etc.) to reside in one class called Mobile - I understand that this file should be called Mobile.php and reside in the controllers folder.
Can I then have multiple functions inside Mobile.php? E.g.
public function getphone() {
xxx
xx
xx
}
public function getdetails() {
xxx
xx
xx
}
Or do I need to put each function in its own class?
I'd really appreciate looking at some sample code that works. I've been going through the documentation and google for a few hours, and tried all sorts of variations in the URL to find a test class, but without much luck! I've even messed around with the routes and .htaccess...
All I am trying to achieve is the following:
http:///model/HTC-Desire/ to be re-routed to a function that accepts HTC-Desire as a parameter (as I need it for a DB lookup). The default controller works fine, but can't get anything to work thereafter.
Any ideas?
Thanks
Actually it works like this:
Controllers and Models go to their perspective folders as you know it
If you want to create functions that are not methods of an object, you must create a helper file. More info here :
http://codeigniter.com/user_guide/general/helpers.html
Now if you want to create your own datatypes (classes that don't extend Models and Controllers), you add them to the library folder. So if let's say you want to create a class "Car" you create this file:
class Car{
function __construct(){}
}
and save it in the libraries folder as car.php
To create an instance of the Car class you must do the following:
$this->load->library('car');
$my_car = new Car();
More information on libraries here:
http://codeigniter.com/user_guide/general/creating_libraries.html
Yes, you can have as many functions in a controller class as you'd like. They are accessible via the url /class/function.
You can catch parameters in the class functions, though it's not advisable.
class Mobile extends CI_Controller{
public function getPhone($phoneModel=''){
echo $phoneModel;
//echo $this->input->post('phoneModel');
}
}
http://site.com/mobile/getPhone/HTC-Rad theoretically would echo out "HTC-Rad". HOWEVER, special characters are not welcome in URL's in CI by default, so in this example you may be met with a 'Disallowed URI characters" error instead. You'd be better off passing the phone model (or any other parameters) via $_POST to the controller.
Classes can exist both as Controllers and Models, as CodeIgniter implements the MVC pattern. I recommend reading more about that to understand how your classes/functions/etc. can best be organized.
Off the top of my head, Pyro CMS is an application built with CodeIgniter and the source code is freely available. I'm sure there are others.
I think it's best you handle it from one perspective, that is; create a utility class with all your functions in it.
The answer to the question of where to put/place the class file is the "libraries" folder.
This is clearly stated in the documentation. Place your class in the libraries folder.
When we use the term “Libraries” we are normally referring to the
classes that are located in the libraries directory and described in
the Class Reference of this user guide.
You can read more on creating and using libraries Creating Libraries — CodeIgniter 3.1.10 documentation
After placing the newly created class in the libraries folder, to use just simply load the library within your controller as shown below:
$this->load->library('yourphpclassname');
If you wish to receive several arguments within you constructor you have to modify it to receive an argument which would be an array and you loading/initialization would then be slightly different as shown below:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('yourphpclassname', $params);
Then, to access any of the functions within the class simply do that as shown below:
$this->yourphpclassname->some_method();
I hope this answers your question if you have further question do leave a comment and I would do well to respond to them.

Magento - Review helper

I'm trying to instantiate the review helper class but failing..
I need to access getReviewsSummaryHtml() of class Mage_Review_Block_Product_View (app/code/code/Mage/Review/Block/Product/View.php).
I've tried this:
Mage::helper('review/product')->getReviewsSummaryHtml($_product, false, true);
But I receive Fatal error: Class 'Mage_Review_Helper_Product' not found.
What am I doing wrong?
(p.s. I can't use $this->getReviewsSummaryHtml as $this is out of scope.)
Thanks
The method getReviewsSummaryHtml() is defined in Mage_Review_Block_Product_View. You can instantiate it anywhere using Mage::app()->getLayout()->createBlock('review/product_view',$product);. In order for this to work, though, you'll need to also have a block instance named product_review_list.count, which is normally defined in review.xml, is of type core/template, and uses the review/product/view/count.phtml template.
You should simply do :
Mage::helper('review')
in order to get helper class which is named as Data.php
The function getReviewsSummaryHtml() resides in a block and you should only be able to call that function (ideally) from you template.
If you have moved that function to helper than you can call it like :
Mage::helper('review')->getReviewsSummaryHtml();
and you should do all these changes in your local.

Magento - create a helper class

I'm having trouble figuring out how to create a helper class with a function in it that's available to *.phtml files.
Can someone describe step by step how I can make the function prtHelloWorld() available to all my *.phtml files?
it's rather simple and you have to call your helper from template like this:
Mage::helper('yourmodule/yourclassfile')->prtHelloWorld();
Default helper class is Data and this defaults to Yourmodule/Helper/Data.php
Mage::helper('yourmodule')->prtHelloWorld();
To add to Anton S's, if you want to be able to access the helper's function using $this->prtHelloWorld() in the phtml instead, add it to your Block like this:
public function prtHelloWorld() {
return Mage::helper(whatever)->prtHelloWorld();
}

How do I access the static methods of a custom class in Kohana?

I have a user class with static methods getById and getByUsername
I have the class in the application/libraries folder
How do I call the classes from a controller?
Theory 1:
$this->user = new User();
$this->user::getById;
Theory 2:
$user = new User();
$user::getById;
or is there a clean way of doing it much like how Kohana helpers do it; much like:
text::random();
here's what I am trying to accompplish:
I want to call a static mehthod in the user library from my controller
In PHP you usually include the file (User.php) and the static methods are ready
User:getById
but then how would I do the same thing in an MVC framework?
shall I do an include too?
Like include ('User.php');?
User::getById();
and
User::getByUserName();
Edit: In response to your question edit, generally frameworks have an auto-loading mechanism that will find and load a class file for you once you reference that class. So when you type User::getById(), the PHP interpreter will see that it needs to load the User class (if it hasn't been loaded already), and run the autoloading procedure to find the correct code to include.
I've never used Kohana, but I would be quite surprised if it didn't use some form of autoloading. If it does not, then yes, a simple include('User.php') will be enough to make the static method calls to User work.
The confusing thing is Kohana's convention of writing "helper" classes with lowercase names.
Your user php file will probably all ready be loaded if your using it as a model, so you can use zombat's suggests of User::getById();.
I don't like to follow their naming convensions for helpers or libraries and instead do:
require_once(Kohana::find_file('libraries', 'user_utils', TRUE, 'php'));

Resources