Webocreation

Wednesday, June 8, 2011

Collection of function and objects





currency:
set($currency):
Set the currency to be used in overall site.
public function set($currency) {
                $this->code = $currency;
                if ((!isset($this->session->data['currency'])) || ($this->session->data['currency'] != $currency)) {
                                $this->session->data['currency'] = $currency;
                }
                if ((!isset($this->request->cookie['currency'])) || ($this->request->cookie['currency'] != $currency)) {
                                                setcookie('currency', $currency, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']);
                }
                }
format($number, $currency = '', $value = '', $format = TRUE):

public function format($number, $currency = '', $value = '', $format = TRUE) {
                                if ($currency && $this->has($currency)) {
                                $symbol_left   = $this->currencies[$currency]['symbol_left'];
                                $symbol_right  = $this->currencies[$currency]['symbol_right'];
                                $decimal_place = $this->currencies[$currency]['decimal_place'];
                } else {
                                $symbol_left   = $this->currencies[$this->code]['symbol_left'];
                                $symbol_right  = $this->currencies[$this->code]['symbol_right'];
                                $decimal_place = $this->currencies[$this->code]['decimal_place'];
                                               
                                                $currency = $this->code;
                }

                if ($value) {
                                $value = $value;
                } else {
                                $value = $this->currencies[$currency]['value'];
                }

                if ($value) {
                                $value = $number * $value;
                } else {
                                $value = $number;
                }

                $string = '';

                if (($symbol_left) && ($format)) {
                                $string .= $symbol_left;
                }

                                if ($format) {
                                                $decimal_point = $this->language->get('decimal_point');
                                } else {
                                                $decimal_point = '.';
                                }
                               
                                if ($format) {
                                                $thousand_point = $this->language->get('thousand_point');
                                } else {
                                                $thousand_point = '';
                                }
                               
                $string .= number_format(round($value, (int)$decimal_place), (int)$decimal_place, $decimal_point, $thousand_point);

                if (($symbol_right) && ($format)) {
                                $string .= $symbol_right;
                }

                return $string;
                }
convert($value, $from, $to):
Convert the given value from once currency to other. The parameters are $value means of what amount and $from means of which currency and $to mean to which currency is the value to be converted.
public function convert($value, $from, $to) {
                                if (isset($this->currencies[$from])) {
                                                $from = $this->currencies[$from]['value'];
                                } else {
                                                $from = 0;
                                }
                                if (isset($this->currencies[$to])) {
                                                $to = $this->currencies[$to]['value'];
                                } else {
                                                $to = 0;
                                }                             
                                return $value * ($to / $from);
                }
getId():
Returns the currency ID.
public function getId() {
                                return $this->currencies[$this->code]['currency_id'];
                }
getCode():
Returns the currency code.
public function getCode() {
                return $this->code;
                }
getValue($currency):
If the currency is set then returns the value nor it will return zero.
public function getValue($currency) {
                                if (isset($this->currencies[$currency])) {
                                                return $this->currencies[$currency]['value'];
                                } else {
                                return 0;
                                }
                }
has($currency):
Whether the request currency is available or not.
public function has($currency) {
                return isset($this->currencies[$currency]);
                }

customer:
login($email, $password):
Check whether the customer is approved and check whether the username and password is correct or not.
public function login($email, $password) {
                                if (!$this->config->get('config_customer_approval')) {
                                                $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(strtolower($email)) . "' AND password = '" . $this->db->escape(md5($password)) . "' AND status = '1'");
                                } else {
                                                $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(strtolower($email)) . "' AND password = '" . $this->db->escape(md5($password)) . "' AND status = '1' AND approved = '1'");
                                }
                               
                                if ($customer_query->num_rows) {
                                                $this->session->data['customer_id'] = $customer_query->row['customer_id'];
                                   
                                                if (($customer_query->row['cart']) && (is_string($customer_query->row['cart']))) {
                                                                $cart = unserialize($customer_query->row['cart']);
                                                               
                                                                foreach ($cart as $key => $value) {
                                                                                if (!array_key_exists($key, $this->session->data['cart'])) {
                                                                                                $this->session->data['cart'][$key] = $value;
                                                                                } else {
                                                                                                $this->session->data['cart'][$key] += $value;
                                                                                }
                                                                }                                             
                                                }
                                               
                                                $this->customer_id = $customer_query->row['customer_id'];
                                                $this->firstname = $customer_query->row['firstname'];
                                                $this->lastname = $customer_query->row['lastname'];
                                                $this->email = $customer_query->row['email'];
                                                $this->telephone = $customer_query->row['telephone'];
                                                $this->fax = $customer_query->row['fax'];
                                                $this->newsletter = $customer_query->row['newsletter'];
                                                $this->customer_group_id = $customer_query->row['customer_group_id'];
                                                $this->address_id = $customer_query->row['address_id'];
     
                                                return TRUE;
                } else {
                                return FALSE;
                }
                }
 
logout():
Destroy all the session of the logged customer.
public function logout() {
                                unset($this->session->data['customer_id']);

                                $this->customer_id = '';
                                $this->firstname = '';
                                $this->lastname = '';
                                $this->email = '';
                                $this->telephone = '';
                                $this->fax = '';
                                $this->newsletter = '';
                                $this->customer_group_id = '';
                                $this->address_id = '';
                               
                                session_destroy();
                }
isLogged():
Check whether the customer is logged in or not.
public function isLogged() {
                return $this->customer_id;
                }
getId(), getFirstName(), getLastName(), getEmail(), getTelephone(), getFax(), getNewsletter(), getCustomerGroupId(), getAddressId():
Returns the customer information like for ID we have to write $this->customer->getId().
public function getId() {
                return $this->customer_id;
                }
     
                public function getFirstName() {
                                return $this->firstname;
                }
 
                public function getLastName() {
                                return $this->lastname;
                }
 
                public function getEmail() {
                                return $this->email;
                }
 
                public function getTelephone() {
                                return $this->telephone;
                }
 
                public function getFax() {
                                return $this->fax;
                }
               
                public function getNewsletter() {
                                return $this->newsletter;           
                }

                public function getCustomerGroupId() {
                                return $this->customer_group_id;         
                }
               
                public function getAddressId() {
                                return $this->address_id;           
                }



db:
query($sql):
$this->db->query(“Select * from product”); This returns all the product available in the database
public function query($sql) {
                                return $this->driver->query($sql);
                }
escape($value):
public function escape($value) {
                                return $this->driver->escape($value);
                }
countAffected():
public function countAffected() {
                                return $this->driver->countAffected();
                }

getLastId():
Last affected or inserted or deleted Id is returned.
public function getLastId() {
                                return $this->driver->getLastId();
                }

document:
setTitle($title):
The title of the page is set with the call of $this->document->setTitle($title); where $title is the parameter which take the string.
public function setTitle($title) {
                                $this->title = $title;
                }
getTitle():
$this->document->setTitle() means the title is get and set to <title> of the document
public function getTitle() {
                                return $this->title;
                }
setDescription($description):
$this->document->setDescription($description) which set the description of the page.
public function setDescription($description) {
                                $this->description = $description;
                }
getDescription():
$this->document->getDescription() gives us the description of the document which is embedded in the <head> of the document
public function getDescription() {
                                return $this->description;
                }
setKeywords($keywords):
$this->document->setKeywords($keywords) set the SEO keywords
public function setKeywords($keywords) {
                                $this->keywords = $keywords;
                }
getKeywords():
$this->document->getKeywords() get the keywords which are set for the page.
public function getKeywords() {
                                return $this->keywords;
                }
setBase($base):
$this->document->setBase($base)  the base url for the document. If we need different base url then we have to set it.
public function setBase($base) {
                                $this->base = $base;
                }
getBase():
Retrieve the set base url
public function getBase() {
                                return $this->base;
                }             
setCharset($charset):
public function setCharset($charset) {
                                $this->charset = $charset;
                }
               
getCharset():

                public function getCharset() {
                                return $this->charset;
                }             
setLanguage($language):
public function setLanguage($language) {
                                $this->language = $language;
                }
getLanguage():
public function getLanguage() {
                                return $this->language;
                }             
setDirection($direction):
public function setDirection($direction) {
                                $this->direction = $direction;
                }
getDirection():
public function getDirection() {
                                return $this->direction;
                }
addLink($href, $rel):
public function addLink($href, $rel) {
                                $this->links[] = array(
                                                'href' => $href,
                                                'rel'  => $rel
                                );                                            
                }

getLinks():
public function getLinks() {
                                return $this->links;
                }             
addStyle($href, $rel = 'stylesheet', $media = 'screen'):
public function addStyle($href, $rel = 'stylesheet', $media = 'screen') {
                                $this->styles[] = array(
                                                'href'  => $href,
                                                'rel'   => $rel,
                                                'media' => $media
                                );                                            
                }

getStyles():
public function getStyles() {
                                return $this->styles;
                }

addScript($script):
public function addScript($script) {
                                $this->scripts[] = $script;                                             
                }             
getScripts():
public function getScripts() {
                                return $this->scripts;
                }

addBreadcrumb($text, $href, $separator = ' &gt; '):
public function addBreadcrumb($text, $href, $separator = ' &gt; ') {
                                $this->breadcrumbs[] = array(
                                                'text'      => $text,
                                                'href'      => $href,
                                                'separator' => $separator
                                );                                            
                }

getBreadcrumbs():
public function getBreadcrumbs() {
                                return $this->breadcrumbs;
                }


encryption:
encrypt($value):
function encrypt($value) {
                                if (!$this->key) {
                                                return $value;
                                }
                                $output = '';
                                for ($i = 0; $i < strlen($value); $i++) {
                                                $char = substr($value, $i, 1);
                                                $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1);
                                                $char = chr(ord($char) + ord($keychar));
                                                $output .= $char;
                                }
        return base64_encode($output);
                }
We pass the value and the value is converted to the base64_encode
base64_encode()
decrypt($value):
The encrypted value is gained after decryption.
function decrypt($value) {
                                if (!$this->key) {
                                                return $value;
                                }
                                $output = '';
                                $value = base64_decode($value);
                                for ($i = 0; $i < strlen($value); $i++) {
                                                $char = substr($value, $i, 1);
                                                $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1);
                                                $char = chr(ord($char) - ord($keychar));                                               
                                                $output .= $char;
                                }
                                return $output;
                }

image:
create($image):
Creation of the image and use of the image create from different type of image like gif, png, jpeg etc
private function create($image) {
                                $mime = $this->info['mime'];    
                                if ($mime == 'image/gif') {
                                                return imagecreatefromgif($image);
                                } elseif ($mime == 'image/png') {
                                                return imagecreatefrompng($image);
                                } elseif ($mime == 'image/jpeg') {
                                                return imagecreatefromjpeg($image);
                                }
    }         
save($file, $quality = 100):
Save file that are of gif, jpeg, jpg
public function save($file, $quality = 100) {
        $info = pathinfo($file);
        $extension = $info['extension'];
        if ($extension == 'jpeg' || $extension == 'jpg') {
            imagejpeg($this->image, $file, $quality);
        } elseif($extension == 'png') {
            imagepng($this->image, $file, 0);
        } elseif($extension == 'gif') {
            imagegif($this->image, $file);
        }        
                    imagedestroy($this->image);
    }
resize($width = 0, $height = 0):
Resize the image function
public function resize($width = 0, $height = 0) {
                if (!$this->info['width'] || !$this->info['height']) {
                                                return;
                                }
                                $xpos = 0;
                                $ypos = 0;
                                $scale = min($width / $this->info['width'], $height / $this->info['height']);
                                if ($scale == 1) {
                                                return;
                                }
                                $new_width = (int)($this->info['width'] * $scale);
                                $new_height = (int)($this->info['height'] * $scale);                                         
                                $xpos = (int)(($width - $new_width) / 2);
                                $ypos = (int)(($height - $new_height) / 2);
                $image_old = $this->image;
        $this->image = imagecreatetruecolor($width, $height);
                                if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {                   
                                                imagealphablending($this->image, false);
                                                imagesavealpha($this->image, true);
                                                $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
                                                imagecolortransparent($this->image, $background);
                                } else {
                                                $background = imagecolorallocate($this->image, 255, 255, 255);
                                }
                                imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
        imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
        imagedestroy($image_old);
        $this->info['width']  = $width;
        $this->info['height'] = $height;
    }
watermark($file, $position = 'bottomright'):
Function to watermark the image may be with the name of the site or anything you like the value to pass is for the $watermark
public function watermark($file, $position = 'bottomright') {
        $watermark = $this->create($file);
        $watermark_width = imagesx($watermark);
        $watermark_height = imagesy($watermark);
        switch($position) {
            case 'topleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = 0;
                break;
            case 'topright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = 0;
                break;
            case 'bottomleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
            case 'bottomright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
        }
        imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);
        imagedestroy($watermark);
    }
crop($top_x, $top_y, $bottom_x, $bottom_y):
function to crop the image into the specified length and breadth.
public function crop($top_x, $top_y, $bottom_x, $bottom_y) {
        $image_old = $this->image;
        $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);
       
        imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']);
        imagedestroy($image_old);
       
        $this->info['width'] = $bottom_x - $top_x;
        $this->info['height'] = $bottom_y - $top_y;
    }
rotate($degree, $color = 'FFFFFF'):
public function rotate($degree, $color = 'FFFFFF') {
                                $rgb = $this->html2rgb($color);
                               
        $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
       
                                $this->info['width'] = imagesx($this->image);
                                $this->info['height'] = imagesy($this->image);
    }
filter($filter):
private function filter($filter) {
        imagefilter($this->image, $filter);
    }
text($text, $x = 0, $y = 0, $size = 5, $color = '000000'):
private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') {
                                $rgb = $this->html2rgb($color);
       
                                imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
    }
merge($file, $x = 0, $y = 0, $opacity = 100):
private function merge($file, $x = 0, $y = 0, $opacity = 100) {
        $merge = $this->create($file);

        $merge_width = imagesx($image);
        $merge_height = imagesy($image);
                                       
        imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity);
    }
html2rgb($color):
private function html2rgb($color) {
                                if ($color[0] == '#') {
                                                $color = substr($color, 1);
                                }
                                if (strlen($color) == 6) {
                                                list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);  
                                } elseif (strlen($color) == 3) {
                                                list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);   
                                } else {
                                                return FALSE;
                                }
                                $r = hexdec($r);
                                $g = hexdec($g);
                                $b = hexdec($b);   
                                return array($r, $g, $b);
                }

Json:
encode($data):
static public function encode($data) {
                                if (function_exists('json_encode')) {
                                                return json_encode($data);
                                } else {
                                                switch (gettype($data)) {
                                                                case 'boolean':
                                                                                return $data ? 'true' : 'false';
                                                case 'integer':
                                                case 'double':
                                                                return $data;
                                                case 'resource':
                                                case 'string':
                                                                return '"' . str_replace(array("\r", "\n", "<", ">", "&"), array('\r', '\n', '\x3c', '\x3e', '\x26'), addslashes($data)) . '"';
                                                                case 'array':
                                                                                if (empty($data) || array_keys($data) === range(0, sizeof($data) - 1)) {
                                                                                                $output = array();
                                                                                               
                                                                                                foreach ($data as $value) {
                                                                                                                $output[] = Json::encode($value);
                                                                                                }
                                                                                               
                                                                                                return '[ ' . implode(', ', $output) . ' ]';
                                                                                }
                                                case 'object':
                                                                $output = array();
                                                               
                                                                                foreach ($data as $key => $value) {
                                                                $output[] = Json::encode(strval($key)) . ': ' . Json::encode($value);
                                                                                }
                                                                               
                                                                                return '{ ' . implode(', ', $output) . ' }';
                                                                default:
                                                                                return 'null';
                                                }
                                }
                }
decode($json, $assoc = FALSE):
static public function decode($json, $assoc = FALSE) {
                                if (function_exists('json_decode')) {
                                                return json_decode($json);
                                } else {
                                                $match = '/".*?(?<!\\\\)"/';

                                                $string = preg_replace($match, '', $json);
                                                $string = preg_replace('/[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/', '', $string);

                                                if ($string != '') {
                                                                return NULL;
                                                }

                                                $s2m = array();
                                                $m2s = array();

                                                preg_match_all($match, $json, $m);
                                               
                                                foreach ($m[0] as $s) {
                                                                $hash = '"' . md5($s) . '"';
                                                                $s2m[$s] = $hash;
                                                                $m2s[$hash] = str_replace('$', '\$', $s);
                                                }

                                                $json = strtr($json, $s2m);

                                                $a = ($assoc) ? '' : '(object) ';
                                               
                                                $data = array(
                                                                ':' => '=>',
                                                                '[' => 'array(',
                                                                '{' => "{$a}array(",
                                                                ']' => ')',
                                                                '}' => ')'
                                                );
                                               
                                                $json = strtr($json, $data);
 
                                                $json = preg_replace('~([\s\(,>])(-?)0~', '$1$2', $json);
 
                                                $json = strtr($json, $m2s);

                                                $function = @create_function('', "return {$json};");
                                                $return = ($function) ? $function() : NULL;

                                                unset($s2m);
                                                unset($m2s);
                                                unset($function);

                                                return $return;
                                }
                }


Collections of opencart function

1 comment:

  1. Thank you for your work. Ist grat and usefull..
    I have a question ...
    How can I duplicate flat shipping module?
    I need two flat rate for my site?
    Thanks!

    ReplyDelete