Joomla call function - joomla

I have a joomla3 site, and want to call function from plugin in joomla template. For that I write in template this code <?php echo my_function($id);?>.
In plugin class I have
class plgSystemMyPlugin extends JPlugin {...}
function my_function($id) { echo $id; }
But when I refresh the page I get
Failed to load resource: the server responded with a status of 500
(Internal Server Error)
What's the problem, can anybody help me please?

Hello Marina,
You should use as per below in your Joomla! template index.php :
JPluginHelper::importPlugin( 'system' );
$dispatcher = JEventDispatcher::getInstance();
$result = $dispatcher->trigger('MySpecificEvent', $arguments);
<?php echo $result;?>
and in your system plugin that you created,
class plgSystemMyPlugin extends JPlugin {
public function MySpecificEvent($id){
echo $id;
}
}
Let me know if it works or not for you.
Thanks

Related

How to load a library from composer?

I load a composer library suited for CodeIgniter called SteeveDroz\Asset, that I can access without problem with $asset = new SteeveDroz\Asset.
I would like to be able to load it with CodeIgniter $this->load->library('SteeveDroz\Asset'), but I get the error message
Unable to load requested class: SteeveDroz\Asset
Is it possible to achieve what I want? If yes, how?
As mentionned Alex in his comment, it is required to make an adapter library. I created an all purpose class for that:
application/libraries/ComposerAdapter.php
class ComposerAdapter
{
private $object;
public function __construct($object)
{
$this->object = $object;
}
public function __call($method, $args)
{
return call_user_func_array([$this->object, $method], $args);
}
}
application/libraries/Asset.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require('ComposerAdapter.php');
class Asset extends ComposerAdapter
{
public function __construct()
{
parent::__construct(new SteeveDroz\Asset());
}
}
application/config/autoload.php
// ...
$autoload['libraries'] = array('asset');
// ...
if you are using CodeIgniter 3 you can modify application/config/config.php and set
$config['composer_autoload'] = TRUE
or
$config['composer_autoload'] = FCPATH .'vendor/autoload.php';
this will automatically load all your composer dependencies.

Codeigniter 3.1.9 MY_Form_validation is not working

Im working on Codeigniter 3.1.9 and completed my form on local machine. i just uploaded my app some moment ago on server and getting error
Unable to access an error message corresponding to your field name
URL.(valid_url_format)
i google alot but unable to fix problem.
Filename: My_Form_validation.php
Location: application\libraries
class MY_Form_validation extends CI_Form_validation{
public function __construct()
{
parent::__construct();
}
function valid_url_format($str){
$pattern = "/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";
if (!preg_match($pattern, $str)){
$this->set_message('valid_url_format', 'The URL you entered is not correctly formatted.');
return FALSE;
}
return TRUE;
}
function url_exists($url){
$url_data = parse_url($url); // scheme, host, port, path, query
if(!fsockopen($url_data['host'], isset($url_data['port']) ? $url_data['port'] : 80)){
$this->set_message('url_exists', 'The URL you entered is not accessible.');
return FALSE;
}
return TRUE;
}
}
Filename: UrlChecker.php
Location:application\controllers
class UrlChecker extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function _initializing(){
}
public function index()
{
$this->form_validation->set_rules('link', 'URL', 'required|trim|valid_url_format|url_exists');
if ($this->form_validation->run() == FALSE)
{
echo validation_errors('<div class="alert alert-danger" role="alert">', '</div>');
}
else
{
echo 'ok';
}
}
Please check and let me know whats wrong is there hosting version problem or there is something else.
I always use custom validation on the fly and its the first time to try to make a custom library for additional validations, anyways i created and tested it to make sure it works, you got to make sure you follow the naming convention, the file name should be like this: MY_Form_validation.php and save it in your application/libraries then create your class:
class MY_Form_validation extends CI_Form_validation
{
// your rules
}
then you have to create error messages for every method, create a lang file in your application/language/english/form_validation_lang.php and add your custom error messages like this:
$lang['valid_url_format'] = 'The {field} field may only contain valid url.';
$lang['url_exists'] = 'The {field} field already exists';

Check for Sessions in Controller _constructor

I am new to Codeigniter and still trying to understand some basics.
I am building a registration/login system. everything good for now.
I am creating a controller to show the user info. Before that I want to check if the session existis and if user is logged in, if not I want to redirect to the site root.
class Myprofile extends CI_Controller {
function __construct()
{
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}
The problem is the error I am getting. It looks like I it can't see the sessions in the construct part of the script.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Myprofile::$session
Filename: controllers/myprofile.php
Line Number: 6
This would be the very best solution, otherwise I need to put the same code on all the functions.
Hope to hear some feedback help. Thanks
Perhaps the session library is not loaded. Add session class on the autoload array() located in application/config/autoload.php
eg.
$autoload['libraries'] = array('database', 'session', 'output');
class Myprofile extends CI_Controller {
function __construct()
{
$this->load->library('session');
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}

Codeigniter HMVC Modular Error

I'm new to CI + MX and I tried the Modules::run(); style but I can't seem to let it work.
Here's my directory structure:
/application
-/modules
--/main
---/controllers
----main.php
---/models
---/views
--/connections
---/controllers
----connections.php
---/models
----/group_model.php
---/views
----connection_view.php
main.php controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends MX_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
}
function index(){
echo modules::run('connections/load_connections');
}
}
?>
connections.php controller:
<?php
class Connections extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('connections/group_model');
}
function load_connections(){
$user_id = 2;
$data['tabs'] = $this->group_model->get_groups($user_id);
$this->load->view('connection_view', $data);
}
}
?>
group_model.php model:
class Group_model extends CI_Model{
function __construct(){
parent::__construct();
}
/**
* Get all groups in db
**/
function get_groups($user_id){
$this->db->select('g.group_name');
$this->db->from('groups AS g');
$this->db->join('members AS m', 'g.group_id = m.group_id');
$this->db->where('m.user_id', $user_id);
return $this->db->get()->result_array();
}
}
My connection_view.php view contains a div and some php codes to display the $data['tabs'] passed as array in load_connections function.
The problem is, I'm getting an error that says:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Connections::$group_model
Filename: controllers/connections.php
Line Number: 14
and
Fatal error: Call to a member function get_groups() on a non-object in C:\xampp\htdocs\hmvc\application\modules\connections\controllers\connections.php on line 14
I have clearly followed all the instructions provided on MX wiki at https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
and set up everything as needed.
My database.php under /application/config is already configured.
routes.php is also configured pointing the default controller to main.php
I wonder what I have missed or have done wrong that I can't get it to work.
Codeigniter version: 2.1.3
MX version: 5.4
almost missed it, extend connections with MX_Controller since you are calling modules::run(). Whenever you want a controller to be called with modules::run(), you extend it with MX_Controller instead of CI_Controller
Your second error is caused because of your first error.
Your first error is most likely caused because it cant open the group_model.
try this
$this->load->model('group_model');

Error message on callback function not show

I have the below code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('users/user_model');
$this->load->library('form_validation');
}
function _load_login_page()
{
$this->load->model('sysconfig/sysconfig_model');
$a = $this->sysconfig_model->get_sysconfig();
$row = $a[0];
$this->lang->load('klas',$row->sysconfig_language);
$data['sys_name'] = $row->sysconfig_system_name;
$data['template_name'] = $row->systemplate_name;
$data['css_script'] = base_url().'assets/'.$row->systemplate_name;
if($row->sysconfig_maintenance === 'Y')
{
$this->load->view('sysconfig/maintenance',$data);
}
else {
$this->load->view('login',$data);
}
}
function index()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[12]|xss_clean|callback_check_auth');
$this->form_validation->set_rules('password','Password','trim|required');
if($this->form_validation->run($this) == FALSE)
{
$this->_load_login_page();
} else {
redirect('welcome','refresh');
}
}
function check_auth()
{
if($this->user_model->authentication())
{
return TRUE;
}
$this->form_validation->set_message('check_auth',$this->lang->line('invalid_username'));
return FALSE;
}
?>
user_model.php
<?php
class User_Model extends CI_Model
{
function authentication()
{
$this->db->where('useracc_id', $this->input->post('username'));
$this->db->where('useracc_password', md5($this->input->post('password')));
$q = $this->db->get('base_useracc');
if($q->num_rows() == 1)
{
$session_data = array('isUserLogged'=>TRUE);
$this->session->set_userdata($session_data);
return TRUE;
}
}
?>
From here we can see if the user didn't fill the username and password fields, it will show the error and everything works as expected. The problem is, if the user provides an invalid username or password, the error message won't show.
for the information, I already put $lang['invalid_username'] = 'Invalid username or password'; on the language file.
I am doing this using the HMVC technique. Please help me.
You don't seem to be passing any data to your callback function. Callback functions expect the value of the input field to be passed as a parameter. I don't think you can make this work as a form validation callback be because the authentication method presumably needs to know both the password and the username. At present you're not passing any data into your check_auth method or indeed onwards to your user_model->authentication() method. That is why form_validation is ignoring it.
Rather than calling check_auth as a callback, why not run the form_validation first (this is really what it's for - checking the data is correct and sanitised) and then pass the values from your form to your check_auth function as part of an if statement. You will not be able to use the form_validation set_message method to display errors but I think this is a cleaner approach.
To summarise, use the form_validation to check the data you are receiving is ok and display relevant messages if it is not. Authenticating a user based on that information is a separate procedure that I don't think belongs with validation. Do you see the difference?
from the HMVC wiki page :
When using form validation with MX you will need to extend the CI_Form_validation class as shown below, before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly. (This has been discussed on the CI forums also). ie:
<?php
/** application/libraries/MY_Form_validation **/
class MY_Form_validation extends CI_Form_validation
{
public $CI;
}
<?php
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
}

Resources