Ejemplo n.º 1
0
 public function actionManage()
 {
     // TODO add check owner
     $id = Yii::app()->user->getId();
     if (isset($_POST['CamsForm']) && !empty($_POST['CamsForm']) && array_sum($_POST['CamsForm']) != 0) {
         if (isset($_POST['doubleShare'])) {
             Yii::app()->user->setFlash('share', $_POST['CamsForm']);
             $this->redirect($this->createUrl('cams/share', array('type' => 'dshare')));
             Yii::app()->end();
         }
         if (isset($_POST['doubleAssign'])) {
             Yii::app()->user->setFlash('share', $_POST['CamsForm']);
             $this->redirect($this->createUrl('cams/share', array('type' => 'dassign')));
             Yii::app()->end();
         }
         if (isset($_POST['share'])) {
             Yii::app()->user->setFlash('share', $_POST['CamsForm']);
             $this->redirect(array('share'));
             Yii::app()->end();
         }
         if (isset($_POST['assign'])) {
             Yii::app()->user->setFlash('share', $_POST['CamsForm']);
             $this->redirect($this->createUrl('cams/share', array('type' => 'assign')));
             Yii::app()->end();
         }
         foreach ($_POST['CamsForm'] as $key => $cam) {
             if ($cam) {
                 $key = explode('_', $key);
                 $key[2] = Cams::model()->getRealId($key[1]);
                 if ($key[0] == 'shcam') {
                     $cam = Shared::model()->findByPK((int) $key[1]);
                 } elseif ($key[0] == 'pcam') {
                     $cam = Shared::model()->findByAttributes(array('cam_id' => $key[2], 'user_id' => $id, 'is_public' => '1'));
                     if (!$cam) {
                         $cam = new Shared();
                         $cam->owner_id = Cams::model()->findByPK($key[2])->user_id;
                         $cam->user_id = $id;
                         $cam->cam_id = $key[2];
                         $cam->show = 1;
                         $cam->is_public = 1;
                     }
                 } else {
                     $cam = Cams::model()->findByPK($key[2]);
                 }
                 if (!$cam) {
                     continue;
                 }
                 if (isset($_POST['show'])) {
                     $cam->show = $cam->show ? 0 : 1;
                     if ($cam->save()) {
                         Yii::app()->user->setFlash('notify', array('type' => 'success', 'message' => Yii::t('cams', 'Cams settings changed')));
                     }
                 } elseif (isset($_POST['del'])) {
                     if ($key[0] == 'shcam') {
                         if ($cam->delete()) {
                             Yii::app()->user->setFlash('notify', array('type' => 'success', 'message' => Yii::t('cams', 'Cams successfully deleted')));
                         }
                     } else {
                         $momentManager = new momentManager($cam->server_id);
                         if ($cam->delete()) {
                             $momentManager->delete($cam);
                             Yii::app()->user->setFlash('notify', array('type' => 'success', 'message' => Yii::t('cams', 'Cams successfully deleted')));
                         }
                     }
                 } elseif (isset($_POST['record'])) {
                     $cam->record = $cam->record ? 0 : 1;
                     $momentManager = new momentManager($cam->server_id);
                     $momentManager->rec($cam->record == 1 ? 'on' : 'off', $cam->id);
                     if ($cam->save()) {
                         Yii::app()->user->setFlash('notify', array('type' => 'success', 'message' => Yii::t('cams', 'Cams settings changed')));
                     }
                 }
             }
         }
         $this->refresh();
         Yii::app()->end();
     } elseif (isset($_POST['show']) || isset($_POST['del']) || isset($_POST['share']) || isset($_POST['assign'])) {
         Yii::app()->user->setFlash('notify', array('type' => 'warning', 'message' => Yii::t('cams', 'You must choose cams to modify')));
     }
     $public = array();
     $publicAll = Cams::model()->findAllByAttributes(array('is_public' => 1));
     $shared = Shared::model()->findAllByAttributes(array('user_id' => $id, 'is_public' => 0, 'is_approved' => 1));
     foreach ($shared as $key => $value) {
         if (!isset($value->cam->id)) {
             $value->delete();
             unset($shared[$key]);
         }
     }
     $publicEdited = Shared::model()->findAllByAttributes(array('user_id' => $id, 'is_public' => 1), array('index' => 'cam_id'));
     foreach ($publicAll as $cam) {
         if (isset($publicEdited[$cam->id])) {
             $public[] = $publicEdited[$cam->id];
         } else {
             $public[] = $cam;
         }
     }
     $this->render('list', array('form' => new CamsForm(), 'myCams' => Cams::model()->findAllByAttributes(array('user_id' => $id)), 'mySharedCams' => $shared, 'myPublicCams' => $public));
 }