/**
  * @param int $id
  * @param  $ebay_global_id
  * @throws CHttpException
  */
 public function actionIndex($id = 0, $ebay_global_id = false)
 {
     Yii::import('application.vendors.CategoryTree');
     if (empty($id)) {
         $model = new SearchRequests();
         //$model->unsetAttributes();  // clear any default values
         if (empty($ebay_global_id)) {
             $ebay_global_id = 'EBAY-US';
         }
         $model->ebay_global_id = $ebay_global_id;
     } else {
         $model = $this->loadModel($id);
         if (!empty($ebay_global_id)) {
             $model->ebay_global_id = $ebay_global_id;
         } else {
             $ebay_global_id = $model->ebay_global_id;
         }
     }
     if (isset($_POST['SearchRequests'])) {
         $attributes = $_POST['SearchRequests'];
         if (isset($_POST['request_type']) and $_POST['request_type'] == 'new') {
             //$model->is
             $model->isNewRecord = true;
             $model->id = null;
             unset($attributes['id']);
         }
         if ($model->isNewRecord) {
             $attributes['user_id'] = WebUser::Id(true);
         }
         $attributes['date_update'] = time();
         $model->attributes = $attributes;
         if ($model->save()) {
             $this->redirect(array('listing', 'id' => $model->id));
         } else {
             $this->setFlashError(Yii::t('sniper_ebay', 'Correct those fields and try again'));
         }
     }
     $categories = false;
     if (empty($categories)) {
         $categoriesAPI = new GetCategoriesClass();
         $categories[0] = 'All';
         try {
             $ebay_site_id = SearchRequests::model()->getSiteEbayId($ebay_global_id);
             $categoriesAPI->siteID = $ebay_site_id;
             if ($ebay_global_id == 'EBAY-MOTOR') {
                 $categoriesAPI->levelLimit = 4;
             }
             $ebay_cats = $categoriesAPI->makeAPICall();
             $cats_attributes = array();
             $categories_tree = array();
             foreach ($ebay_cats as $_e_category) {
                 $cats_attributes[] = array('ebay_category_id' => (int) $_e_category->CategoryID, 'category_name' => (string) $_e_category->CategoryName, 'ebay_category_level' => (int) $_e_category->CategoryLevel, 'ebay_parent_id' => (int) $_e_category->CategoryParentID, 'auto_pay' => $_e_category->AutoPayEnabled == 'true' ? 1 : 0, 'best_offer' => $_e_category->BestOfferEnabled == 'true' ? 1 : 0);
                 $categories[(int) $_e_category->CategoryID] = (string) $_e_category->CategoryName;
                 $_id = (int) $_e_category->CategoryID;
                 $_parent_id = (int) $_e_category->CategoryParentID;
                 $categories_tree[] = array('id' => $_id, 'parent_id' => $_id == $_parent_id ? 0 : $_parent_id, 'title' => (string) $_e_category->CategoryName);
             }
             $tree = new CategoryTree($categories_tree);
             $categories = $tree->getOneDimTree();
             //Categories::model()->saveMultiple($cats_attributes);
         } catch (Exception $ex) {
             $this->setFlashError($ex->getMessage());
         }
     } else {
         $categories = CHtml::listData($categories, 'ebay_category_id', 'category_name');
     }
     $data = array('categories' => $categories, 'model' => $model);
     $this->render('index', $data);
 }