Wednesday, July 8, 2009

Block Calling in CMS & Template Files

Dynamic Block Call in CMS :

{{block type="banner/banner" template="banner/banner.phtml"}}

{{block type="contest/contest" template="contest/contest_home.phtml"}}

By placing the code for particular block as above, whole content return by that particular phtml file will be display in CMS page.

Static Block Call in CMS :


{{block type="cms/block" block_id="advertise-management"}}

As client wants to change regularly some part of home page, If we place that particular code in cms page only, then Client needs to find that particular portion of code in whole code. Rather than this, we can create static block for that particular portion of code & place that block as shown above.

Here block_id will be identifier , the field you have entered when creating static block. Identifier must be unique for every static block created.



Static Block Call in Template Files (.phtml files) :


getLayout()->createBlock('cms/block')->setBlockId('tshirtwearcontesttext')->toHtml() ?>

To call any static block to any template file , you need to write just above code & in setBlockId('blockIdentifier'), you need to pass the static block identifier, and your content will display in that particular page.

Tuesday, July 7, 2009

Validation of various fields

For validation of any field in admin & front end, all fileds are having css class.

1.<input type="text" class="required-entry validate-namemaxlength input-text" title="First Name" value="" name="firstname" id="firstname"/>
2.<select class="validate-select" title="Gender" name="gender" id="gender">
<option value="">Select</option>
<option value="123">Male</option>
<option value="124">Female</option>
</select>
3.<input type="text" class="required-entry validate-zip validate-zipmaxlength input-text" id="zip" title="Zip/Postal Code" value="" name="postcode"/>
4.<input type="password" class="required-entry validate-password input-text" title="Password" id="password" name="password"/>
5.<input type="password" class="required-entry validate-cpassword input-text" id="confirmation" title="Confirm Password" name="confirmation"/>
6.<textarea cols="15" rows="2" class="required-entry validate-minlength validate-maxlength required-entry textarea" style="width: 450px; height: 500px;" title="Content" name="content" id="content">Best of tees on Tshirt Crack</textarea>


You can see the number of examples of different type of fields.

Basic Idea is , you need to make validation class in validation.js file for any new validation type.
After that need to add that class to particular field. If multiple validation is there, i.e. Field is required, Field should have minimum length of 10 character, Field should have maximum length of 100 character, Field must follow some regex(regular expression) pattern.

Then priority can be set like, First need to check required, second minimum length, third maximum length, forth regex pattern matching.

Steps for creating new validation class ,

Step1:
Go to file, js/prototype/validation.js in main magento folder.


Step2:
In Validation.addAllThese() , all validation classes are defined.

Validation.addAllThese([
['validate-select', 'Please select an option.', function(v) {
return ((v != "none") && (v != null) && (v.length != 0));
}],
['required-entry', 'This is a required field.', function(v) {
return !Validation.get('IsEmpty').test(v);
}],
|
|
|
|
[.....................................................
..],
[.....................................................
..] ] )
Step3:
If you want to add new validation, you need to add only,

['validate-titlemaxlength', 'Maximum 100 characters are allowed.', function (v, elm) {

if(v.length > 100)
{
return false;
}
else
{
return true;
}
}],

Here validate-titlemaxlength is your class which needs to be placed in css class of particular field. 'Maximum 100 characters are allowed.' is error message.

In function (v, elm) , v is the value of field and elm is object of element by which we can access any property of field.


Steps to place new class Or modify existing allocation of validation classes at frontend,

Step1 :
For any form field in front end go to the template file of that particular module.

i.e. app/design/frontend/default/default/template/customer/form/register.phtml

Step2 :
ex, <div class="input-box">
<label for="zip"><?php echo $this->__('Zip/Postal Code') ?> <span class="required">*</span></label><br/>
<input type="text" name="postcode" value="<?php echo $this->htmlEscape($this- >getFormData()->getPostcode()) ?>" title="<?php echo $this->__('Zip/Postal Code') ? >" id="zip" class="required-entry validate-zip validate-zipmaxlength input-text" />
</div>

In above code in class of zip code , required-entry validate-zip validate-zipmaxlength is there.

Now, if you want to add minimum length validation i.e. validate-zipminlength, then create new class in validation.js as mentioned before and then just append to existing classes by space in field according to priority , i.e. required-entry validate-zip validate-zipminlength validate-zipmaxlength .


Steps to place new class Or modify existing allocation of validation classes at backend,

Step1:
Go to app/code/core/Mage/Adminhtml/Block/ModuleName/Edit/Tab/Form.php,

$fieldset->addField('title', 'text', array(
'label' => Mage::helper('contest')->__('Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',

Step2: ));
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('contest')->__('Title'),
'class' => 'required-entry validate-title',
'required' => true,
'name' => 'title',
));

Redirection of URL

Mostly, this customization can be used in the case, in which user is on any page of site & after login, User need to redirect to that particular page.

Steps for this customization,

Step1 :
Go to the controller of every page.
For core module, app/code/core/Mage/moduleName/controllers/controllerName.php,
i.e. app/code/core/Mage/Catalog/controllers/ProductController.php.
For custom (newly created) module,
i.e. app/code/local/Mage/Contest/controllers/IndexController.php

Step2 :
In controller file every action function , actionNameAction(), i.e. indexAction(), detailAction(), postAction() etc., need to add following line.

Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()- >getRequestUri());

Saturday, June 13, 2009

Keep the track, who has attained the order in last?

1.You need to do following steps to add Updated By Field in Order Section.
Go to the database :
Our Main table for order is sales_order , and for Order we are having entity_type_id 11 in our database table eav_attribute.
So, I have added one attribute 'updated_by' in eav_attribute table with entity_type_id 11.
Now, go to the table sales_order and add one column 'updated_by' (type is varchar, length can be any)

Go to file app/code/core/Mage/Sales/Model/Order.php
In this file , all operations for Order can be done.
$this->setData('field_name','field_value'); , this function has been called when you want to update or add any record.
So, to set any value to any field, you can use setData() function.
Here , I have created one function setUpdatedAdmin() to update 'updated_by' field.
Function is like ,
public function setUpdatedAdmin()
{
$user = Mage::getSingleton('admin/session')->getUser();
$this->setData('updated_by', $user->getUsername());
return $this;
}

Mage::getSingleton('admin/session')->getUser(), get all details of current logged in user in admin panel. $user->getUsername() retrieve current logged in user's name, this is magento's default function. By using setData() (magento's default), we have set the value for updated_by field.
Now, the question is where to call this function ? , so on every operation for Order , _beforeSave() function of app/code/core/Mage/Sales/Model/Order.php has been called every time.
So, we can use setUpdatedAdmin() in _beforeSave() function.

protected function _beforeSave()
{
parent::_beforeSave();
$this->_checkState();
if (!$this->getId())
{
$store = $this->getStore();
$name = array($store->getWebsite()->getName(),$store->getGroup()->getName(),$store->getName());
$this->setStoreName(implode("\n", $name));
}
$this->setUpdatedAdmin();
return $this;
}

After some R & D, I have found getUser() for current user, & setData() for set field value, other logic is mine :).

Friday, June 5, 2009

Magento Debugging - It's like an adventure sometime

I am working on magento since last 6 months.
I really enjoyed whatever i have learned and done in magento for some sort of customization.
I was just thinking that, many developers are there who are facing similar problems for customization and changes in magento.

So, Here I am going to share every experience in magento that can help you and me both.