Question

I have a cropping tool component and helper which work on cake 2.x but now I need to use this cropping tool into one of the older project in cakephp 1.3. How should I do this?

The component:

<?php
    App::uses('Component', 'Controller');
    class JcropComponent extends Component {

        public $components = array('Session');

        public $options = array(
            'overwriteFile' => true,
            'boxWidth' => '940'
        );

        /**
         * Constructor
         */
        public function __construct(ComponentCollection $collection, $options = array()) {
            parent::__construct($collection,$options);
            $this->options = array_merge($this->options, $options);
        }
?>

The Helper

<?php
App::uses('AppHelper', 'View/Helper');

class JcropHelper extends AppHelper {
    public $helpers = array('Html','Form','Js');

    public $options = array(
        'tooltip' => true,
        'boxWidth' => '940'
    );


    public function __construct(View $view, $options = null) {
        parent::__construct($view,$options);
        $this->options = array_merge($this->options, $options);
    }
?>

I tried to change it into this, it can show the image, but how can I merge the options array? the one where __construct ($options = array())

<?php
    class JcropComponent extends Component {

        var $components = array('Session');

        public $options = array(
            'overwriteFile' => true,
            'boxWidth' => '940'
        );

    //public function initialize(&$controller, $settings = array()) {
    //  $this->controller =& $controller;
        //parent::__construct($collection,$options);
        //$this->options = array_merge($this->options, $options);
    //}
?>


<?php
class JcropHelper extends AppHelper {
    var $helpers = array('Html','Form','Js');

    public $options = array(
        'tooltip' => true,
        'boxWidth' => '940'
    );


    public function __construct($settings = null) {
        //parent::__construct($view,$options);
        //$this->options = array_merge($this->options, $options);
    }
?>
Was it helpful?

Solution

Components in 1.3 don't use the constructor for settings

The first significant problem you have is: the way components receive settings changed between major versions.

in 1.3:

//Component(Collection) Class
$component->initialize($controller, $settings);

in 2.x:

//ComponentCollection class
new $componentClass(ComponentCollectionObject, $settings);

Therefore the way to make the component work the same way in 1.3 - is to define the initialize method.

Helpers have a different constructor

A similar change was made to helpers:

In 1.3:

//View class
new $helperCn($options);

In 2.x:

//HelperCollection class
new $helperClass(ViewObject, $settings);

In this case it should be more obvious - any overridden method should have the same method signature as the parent class. Therefore: change the helper to have the same expected arguments in the constructor

Warning: extends Component

In 1.3, components do not extend Component, they extend Object. Component is the class which acts as a collection in 1.3, extending it will cause unexpected and undesired behavior (i.e. it'll likely cause unexpected "random" warnings and fatal errors). As such you should change the component class to be analogous to all other components, extending Object (or quite simply - not extending component).

If this class is something you use and maintain in various projects, it'd be a good idea to extract the main functionality into a standalone class, and implement only a thin wrapper class (component/behavior) to interface with it. In this way any changes made to the main functionality can be leveraged irrespective of the version of cake - or any other framework - used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top