<?php
namespace backend\controllers;

use yii\rest\Controller;
use common\models\Image;
use backend\models\UploadForm;
use yii\web\UploadedFile;
USE Yii;

class UploadController extends  Controller
{

	
	

//     public function verbs()
//     {
//         $verbs = [];
//         $verbs[ "upload" ] = ['POST' ];
//         return $verbs;
//     }

    public function actionUpload()
    {
    	$model = new UploadForm();
    	$resp = [];
    	
    	if ( $model->load(Yii::$app->request->post()) && $model->validate()) {
    		
    		
    		$model->image =  UploadedFile::getInstance($model, 'image');
    		
    		/**save into frontend/web/uploads/profile*/
    		$path = \common\components\Image::upload($model->image,'profile');
	
	        $image = new Image();
	        $image->path = $path;
	        $image->save();
        
			/* the result object that is sent to client*/

			$resp['ok'] = 1;
			$resp['id_image'] = $image->id_image;
    	}else{
			$resp['ok'] = 0;
    		$resp['message'] = print_r($model->errors,true);
    	}
    	
    	return $resp;
    }
}