示例#1
0
 /**
  * Updates an existing Race model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $sports = Sport::findAll(['status' => 'ACTIVE']);
     $categories = Category::find()->all();
     $c = [];
     foreach ($categories as $category) {
         $c[$category->id] = $category->name;
     }
     $picture = UploadedFile::getInstance($model, 'picture');
     $sponsor = UploadedFile::getInstance($model, 'sponsor');
     $attachment1 = UploadedFile::getInstance($model, 'attachment1');
     $attachment2 = UploadedFile::getInstance($model, 'attachment2');
     $last_picture = $model->picture;
     $last_attachment1 = $model->attachment1;
     $last_attachment2 = $model->attachment2;
     if ($model->load(Yii::$app->request->post())) {
         if ($_POST['pictureChanged'] == 'true') {
             // unlink('images/'.$last_picture);
             if ($picture != NULL) {
                 $name = date('Y_m_d_H_i_s_') . $picture->baseName . '.' . $picture->extension;
                 $model->picture = $name;
             }
         } else {
             $model->picture = $last_picture;
         }
         if ($_POST['sponsorChanged'] == 'true') {
             if ($sponsor != NULL) {
                 if ($picture != NULL) {
                     $model->sponsor = $name;
                 } else {
                     $model->sponsor = $last_picture;
                 }
             }
         } else {
             $model->sponsor = $last_picture;
         }
         if ($_POST['attachment1Changed'] == 'true') {
             if ($attachment1 != NULL) {
                 $name1 = date('Y_m_d_H_i_s_') . $attachment1->baseName . '.' . $attachment1->extension;
                 $model->attachment1 = $name1;
             }
         } else {
             $model->attachment1 = $last_attachment1;
         }
         if ($_POST['attachment2Changed'] == 'true') {
             if ($attachment2 != NULL) {
                 $name2 = date('Y_m_d_H_i_s_') . $attachment2->baseName . '.' . $attachment2->extension;
                 $model->attachment2 = $name2;
             }
         } else {
             $model->attachment2 = $last_attachment2;
         }
         if ($_POST['Race']['categories'] != '') {
             foreach ($model->categories as $i => $category) {
                 if (!array_search($category->id, $_POST['Race']['categories'])) {
                     $nt = Category::findOne($category->id);
                     $model->unlink('categories', $nt, true);
                 }
             }
             foreach ($_POST['Race']['categories'] as $i => $category) {
                 if (!Categories::findOne(['race_id' => $model->id, 'category_id' => $category])) {
                     $nt = Category::findOne($category);
                     $model->link('categories', $nt);
                 }
             }
         } else {
             $model->unlinkAll('categories', true);
         }
         if ($model->save()) {
             if ($picture != NULL) {
                 $picture->saveAs('img/carrera/' . $name);
             }
             if ($sponsor != NULL) {
                 if ($picture != NULL) {
                     $sponsor->saveAs('img/carrera/auspiciante/' . $name);
                 } else {
                     $sponsor->saveAs('img/carrera/auspiciante/' . $last_picture);
                 }
             }
             if ($attachment1 != NULL) {
                 $attachment1->saveAs('img/carrera/adjunto/' . $name1);
             }
             if ($attachment2 != NULL) {
                 $attachment2->saveAs('img/carrera/adjunto/' . $name2);
             }
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('update', ['model' => $model, 'sports' => $sports, 'categories' => $c]);
     }
 }
示例#2
0
 public function actionIndex()
 {
     $sports = Sport::findAll(['status' => 'ACTIVE']);
     return $this->render('index', ['sports' => $sports]);
 }