calling a function within a class with a Joomla module - joomla

I have a Joomla helper class that I'm using for module development with a method I'm trying to call:
class modCamcloudReferralHelper
{
/*
* Sanitize email form
*/
public function isInjected($str) {
$inject = "/(\r|\t|%0A|%0D|%08|%09)+/i";
return (preg_match($inject, $str) > 0);
}
public static function sendEmail() {
$jinput = JFactory::getApplication()->input;
$email = $jinput->post->get('email', '', 'STRING');
//check email is fine
if (isInjected($email)){ //never get into this code and it causes some sort of failure
echo "blah";
}
}
}
Simple, right? But this code just gives me a blank page and I don't see any errors...anywhere. I can instead just put this code right into my sendEmail function and it works just fine:
$inject = "/(\r|\t|%0A|%0D|%08|%09)+/i";
if (preg_match($inject, $email) > 0){
echo "This works";
}
I've had this problem with my Joomla components I've built before. For some reason calling this function from inside the same class is not working. It must be a Joomla thing...or I'm going nuts. Any ideas?

You should call the method with a reference to its container, even if it's local.
So the right syntax here is:
if (self::isInjected($email))
of from another class:
modCamcloudReferralHelper::isInjected(
This is good for helpers: just make sure you declare the method you are invoking as static
public static function isInjected($str) {
If however you're calling a method on an instantiated class (a view, a template, a model, you should use
$this->method()

Related

Unable to call custom methods within extended Magento model

I'm attempting to customize the way pricing / tiered pricing is displayed in Magento CE 1.6.0.0.
I've followed the instructions in the second post of the link below to override Mage_Catalog_Model_Product_Type_Price
http://www.magentocommerce.com/boards/viewthread/16829/
Following is my custom model class:
class PHC_Price_Model_Price extends Mage_Catalog_Model_Product_Type_Price {
public function getPrice() {
echo "overridden getPrice method called<br>";
}
public function getPHCDisplayPrice($product) {
echo "custom price function called<br>";
}
}
I'm able to successfully call the overridden getPrice() function from my template file as follows:
$product = Mage::getModel("catalog/product")->load($_product->entity_id);
$displayPrice = $product->getPrice();
However, when I try to call my custom price function with
$product = Mage::getModel("catalog/product")->load($_product->entity_id);
$displayPrice = $product->getPHCDisplayPrice();
I get absolutely nothing. Can anyone tell me what I'm missing?
It't normal that you don't get a result. I would be amazed if this worked.
You are overriding the class Mage_Catalog_Model_Product_Type_Price, but in your example the $product variable is an instance of Mage_Catalog_Model_Product. That class does not have the method getPHCDisplayPrice and it calls the __call method and returns null.
You get the expected result when calling getPrice by accident. It is because the getPrice method in Mage_Catalog_Model_Product looks like this:
public function getPrice()
{
if ($this->_calculatePrice || !$this->getData('price')) {
return $this->getPriceModel()->getPrice($this);
} else {
return $this->getData('price');
}
}
So when you call it, it calls $this->getPriceModel()->getPrice($this) and $this->getPriceModel() returns an instance of your class.

Laravel 4: Responding to AJAX requests from controller

I'm trying to generate ajax specific responses from my controllers by using the Request::ajax() method, which is working just fine. The only problem is that the way I have it set up right now isn't really a nice looking solution.
My controller:
class HomeController extends BaseController {
protected $layout = 'layouts/main';
public function __construct()
{
$this->beforeFilter('auth');
}
public function getIndex()
{
$view = View::make('content.home.index');
if(Request::ajax()) return $view; //For ajax calls we only want to return the content to be placed inside our container, without the layout
$this->layout->menu = 'content.menu';
$this->layout->content = $view;
}
}
So right now, for every method I define within my controllers I need to add the code snippet that checks for an AJAX request and returns a single view if the statement returns true.
This leads to my question that is probably more PHP related than it is to the framework;
Is there a way of executing my AJAX check on every method call, without actually placing it inside the method? Or is there some other solution to keep my code DRY?
Thanks in advance!
PS: This is my first post on stackoverflow, so feel free to correct me if I made any mistakes
Create a new barebone layout named 'layouts/ajax' (or any name you like).
<?php echo $content ?>
In your Base controller, override this setupLayout() function.
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$layout = Request::ajax() ? 'layouts/ajax' : $this->layout;
$this->layout = View::make($layout);
}
}
Change your getIndex() function to this.
public function getIndex()
{
$view = View::make('content.home.index');
$this->layout->menu = 'content.menu';
$this->layout->content = $view;
}
Now non-ajax requests will be rendered using layout set in the controller, where as ajax requests will receive whatever set to $this->layout->content.
Note : Controller will neglect the layout setup in setupLayout(), if the called method returns truthy value. So this method will not work for functions like below.
public function getIndex()
{
return View::make('content.home.index');
}
You could just change the layout property, in the constructor, if it's an ajax request:
public function __construct()
{
$this->beforeFilter('auth');
if(Request::ajax()) {
$this->layout = '';
}
}
If it doesn't work try setting it to NULL instead.
Why would you return a VIEW via ajax? Are you using it to create a SPA? If so there are better ways. I'm generally against returning HTML via AJAX.
The route I'd go in your position is probably opposite of how you're doing it. Render the view no matter what, if the request is ajax, pass the extra data back and have JS render the data on the page. That's essentially how most Javascript MVC frameworks function.
Sorry if I am totally missing the point here, just going on an assumption of your end goal with the info you provided.

why my joomla controllers method is not getting executed

I have an ajax method which sends data to one of my controller but the method inside of my controller is not getting fired. Everytime the first method is getting executed on call. The controller looks as it follows
class TieraerzteControllerUploader extends JController
{
/**
* display task
*
* #return void
*/
function display($cachable = false)
{
require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/upload.php';
$upload_handler = new UploadHandler();
//this one is going to be outputed
die();
}
public function locator(){
// I wait here for a dump, but is not happening
var_dump('test');
die();
echo '{"text":"John Smith","id":"433"},{"text":"Paul Sparks","id":"434"}';
}
}
I call the controller with the following url
/administrator/index.php?option=com_tieraerzte&task=uploader.locator&tmpl=component&q=search
even if I call the above url the result is the same
I think you are using wrong formated joomla url.
Try this url formate,u may call the controller
index.php?option=com_tieraerzte&view=uploader&tmpl=component&q=search
watch the difference between your url and my url.

Codeigniter template library and HMVC ( mx library ) the static method run()

i have this testing code which am working with ..
i have a module called ms and and another one called test
the test controller code is :
<?php
class Test extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->template->title($this->config->item('site_name','app'));
}
public function index()
{
$this->template->build('index');
}
}
and the code inside ms is :
<?php
//ms module
class Msrofi extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->template->title($this->config->item('site_name','app'));
}
public function index()
{
$t = Modules::run('test/test/index');
var_dump($t);
$this->template->build('index_message');
}
}
the problem is that the build function inside test is trying to find the index view file inside the ms views folder not the test views folder ..
i checked the $this->_module and it gave me the ms module name ..
any one know how to fix that ??
Since the test module is being called in the context of the ms one, $this->template->build() is looking for a view file in the ms module. The same way you can load models and libraries cross-module, you would have to do this for your view path as well:
class Test extends MX_Controller {
public function index()
{
// This path works only from the "test" module
// $this->template->build('index');
// This path works from any module
$this->template->build('test/index');
}
}
It's a little annoying maybe to have to explicitly call the module path in the module itself, but cross-module dependency defeat some of the goals of modularity in the first place.
A quick aside: Modules::run() output not returned, but directly echoed, so you can't assign it to a variable or print_r/var_dump it without using an output buffer:
ob_start();
Modules::run('test/test/index');
$t = ob_get_clean();
var_dump($t);
You can try to change the module.php the run method
The following example is I have to use the fix solution:
Open the third_party/MX/Modules.php
Near 75 lines to find
$buffer = ob_get_clean();
Increase in its following:
if($output === NULL && $buffer === ''){
$output = CI::$APP->output->get_output();
}
At this time, it should be able to work properly...

Zend Framework: Incomplete object error when passing a value from controller to view

I'm passing a user object from the controller to the view, then calling a method on that controller. I've done a print_r on the object in the view, so I know it's the right object with the right values. The current_user variable is an instance of the user class.
Here is the line in the layout that gives the error.
<?php echo $this->current_user->get_avatar_url(); ?>
Here is the method in the user class it's calling
public function get_avatar_url()
{
return !empty($this->avatar) ? $this->avatar : $this->fb_userid != '' ? "http://graph.facebook.com/".$this->fb_userid."/picture" : "/public/images/pukie.jpg";
}
This is the error I get
Fatal error: main() The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "User" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition in /home/breathel/public_html/application/views/layouts/layout.phtml on line 48
I'm including the full controller base where this in called in case it makes a difference
<?php
Zend_Loader::loadClass('Zend_Controller_Action');
Zend_Loader::loadClass('User');
class BaseController extends Zend_Controller_Action
{
protected $auth;
protected $current_user;
protected $db;
protected function initialize_values()
{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$this->current_user = $auth->getIdentity();
$this->view->current_user = $this->current_user;
}
$this->db = Zend_Registry::get('dbAdapter');
$this->view->controller_name = $this->_request->getControllerName();
$this->view->view_name = $this->_request->getActionName();
}
}
Zend Framework's authorisation module uses sessions to preserve identity across page load and is probably serialising the User model under the covers (especially if you're just assigning the result of a Zend_Auth_Adapter call).
Try including the User class before the first call to getIdentity() and see if that fixes it (even if you're confident you're not serialising it yourself).

Resources