문제

Im busy with an image moving function so im overriding some controller functions, and unfortuantly i required the items id for the image name so i changed form save() to postSaveHook() as i was not able to get the item id in save() but now im facing another problem i cant set form data to the newly renamed image.

Here's the code:

public function postSaveHook($model, $validData){
    $item = $model->getItem();
    $id = $item->get('id');

    $path = JPath::clean(JPATH_SITE. DS ."images". DS ."menu_slider". DS );

    $input=JFactory::getApplication()->input;
    $input->get('jform', NULL, NULL);

    $src_image = $this->moveOriginal($path,$id);

    $imageTest = $this->findImages($src_image);

    if(!empty($imageTest)){
        foreach($imageTest as $images){
            $this->createImageSlices($images,$src_image,$path);
        }
    }else{
        echo 'all images are there';
    }
    /*this part no longer works*/
    $data = JRequest::getVar( 'jform', null, 'post', 'array' );
    $data['image'] = 'images'.DS.'menu_slider'.DS.'original'.DS.$src_image;
    $input->post->set('jform',$data);

    return parent::postSaveHook($model, $validData);
}

is there anyway i can save the data from this? or if i revert back to save, how would i get the id?

Any Help Greatly Appreciated.

도움이 되었습니까?

해결책

I tried different ways and the absolute safest way for me was to add the following code in the model

class DPCasesModelCase extends JModelAdmin {

    public function save($data) {
        new EventHandler(JDispatcher::getInstance(), $this);
        return parent::save($data);
    }
}

class EventHandler extends JEvent {

    private $model = null;

    public function __construct(&$subject, $model) {
        parent::__construct($subject);

        $this->model = $model;
    }

    public function onContentChangeState($context, $pks, $value) {
        if ($context != 'com_dpcases.case' && $context != 'com_dpcases.form') {
            return;
        }

        if (! is_array($pks)) {
            $pks = array($pks);
        }
        foreach ( $pks as $pk ) {
            $this->dowork($this->model->getItem($pk), 'edit');
        }
    }

    public function onContentAfterSave($context, $object, $isNew) {
        if ($context != 'com_dpcases.case' && $context != 'com_dpcases.form') {
            return;
        }

        $this->dowork($object, $isNew ? 'create' : 'edit');
    }

    private function dowork($object, $action) {
        ...
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top