Tuesday, December 28, 2010

Date Magento in any format

Mage::app()->getLocale()->date(strtotime($Model->getCreatedAt()), null, null, false)->toString('dd/MM/yyyy')


Monday, December 20, 2010

Catalog Search Index refresh

<?php
require_once 'app/Mage.php';
umask( 0 );
Mage :: app( "default" );
Mage::log("Started Rebuilding Search Index At: " . date("d/m/y h:i:s"));
$sql = "truncate catalogsearch_fulltext;";
$mysqli = Mage::getSingleton('core/resource')->getConnection('core_write');
$mysqli->query($sql);
$process = Mage::getModel('index/process')->load(7);
$process->reindexAll();
Mage::log("Finished Rebuilding Search Index At: " . date("d/m/y h:i:s"));

Friday, December 17, 2010

How to add days, weeks, months to any date ?

$date = date("Y-m-d");// current date

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");

Tuesday, December 14, 2010

/var/empty/sshd must be owned by root and not group or world-writable

Check the ownership and permissions on /var/empty and /var/empty/sshd.
They must be owned by root (chown root /var/empty; chown root /var/empty/sshd) and not be writable by anyone except root.

chmod go-w /var/empty/sshd;
chmod go-w /var/empty

Friday, November 12, 2010

Magento: Use cache

$cacheKey = 'CODE_CODE'.Mage::app()->
getStore()->getId();
        if (Mage::app()->useCache('config') && $collection = Mage::app()->loadCache($cacheKey)) {
            $prod = unserialize($collection);
        } else {
            $prod = Mage::getModel()->getCollection;
            ....
           
            if (Mage::app()->useCache('config')) {
                Mage::app()->saveCache(serialize($prod), $cacheKey, array('config'));
            }
        }

Friday, October 29, 2010

Magento: Get url stores

$stores = Mage::getModel('core/store')->getCollection();
foreach(  $stores as $store ){
            $url = Mage::getModel('core/url') ->setStore($store)->getUrl('');
            echo $url."\n";
}

Magento: Request to do in case of duplication in the flat catalog

SELECT entity_id, count( * )
FROM catalog_category_flat_store_x
GROUP BY entity_id
ORDER BY count( * ) DESC
LIMIT 0 , 30

PHP: 301 redirect

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>

Wednesday, October 27, 2010

fileclass: cannot open '/etc/mail/local-host-names sendmail

fileclass: cannot open '/etc/mail/local-host-names sendmail
problem

sendmail is not working. Giving the error '/etc/mail/sendmail.cf: line 91: fileclass: cannot open '/etc/mail/local-host-names': World writable directory'.

Solution

1). Edit the file /etc/mail/sendmail.cf and add -o ('oh') to line 91 and 588 that is change Fw/etc/mail/local-host-names to Fw-o /etc/mail/local-host-names.

2). Similarly edit /etc/mail/submit.cf and add -o to line 544( i think it may differ) that is change Ft/etc/mail/trusted-users to Ft-o /etc/mail/trusted-users.

3). save both files and restart sendmail

Tuesday, October 26, 2010

Centos: Restore Root password

1. Start your computer
2. At the GRUB prompt, choose the line on your linux and press e
3. Select the second line (the one that starts with kernel ...) with the cursor and press e again to edit it also.

4. Go to the end of the line and add the word single (this also works with the letter s or the number 1 on most distributions, the choice is yours)

5. Press b to boot with these new parameters

The system will boot into single user mode and offer you a command prompt as root without asking any password.

You can then choose a new password and reboot the
computer.

Wednesday, October 20, 2010

Magento: Use Filter template

            $filter = Mage::getModel('core/email_template_filter');
            $vars = array( 'value'=>'toto');
            $filter->setVariables( $vars );
            $filterdResult = $filter->filter( '  Text Text Text Text Text {{var value}} Text Text Text Text   ' );

Tuesday, October 19, 2010

Magento: .htaccess 301 Redirect

Redirect 301 /oldpage.html http://www.yoursite.com/redirectpage.html
Redirect 301 /redirectpage2.html http://www.yoursite.com/folder/

Magento: RESET MAGENTO FILE PERMISSIONS VIA SSH

find . -type f -exec chmod 644 {} \;
   find . -type d -exec chmod 755 {} \;
   chmod o+w var var/.htaccess includes includes/config.php app/etc
   chmod 550 pear
   chmod -R o+w med

Magento : How to show cart total

<?php getQuote();
$total = $quote->getGrandTotal();
// format total in order to have a user friendly price
$total = $this->helper('checkout')->formatPrice($total); ?>

Monday, October 18, 2010

Function to download file with Magento

In the controller use this function

    protected function _sendFile($pdfPath) {
        if (! is_file ( $pdfPath ) || ! is_readable ( $pdfPath )) {
            throw new Exception ( );
        }
        $this->getResponse ()
                    ->setHttpResponseCode ( 200 )
                    ->setHeader ( 'Pragma', 'public', true )
                    ->setHeader ( 'Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true )
                    /* View in browser */
                    //->setHeader ( 'Content-type', 'application/pdf', true )
                    /*  Download */
                    ->setHeader ( 'Content-type', 'application/force-download' )
                    ->setHeader ( 'Content-Length', filesize($pdfPath) )
                    ->setHeader ('Content-Disposition', 'inline' . '; filename=' . basename($pdfPath) );
        $this->getResponse ()->clearBody ();
        $this->getResponse ()->sendHeaders ();
        readfile ( $pdfPath );
        //exit(0);
    }

The passed parameter is the path of the File ( here i used this function to download a PDF file )

Friday, October 15, 2010

Instalers: CGV Model

This instaler is used to create a new CGV ( General Sales Conditions ) : 
 
$text = <<<TXT

text here

TXT;


$model = Mage::getModel('checkout/agreement');
$model->setName ();
$model->setContent ( $text );
//$model->setContentHeight ();
$model->setCheckboxText ();
$model->setIsActive (1);
$model->setIsHtml (1);

$model->save();

[URL] Current and home Url

To get the current url with his parameters use: 
Mage::helpet('core/url')->getCurrentUrl()

Fo the home, use: 
Mage::helpet('core/url')->getHomeUrl()


Tuesday, October 12, 2010

Show / Hiden rows of table (Jquery)

For show hidden rows of a table, the toggle function of Jquery freamwork can be used like this
    jQuery(".link-view").toggle(
              function () {
                   // Show treatment
              },
              function () {
                  // hidden treatment
              }
    );

Tuesday, September 28, 2010

Crontab: 3- Generate log file

To collect the cron execution execution log in a file :
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

Crontab: 2- file

Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
*     *     *   *    *        command to be executed

|     |     |   |    +----- day of week (0 - 6) (Sunday=0) 

|     |     |   +------- month (1 - 12) 

|     |     +--------- day of        month (1 - 31)

|     +----------- hour (0 - 23)

+------------- min (0 - 59) 
Example:
30     18     *     *     *         rm /home/someuser/tmp/*

Crontab: 1- Commands

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon.

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e     Edit your crontab file, or create one if it doesn't already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)


Monday, September 27, 2010

5 Cool PHP Tricks You May Not Know

Over the years I've come across some useful tricks in PHP that are not obvious, but are worth mentioning. This is not meant to be a comprehensive list of all the useful tricks that you can do with PHP.

1. Count Characters in a String
To do this, I've usually just used the function strlen(). However, there is a faster method. Take a look at the following code example:
$string = 'testing';

if(isset($string[6]))
 echo "The string '$string' is at least 7 characters long.";
else
 echo "The string '$string' is less than 7 characters long.";
You treat the $string value like an array by passing an integer value to isset(). If that number plus one is greater than or equal to the number of characters in the string, it will return true. You need to add one because it is zero based.

2. Use PHP Echo like a Function
I've always thought that if you wanted to concatenate strings with echo, you needed to use periods. But you can actually treat echo like a function and use commas instead (it is also faster). Take a look at the following code:
$string1 = 'test-string1';
$string2 = 'test-string2';
$string3 = 'test-string3';

echo 'String #1: ', $string1, '
';
echo 'String #2: ', $string2, '
';
echo 'String #3: ', $string3, '
';

3. Use Single Quotes When Possible
By using single quotes instead of double quotes, you save PHP from having to parse your string for variables. It not only is faster, but I find it more programmer-friendly because it is easier to find variables in your code.
Also, when referencing an array that has a string index, always use single quotes. This prevents PHP from having to figure out exactly what you were trying to say.

4. PHP Variable Variables
There have been several cases when I needed to access a dynamic variable (where the variable name changed). You can do this easily in PHP by using what is called Variable Variables. Take a look at this example:
$var1 = 'nameOfVariable';
$nameOfVariable = 'This is the value I want!!!';

echo $$var1;
5. PHP Output Buffering
I have come across cases where something was being output to the screen that I didn't want (at least at that point, or maybe not at all).
A common example of this is if you have a function or a script that echoes out a string, but you are using the code in that instance where you don't want it to print to the screen at that point (when using a template system or framework system, for example). In this case you would still want to be able to reuse the code, but just prevent anything from being printed out at that exact point.
This will make more sense if you look at this simple example:
ob_start();

echo 'Print to the screen!!!';
$getContent = ob_get_contents();

ob_end_clean();

// Do whatever you want...

// Do something with the printed content (only if you want)...
echo 'Now: ' . $getContent;

Action with Json return

If you need to create an action in Magento with content Json as return, you can use this procedure/code :

        $reponse =  new Varien_Object( );       
        ...
        $reponse->setTimeserver( Mage::getModel('core/locale')->storeTimeStamp(   Mage::app()->getStore() ) );
         ... ... ...
        $this->getResponse()->setBody ( $reponse->toJson() );
Powered by Blogger.