Jul 3, 2013

Magento: How to minified your html content?

I know its very bad idea to modify the core code, but sorry I don't have time to test override the class.


To do this you need to modify Mage_Core_Block_Template class and its fetchView method.

public function fetchView($fileName)
{
  // ***********
  // Some other code up
  // *********************

  // This is the in the bottom code
  if (Mage::app()->getStore()->getCode() == 'admin'){
     // Admin must not to minify since, there was lot on inline comment(//)
     // which generate javascript error.
     return $html;
  }else{
    // Instead of just returning the html data, you need to
    //  remove the  un-needed extra space and new lines.
    $html = preg_replace('/\s+/', ' ', trim($html)); // remove new line

    // Remove space on every start on a tag </script> <div> become </script> <div>
    $html = preg_replace('/ </', '<',$html); // For more compression only you can skip this line.

    return $html; 
  }


}







WARNING:

This is not applicable if you have javascript in your template will an inline comment double slash(//). Since the code become one line, whole javascript after the // become comment. I know you know that. Suggestion pleas use the group comment(/*** Your comment inside */) or move all you javascript in a js file. :)

Cheerr

Jul 2, 2013

Magento: core_file_storage' doesn't exist'

This kind of error mostly happen if you just copy a database and checkout the source that you team is working for..

So here the solution I found, create to tables manually.
core_directory_storage
CREATE TABLE `core_directory_storage` (
  `directory_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL DEFAULT '',
  `path` VARCHAR(255) NOT NULL DEFAULT '',
  `upload_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `parent_id` INT(10) UNSIGNED NULL DEFAULT NULL,
  PRIMARY KEY (`directory_id`),
  UNIQUE INDEX `IDX_DIRECTORY_PATH` (`name`, `path`),
  INDEX `parent_id` (`parent_id`),
  CONSTRAINT `FK_DIRECTORY_PARENT_ID` FOREIGN KEY (`parent_id`) 
  REFERENCES `core_directory_storage` (`directory_id`) ON UPDATE     CASCADE ON DELETE CASCADE
)
COMMENT='Directory storage'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
core_file_storage
CREATE TABLE `core_file_storage` (
  `file_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `content` LONGBLOB NOT NULL,
  `upload_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `filename` VARCHAR(255) NOT NULL DEFAULT '',
  `directory_id` INT(10) UNSIGNED NULL DEFAULT NULL,
  `directory` VARCHAR(255) NULL DEFAULT NULL,
  PRIMARY KEY (`file_id`),
  UNIQUE INDEX `IDX_FILENAME` (`filename`, `directory`),
  INDEX `directory_id` (`directory_id`),
  CONSTRAINT `FK_FILE_DIRECTORY` FOREIGN KEY (`directory_id`) 
    REFERENCES `core_directory_storage` (`directory_id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COMMENT='File storage'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT

Jun 20, 2013

Java: How to get cookie from HttpURLConnection

Here the simple code to get the site response cookie.

URL urlReq = new URL("http://www.google.com");

conn = (HttpURLConnection) urlReq.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(8000);
conn.setConnectTimeout(8000);

// Start building other http request parameter
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Firefox/21.0");
conn.setRequestProperty("Accept-Language","en-us,en;q=0.5");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Connection", "close");

conn.setDoOutput(true);
conn.setDoInput(true);

conn.connect();
resCode = conn.getResponseCode(); 
if (resCode==HttpURLConnection.HTTP_OK){
 for(Map.Entry<String, List<String>> headers : conn.getHeaderFields().entrySet()){
 if (headers.getKey().equals("set-cookie")){
      logger.info("\t==>"+headers.getKey());
      for(String hval : headers.getValue()){
       logger.info("\t\t==>"+hval);
      }
 }else{
  logger.info("\t==>"+headers.getKey() +" = "+conn.getHeaderField(headers.getKey()));
 }
}





You can also use
conn.getHeaderField("Set-Cookie")
However, the bad thing is you can one get one cookie, I suggest to use
conn.getHeaderFields()

Happy Reading

Jun 5, 2013

How to determine the user browser version in html?

Since this code, is only application in IE(Internet Explorer). Maybe the question is, How to determine the browser version in html.

This this check is the user user IE Browser, whatever the version.
<!--[if IE]>
Place content here to target all Internet Explorer users.
<![endif]-->

This was opposite behavior on the first item. "This is only run when the browser is NOT Internet Explorer".
<![if !IE]>
Place content here to target all users not using Internet Explorer.
<![endif]>

This will run if the browser version is greater than or equal to 8.
<!--[if gte IE 8]>
Place content here to target users of Internet Explorer 8 or higher.
<![endif]-->

A similar example above that only runs when the browser version is less than 7 (i.e. 6 or lower).
<!--[if lt IE 7]>
Place content here to target users of Internet Explorer 6 or lower (less than 7).
<![endif]-->

Conclusion:
The code/conditions are only run when running on IE browser, otherwise the browser will detect/treat as normal comment.