/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new ProductFolder();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['ProductFolder'])) {
         $model->attributes = $_POST['ProductFolder'];
         $model->company_id = Yii::app()->session['user']->company_id;
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * create new company & user as creator & admin
  */
 public function register()
 {
     $transaction = Yii::app()->db->beginTransaction();
     try {
         //create company first
         $company = new Company();
         $company->name = $this->name;
         $company->country = $this->country;
         $company->phone = $this->phone;
         if (!$company->save()) {
             $transaction->rollback();
             return false;
         }
         //create user
         $user = new User();
         $user->username = $this->username;
         $user->password = $user->encrypt($this->password);
         $user->password_repeat = $this->password_repeat;
         $user->email = $this->email;
         $user->company_id = $company->id;
         $user->create_time_utc = $user->update_time_utc = time();
         if (!$user->save(false)) {
             $transaction->rollback();
             return false;
         }
         $company->owner_id = $company->create_user_id = $company->update_user_id = $user->id;
         if (!$company->update()) {
             $transaction->rollback();
             return false;
         }
         $user->create_user_id = $user->update_user_id = $user->id;
         if (!$user->update()) {
             $transaction->rollback();
             return false;
         }
         //create default product folder
         $defaultProductFolder = new ProductFolder();
         $defaultProductFolder->name = 'Main Folder';
         $defaultProductFolder->parent_id = 0;
         $defaultProductFolder->company_id = $company->id;
         if (!$defaultProductFolder->save()) {
             $transaction->rollback();
             return false;
         }
         $transaction->commit();
         return true;
     } catch (Exception $ex) {
         $transaction->rollback();
         return false;
     }
 }