/**
  * Creates a new ActivityImage model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new ActivityImage();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function actionLink()
 {
     /*$categories = Category::find()->all();
     
             foreach ($categories as $category){
                 $names = explode(';',$category->id_classification);
                 $array = array();
                 foreach ($names as $id=>$name){
                     $classification = CategoryClassification::find()->where(['name' => $name])->one();
                     if ($classification){
                         $array[] = $classification->id;
                         echo 'added to array '. $name . '<br>';
                     }
                 }
                 $temp = implode(";", $array);
                 $category->id_classification = $temp;
                 $category->save();
             }*/
     $categories = Activity::find()->all();
     foreach ($categories as $category) {
         $images = explode(';', $category->images);
         foreach ($images as $id => $name) {
             $categoryImage = ActivityImage::find()->where(['name' => $name, 'id_activity' => $category->id])->one();
             if ($categoryImage) {
             } else {
                 $categoryImage = new ActivityImage();
                 $categoryImage->id_activity = $category->id;
                 $categoryImage->name = $name;
                 $categoryImage->save();
             }
         }
     }
     /*$categories = Category::find()->all();
     
             foreach ($categories as $category){
                 $names = explode(';',$category->id_classification);
                 foreach ($names as $id=>$name){
                     $classification = CategoryClassification::find()->where(['name' => $name])->one();
                     if ($classification){
                         //echo 'yes=' . $name . '<br>';
                     }else{
                         echo 'creating =' . $name . '<br>';
                         $classification = new CategoryClassification();
                         $classification->name = $name;
                         $classification->save();
                     }
                 }
     
                 //var_dump($names);
             }*/
     /*$types = ActivityType::find()->all();
     
             foreach ($types as $type) {
                 $activities = Activity::find()->where(['like', 'id_type', $type->name])->all();
                 foreach ($activities as $activity) {
                     $activity->id_type = $type->id;
                     $activity->save();
                 }
             }*/
     /*$activities = Category::find()->where(['id_where' => ''])->all();
       foreach ($activities as $activity) {
           $activity->id_where = null;
           $activity->save();
       }*/
     /*$activities = Activity::find()->where(['id_type' => ''])->all();
       foreach ($activities as $activity) {
           $activity->id_type = null;
           $activity->save();
       }*/
     /*$where = CategoryWhere::find()->all();
     
             foreach ($where as $place) {
                 $categories = Category::find()->where(['like', 'id_where', $place->name])->all();
                 foreach ($categories as $category) {
                     $category->id_where = $place->id;
                     $category->save();
                 }
             }
     
             $difficulties = CategoryDifficulty::find()->all();
     
             foreach ($difficulties as $difficulty) {
                 $categories = Category::find()->where(['like', 'id_difficulty', $difficulty->name])->all();
                 foreach ($categories as $category) {
                     $category->id_difficulty = $difficulty->id;
                     $category->save();
                 }
             }
     
             $trainings = CategoryTraining::find()->all();
     
             foreach ($trainings as $training) {
                 $categories = Category::find()->where(['like', 'id_training', $training->name])->all();
                 foreach ($categories as $category) {
                     $category->id_training = $training->id;
                     $category->save();
                 }
             }*/
     return $this->render('link', []);
 }
 /**
  * @param integer $id
  * @return boolean
  */
 public function upload($id)
 {
     if ($this->validate()) {
         $name = ActivityImage::find()->where(['id_activity' => $id])->max('name');
         if (!$name) {
             $counter = 0;
         } else {
             $array = explode(".", $name);
             $counter = $array[0] + 1;
         }
         $folder = Yii::getAlias('@activity/' . $id);
         if (!file_exists($folder)) {
             BaseFileHelper::createDirectory($folder);
         }
         foreach ($this->imageFiles as $file) {
             $file->saveAs($folder . '/' . $counter . '.jpg');
             Image::thumbnail(Yii::getAlias('@activity/' . $id . '/' . $counter . '.jpg'), 120, 120)->save(Yii::getAlias('@activity/' . $id . '/thumb' . $counter . '.jpg'), ['quality' => 80]);
             $newImage = new ActivityImage();
             $newImage->id_activity = $id;
             $newImage->name = $counter . '.jpg';
             $newImage->save();
             $counter++;
         }
         return true;
     } else {
         return false;
     }
 }
 public function actionPost()
 {
     $request = Yii::$app->request;
     $url_oficial = $request->post('officialURL');
     $name = $request->post('TITLE');
     $text = $request->post('DESCRIPTION');
     $imageURL = $request->post('imageURL');
     $noNoNo = str_replace(chr(10), "", $imageURL);
     $noNoNo = str_replace(chr(13), "", $noNoNo);
     $images = explode(';', $noNoNo);
     $street = $request->post('ADDRESS');
     $emails = $request->post('Email');
     $phones = $request->post('Phone');
     $activityType = $request->post('activityType');
     $activityClasses = $request->post('activityClasses');
     $username = $request->post('username');
     $userExists = User::find()->where(['username' => $username])->one();
     if ($userExists) {
         $activity = new Activity();
         $activity->url_official = $url_oficial;
         $activity->name = $name;
         $activity->text = $text;
         $activity->street = $street;
         $activity->emails = $emails;
         $activity->phones = $phones;
         $activityName = explode("|", $activityType);
         $id_type = ActivityType::find()->where(['name' => $activityName[1]])->one();
         $activity->id_type = $id_type->id;
         $id_category = Category::find()->where(['name' => $activityClasses])->one();
         $activity->id_category = $id_category->id;
         $now = time();
         $activity->created_at = $now;
         $activity->updated_at = $now;
         $activity->url_info = $imageURL;
         $activity->save();
         $id = $activity->id;
         $counter = 0;
         $path = Yii::getAlias('@activity/' . $id);
         BaseFileHelper::createDirectory($path);
         foreach ($images as $image) {
             if (copy($image, $path . '/' . $counter . '.jpg')) {
                 $newImage = new ActivityImage();
                 $newImage->id_activity = $id;
                 $newImage->name = $counter . '.jpg';
                 $newImage->save();
                 $counter++;
             } else {
                 echo 'cant copy image';
             }
         }
         return true;
     } else {
         return false;
     }
 }