PHP

XML file for the view same name as of your layout:-

 
<?xml version="1.0" encoding="utf-8"?>
<metadata>
        <layout title="COM_HELLOWORLD_UPDHELLOWORLD_VIEW_DEFAULT_TITLE">
                <message>COM_HELLOWORLD_UPDHELLOWORLD_VIEW_DEFAULT_DESC</message>
        </layout>
</metadata>

Module parameters from inside a module 

$param = $params->get('paramName', 'defaultValue');

Module parameters from outside a module 

$module = &JModuleHelper::getModule('example'); 
$moduleParams = new JParameter($module->params); 
$param = $moduleParams->get('paramName', 'defaultValue');
 
Joomla User Save Using XCI:-
 
This function is defined in Candidate Model. 
 
function saveJoomlaUser($id, $pass, $name,$email,$option=false) {
        global $mainframe;

        // Check for request forgeries
//            JRequest::checkToken() or jexit('Invalid Token');
        // Get required system objects
        $user = clone(JFactory::getUser());
        $config = & JFactory::getConfig();
        $authorize = & JFactory::getACL();
        $document = & JFactory::getDocument();

        // Initialize new usertype setting
        $newUsertype = null; //$usersConfig->get('new_usertype');
        if (!$newUsertype) {
            $newUsertype = 'Registered';
        }

        // Bind the post array to the user object
        $userdata = array();
        $userdata['id'] = "";
        $userdata['name'] = $name;
        $userdata['username'] = $id;
        $userdata['email'] = $email;
        $userdata['password'] = $pass;
        $userdata['password2'] = $pass;
        if (!$user->bind($userdata, 'usertype')) {
            JError::raiseError(500, $user->getError());
        }

        // Set some initial user values
        $user->set('id', 0);
        $user->set('usertype', $newUsertype);
        $user->set('gid', $authorize->get_group_id('', $newUsertype, 'ARO'));

        $date = & JFactory::getDate();
        $user->set('registerDate', $date->toMySQL());

        $useractivation = 0; //$usersConfig->get('useractivation');
        if ($useractivation == '1') {
            jimport('joomla.user.helper');
            $user->set('activation', JUtility::getHash(JUserHelper::genRandomPassword()));
            $user->set('block', '1');
        }

        // If there was an error with registration, set the message and display form
        if (!$user->save()) {
            JError::raiseWarning('', JText::_($user->getError()));
            return false;
        }
        $this->jid = $user->id;
//        $this->save();

        if($option!=false){
             $q="update jos_users set password='".$pass."' where id= $this->jid";
             $this->db->query($q);
        }
        return true;
    }
 
Now This coding is done at front-end level in Candidate controller. 
$candidate=new Candidate(); 
$candidate->saveJoomlaUser($id, $pass, $name, $email);
$candidate->save(); 


 
Force Download: 
 
$file_name = $_GET['file'];

// make sure it's a file before doing anything!
if(is_file($file_name)) {

  /*
    Do any processing you'd like here:
    1.  Increment a counter
    2.  Do something with the DB
    3.  Check user permissions
    4.  Anything you want!
  */

  // required for IE
  if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off');  }

  // get the file mime type using the file extension
  switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
    case 'pdf': $mime = 'application/pdf'; break;
    case 'zip': $mime = 'application/zip'; break;
    case 'jpeg':
    case 'jpg': $mime = 'image/jpg'; break;
    default: $mime = 'application/force-download';
  }
  header('Pragma: public');   // required
  header('Expires: 0');    // no cache
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
  header('Cache-Control: private',false);
  header('Content-Type: '.$mime);
  header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: '.filesize($file_name));  // provide file size
  header('Connection: close');
  readfile($file_name);    // push it out
  exit();

}


//In Simple language:-
<?php 
    $file_name = 'file.exe';
    $file_url = 'http://www.myremoteserver.com/' . $file_name;
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"".$file_name."\""); 
    readfile($file_url);?>

Joomla 1.7 Permissions:
Below you can see the screenshot of a typical interface for Permission Settings. The settings include all created User Groups on your website. For each User Group you can set the Actions and Permission Level.
A User Group has 9 Actions:
  • Site Login – Allows users in the group to login to the front - end site.
  • Admin Login - Allows users in the group to login to the backend administrator site.
  • Super Admin – Allows users in the group to perform any action over the whole site regardless of any other permission settings.
  • Access Component – Allows users in the group to access all areas on the backend administrator site except Global Configuration.
  • Create – Allows users in the group to create any content in any extension.
  • Delete - Allows users in the group to delete any content in any extension.
  • Edit - Allows users in the group to edit any content in any extension.
  • Edit State – Allows users in the group to edit the state of any content in any extension.
  • Edit Own – Allows users in the group to edit any content they own in any extension.
The Action has 4 Permissions:
  • Not Set – No permission. (Available only in the Public User Group).
  • Inherited – The permission from the parent group will be used.
  • Denied – No matter what the parent group's setting is, the group being edited cannot take this action.
  • Allowed - The group being edited will be able to take this action
The Joomla 1.7 ACL defines 4 permission levels which can override one another. The low permission level uses the permission from parent level.
  • Level 1: Global Configuration
  • Level 2: Component Options
  • Level 3: Category
  • Level 4: Article

Global Configuration

Defines the default permission for each user group and actions.

Components

Overrides the default permission for components. For instance, Articles, Menus, Users, Banners and so on.

Category

Overrides the permission of Global Configuration and Components. It's available for components with categories including Articles, Contacts, Banners, Newsfeeds, and Weblinks.

Article

Overrides the permission of Global Configuration, Components and Category. It's only available for articles in Joomla 1.7 core.
With a plain 4-levels structure you can customize permission settings from the smallest object to the biggest one. One thing worth to mentioning here is that with the permission value "inherited" you can configure low-level permissions more effectively and faster.

The conclusion

With the launch of Joomla 1.7 the ACL mechanism added great value to the whole content management system. It gave us more ways to customize the user groups and assign permission for various purposes. Joomla 1.7 ACL is a serious step in the improvement and increased the flexibility of the system. If you have any opinions feel free to share them it out here in the comment box.


Defining custom position in Joomla template:

<?php if ($this->countModules('custom')) : ?>
<div>
<jdoc:include type="modules" name="custom" style="xhtml" />
</div>
<?php endif; ?>


Safe way to autoload a php class:
<?php
//check to see if there is an existing __autoload function from another library
if(!function_exists('__autoload')) {
    if(function_exists('spl_autoload_register')) {
        //we have SPL, so register the autoload function
        spl_autoload_register('my_autoload_function');      
    } else {
        //if there isn't, we don't need to worry about using the stack,
        //we can just register our own autoloader
        function __autoload($class_name) {
            my_autoload_function($class_name);
        }
    }
} else {
    //ok, so there is an existing __autoload function, we need to use a stack
    //if SPL is installed, we can use spl_autoload_register,
    //if there isn't, then we can't do anything about it, and
    //will have to die
    if(function_exists('spl_autoload_register')) {
        //we have SPL, so register both the
        //original __autoload from the external app,
        //because the original will get overwritten by the stack,
        //plus our own
        spl_autoload_register('__autoload');
        spl_autoload_register('my_autoload_function');      
    } else {
        exit;
    }
}
?> 


Safe redirection in php:
<?php 
function Redirect($Str_Location) {
    if (!headers_sent()) {
        header('location: ' . urldecode($Str_Location));
        exit;
    }
    die("<script>location.href = '".urldecode($Str_Location)."'</script>");
    return;
}

?>



Calling class function with variable number of Arguments (accessing from URL) in php:-
<?php
class ABC{
    function singleArg($a){
        echo $a."<br />";
    }
    function doubleArg($a,$b){
        echo "$a<br />$b";
    }
}
$url=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$explodURL=  explode("/", $url);
$ob= new $explodURL[3]();
//$ob->$explodURL[4](5);
$i=0;
$args=array();
foreach($explodURL as $e){
    if($i<5){
        $i++;
        continue;
    }
    echo $e;
    array_push($args,$e);
}
print_r($args);
call_user_method_array($explodURL[4], $ob, $args);// this is deprecated, so better to use
//call_user_func(array($ob,$explodURL[4] ), $args);

?>

Sample Calculator code in php:

<?php
session_start();
if(isset($_GET['one'])){
    if(isset($_SESSION['num1'])){
        $_SESSION['num1'].="1";
    }
    else
        $_SESSION['num1']="1";
}
if(isset($_GET['two'])){
    if(isset($_SESSION['num1'])){
        $_SESSION['num1'].="2";
    }
    else
        $_SESSION['num1']="2";
}
if(isset($_GET['three'])){
    if(isset($_SESSION['num1'])){
        $_SESSION['num1'].="3";
    }
    else
        $_SESSION['num1']="3";
}
if(isset($_GET['add'])){
    if(isset($_SESSION['result'])){
        $_SESSION['result']=0;
    }
    $_SESSION['result']+=$_SESSION['num1'];
    $_SESSION['num1']='';
    $_SESSION['op']='+';
}
if(isset($_GET['equal'])){
    switch($_SESSION['op']){
        case '+':$_SESSION['result']+=$_SESSION['num1'];
    }
    $_SESSION['num1']=$_SESSION['result'];
}
if(isset($_GET['reset'])){
    session_destroy();
    session_unset();    
}
?>
<html>
    <head>
        <title>Calc</title>
    </head>
    <body>
        <form>
            <input type="text" name="number" id="number" value="<?php if(isset($_SESSION['num1'])) echo $_SESSION['num1'];?>"/><br />
            <input type="submit" name="one" id="one" value="1"/>
            <input type="submit" name="two" id="two" value="2"/>
            <input type="submit" name="three" id="three" value="3"/><br />
            <input type="submit" name="add" id="add" value="+"/>
            <input type="submit" name="sub" id="sub" value="-"/>
            <input type="submit" name="mul" id="mul" value="*"/><br />
            <input type="submit" name="div" id="div" value="/"/>
            <input type="submit" name="equal" id="equal" value="="/>
            <input type="submit" name="reset" id="reset" value="C"/>
        </form>
    </body>
</html>


No comments:

Post a Comment