Jump to content

classes/cmscategory.php analyze


Kogkalidis

Recommended Posts

I try to understand how everything works

Please help (experienced dev please)

Row 97-something

 

 

public function add($autodate = true, $null_values = false)

{

$this->position = CMSCategory::getLastPosition((int)$this->id_parent);

$this->level_depth = $this->calcLevelDepth();

foreach ($this->name as $k => $value)

if (preg_match('/^[1-9]\./', $value))

$this->name[$k] = '0'.$value;

$ret = parent::add($autodate, $null_values);

$this->cleanPositions($this->id_parent);

return $ret;

}

 

public function update($null_values = false)

{

$this->level_depth = $this->calcLevelDepth();

foreach ($this->name as $k => $value)

if (preg_match('/^[1-9]\./', $value))

$this->name[$k] = '0'.$value;

return parent::update($null_values);

}

Link to comment
Share on other sites

In which parts ... each line of the code inside both method ?
I guess the variable name already clear enough to explain what 'they' did.

Before digging up more further ...

In Prestashop, an object usually have a definition properties.
public static $definition = array( ... ... ...);
Looking through this definition we will know about the required and optional properties of an object, also know what properties which are multi languages.
In Prestashop, we can creating a new object easily by creating a new classes file which extending the

"abstract class ObjectModelCore"
This way we don't have to write our classes file from the scratch, and with

public static properties $definition

we can defining what we want for the new object.

(required fields, multi languages fields, field validation, field type, etc)

CMSCategory.php
Is a classes file which handle CMS category object.
Looking through the public static $definition defined in this classes file, the required properties of this CMS Category object are :
$active, $id_parent, $name and $link_rewrite

$position and $level_depth are optional, because it can be defined (automatically) through the available values within this object.

Therefore inside the add method, both optional properties are defined with :

$this->position = CMSCategory::getLastPosition((int)$this->id_parent);
$this->level_depth = $this->calcLevelDepth();

@var  integer category position
public $position;
This is a properties to define in which position a CMS Category reside (integer value)

@var integer Parents number
public $level_depth;
This is a properties to define in which level a CMS Category reside (integer value)

Let say we are going to create new CMS Category "My New CMS Category"
Regarding to the required properties of this object, we only have to define at least 4 properties values to create new CMS Category.

So here we go ...

$my_new_cms_category = new CMSCategory();
$my_new_cms_category->active = 1; // set our new cms category as an active cms category
$my_new_cms_category->id_parent = 0; // set our cms category parent to Home category
$my_new_cms_category->name = ''; // since this properties is multi language, then we should defining the value according to the language that we use (by id_lang)
$my_new_cms_category->$link_rewrite = ''; // FOR SEO sake, we are going to define the value according to its name

// define the avaliable and active languages on the systems
$languages = Language::getLanguage();
foreach($languages as $lang)
{
    if ($lang['iso_code'] == 'en') {
        $my_english_var = 'My New CMS Category in english';
        $my_new_cms_category->name[$lang['id_lang'] = $my_english_var;
        // For link_rewrite, all spaces will be replaced with underscrore and then all uppercase letter will be change to lowercase
        $my_new_cms_category->link_rewrite = strtolower(str_replace(' ', '_', $my_english_var));
    }
    else {
        $my_other_lang_var = 'My New CMS Category in other language';
        $my_new_cms_category->name[$lang['id_lang'] = $my_other_lang_var;
        // For link_rewrite, all spaces will be replaced with underscrore and then all uppercase letter will be change to lowercase
        $my_new_cms_category->link_rewrite = strtolower(str_replace(' ', '_', $my_other_lang_var));
    }
}
// No need to worry about the other object properties because the classes file which handle this object will take care all of this things
// Finally we can create new CMS category using simple method '$object->add()'
$my_new_cms_category->add();

... to be continued  :rolleyes:

Edited by gonebdg - webindoshop.com (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...