/**
  * Update settings
  */
 public function actionIndex()
 {
     // load settings
     /** @var YdSetting[] $settings */
     $settings = array();
     $_settings = YdSetting::model()->findAll();
     foreach ($_settings as $setting) {
         $settings[$setting->key] = $setting;
     }
     //        // load from items
     //        foreach (YdSetting::items() as $key => $value) {
     //            if (!isset($settings[$key])) {
     //                $settings[$key] = new YdSetting();
     //                $settings[$key]->key = $key;
     //                $settings[$key]->value = $value;
     //                $settings[$key]->save(false);
     //            }
     //        }
     // load from params
     foreach (Yii::app()->params as $key => $value) {
         if (is_scalar($value) && !isset($settings[$key])) {
             $settings[$key] = new YdSetting();
             $settings[$key]->key = $key;
             $settings[$key]->value = $value;
             $settings[$key]->save(false);
         }
     }
     // handle posted data
     if (isset($_POST['YdSetting'])) {
         // begin transaction
         $error = false;
         $transaction = YdSetting::model()->getDbConnection()->beginTransaction();
         // save settings
         foreach ($_POST['YdSetting'] as $key => $value) {
             $value = isset($value['value']) ? $value['value'] : 0;
             $settings[$key]->value = $value;
             if (!$settings[$key]->save()) {
                 $error = true;
                 break;
             }
         }
         if (!$error) {
             // commit transaction
             $transaction->commit();
             // clear cache
             Yii::app()->cache->flush();
             // flash and redirect
             Yii::app()->user->addFlash(Yii::t('dressing', 'Settings have been saved.'), 'success');
             $this->redirect(Yii::app()->returnUrl->getUrl(array('/setting/index')));
         }
         // rollback transaction and flash error
         $transaction->rollback();
         Yii::app()->user->addFlash(Yii::t('dressing', 'Settings could not be saved.'), 'error');
     } else {
         !empty($settings['script_path']) && ($settings['script_path']->value = empty($settings['script_path']->value) ? dirname($_SERVER['SCRIPT_FILENAME']) : $settings['script_path']->value);
         !empty($settings['script_url']) && ($settings['script_url']->value = empty($settings['script_url']->value) ? dirname($_SERVER['SCRIPT_NAME']) : $settings['script_url']->value);
         !empty($settings['server_name']) && ($settings['server_name']->value = empty($settings['server_name']->value) ? $_SERVER['SERVER_NAME'] : $settings['server_name']->value);
     }
     $this->render('index', array('settings' => $settings));
 }