CakePHP "action" with various functions

2

I have a few points about how I can improve this "add action" in my "controller":

Some descriptions: In the system the user can upload a Product to a single form, in the system a Product is composed of Product , ProductFeatures e entidades ProductMedias ( and 3 different tables in database).

Question: In the same action ( add Controller method Products ) I do several tasks (described below) what would be the best way to separate concepts and operations?

1st: I create a Product entity and fill it with form data and custom data, then I insert it into the database.

2nd: I create an array ProductFeatures (using a method in a component) Each position in the array is another array with ProductFeatures Key-Value

3rd: I created a ProductFeatures array with entities (created from the array above) and then massively entered all in the database (using a method in a component).

4th: Send the images sent in the form to a suitable folder (using a method in a component).

5th: I created an array with the data of Pictures and then I treat them to the correct format, then create an array of entities (each entity was an array position) then I massively insert them all into the database (using a method in a component).

public function add() 
{       
    if ($this->request->is('post')) {

        //Save Product entity
        $product = $this->Products->newEntity();
        $product = $this->Products->patchEntity($product, $this->request->data);
        $product->sub_category_id = 18;
        $product->store_id = 1;
        $productSaved = $this->Products->save($product);

        if($productSaved)
        {
            //Save ProductFeatures entities
            $featuresArray = $this->Insert->getFeatuesArray($this->request->data);
            $featuresEntities = $this->Insert->createMassFeaturesEntities($featuresArray, $productSaved['id']);
            $this->Insert->insertMassEntities($featuresEntities, 'ProductFeatures');

            //Upload pictures to folder in server
            $ROOT_PATH = dirname(ROOT) . DS;
            $PRODUCTS_IMAGES_FOLDER = $ROOT_PATH . 'ShoppingResources' . DS . 'img' . DS . $productSaved['id'];
            $imagesUploaded = $this->UploadFile->uploadFiles($PRODUCTS_IMAGES_FOLDER, $this->request->data['file']);

            //Create and Upload thumbnail to folder in server
            $outputThumb = str_replace($PRODUCTS_IMAGES_FOLDER, $ROOT_PATH . 'ShoppingResources' . DS . 'thumb', $imagesUploaded[0]['url']);
            $imageResized = $this->UploadFile->resizeImage([
                'input' => $imagesUploaded[0]['url'], 'output' => $outputThumb, 'width' => 250, 'height' => 250, 'mode' => 'stretch'
            ]);

            //Prepare image data array to be transformed into entity
            $thumbUploaded['url'] = str_replace($ROOT_PATH, 'http://localhost/PROJETOS/', $outputThumb);
            $thumbUploaded['url'] = str_replace('\', '/', $thumbUploaded['url']);
            $thumbUploaded['media_type_id'] = 3;

            //Save Media entity (thumbnail)
            $mediaEntity = $this->Insert->createMediaEntity($thumbUploaded, $productSaved['id']);
            TableRegistry::get('Medias')->save($mediaEntity);

            //Prepare image data array to be transformed into entities
            $imagesUploaded = $this->Insert->addKeyValueToArray($imagesUploaded, 'media_type_id', 1);
            $imagesUploaded[0]['media_type_id'] = 2;
            $imagesUploaded = $this->Insert->replaceArrayValue($imagesUploaded, 'url', 'http://localhost/PROJETOS/', $ROOT_PATH);
            $imagesUploaded = $this->Insert->replaceArrayValue($imagesUploaded, 'url', '/', '\');

            //Save Medias entities
            $mediasEntities = $this->Insert->createMassMediasEntities($imagesUploaded, $productSaved['id']);
            $this->Insert->insertMassEntities($mediasEntities, 'Medias');
        }
    }
}
    
asked by anonymous 02.12.2015 / 17:13

0 answers