/**
  *
  * @param $categname
  * @param int $parentid
  * @param int $intPosition
  * @return bool|Category|int
  */
 protected function parseCategoryString($categname, $parentid = 0, $intPosition = 0)
 {
     $categname = trim($categname);
     if (empty($categname)) {
         return $parentid;
     }
     $categs = Category::LoadArrayByName($categname);
     $exist = false;
     foreach ($categs as $categ) {
         if ($categ->Parent == $parentid) {
             $exist = $categ;
             break;
         }
     }
     if ($exist) {
         return $exist;
     }
     $categ = new Category();
     $categ->Name = $categname;
     $categ->Parent = $parentid;
     $categ->Created = new QDateTime(QDateTime::Now);
     $categ->Position = $intPosition;
     $categ->Save(true);
     $categ->UpdateChildCount();
     return $categ;
 }
 /**
  * Save/Add a category with ID.
  * Rowid and ParentId are RowID of the current category and parentIDs
  * Category is the category name
  * blbImage is base64encoded png
  * meta keywords and descriptions are for meta tags displayed for SEO improvement
  * Custom page is a page-key defined in Custom Pages in admin panel
  * Position defines the sorting position of category. Lower number comes first
  *
  * @param string $passkey
  * @param int $intRowId
  * @param int $intParentId
  * @param string $strCategory
  * @param string $strMetaKeywords
  * @param string $strMetaDescription
  * @param string $strCustomPage
  * @param int $intPosition
  * @param string $blbImage
  * @return string
  * @throws SoapFault
  * @soap
  */
 public function save_category_with_id($passkey, $intRowId, $intParentId, $strCategory, $strMetaKeywords, $strMetaDescription, $strCustomPage, $intPosition, $blbImage)
 {
     self::check_passkey($passkey);
     Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS=0;')->execute();
     // Prepare values
     $strCategory = trim($strCategory);
     $strCustomPage = trim($strCustomPage);
     if (empty($strCategory)) {
         Yii::log("Empty category received", CLogger::LEVEL_ERROR, 'application.' . __CLASS__ . "." . __FUNCTION__);
         $strCategory = "Untitled Category";
     }
     $objCategoryAddl = false;
     // If provided a rowid, attempt to load it
     if ($intRowId) {
         $objCategoryAddl = CategoryAddl::model()->findByPk($intRowId);
     } else {
         if (!$objCategoryAddl && $intParentId) {
             $objCategoryAddl = CategoryAddl::LoadByNameParent($strCategory, $intParentId);
         }
     }
     // Failing that, create a new Category
     if (!$objCategoryAddl) {
         $objCategoryAddl = new CategoryAddl();
         $objCategoryAddl->created = new CDbExpression('NOW()');
         $objCategoryAddl->id = $intRowId;
     }
     $objCategoryAddl->label = $strCategory;
     if ($intParentId > 0) {
         $objCategoryAddl->parent = $intParentId;
     }
     $objCategoryAddl->menu_position = $intPosition;
     $objCategoryAddl->modified = new CDbExpression('NOW()');
     $objCategoryAddl->save();
     //Now that we've successfully saved in our cache table, update the regular Category table
     $objCategory = Category::model()->findByPk($intRowId);
     // Failing that, create a new Category
     if (!$objCategory) {
         $objCategory = new Category();
         $objCategory->created = new CDbExpression('NOW()');
         $objCategory->id = $objCategoryAddl->id;
     }
     if ($objCategory) {
         $objCategory->label = $objCategoryAddl->label;
         $objCategory->parent = $objCategoryAddl->parent;
         $objCategory->menu_position = $objCategoryAddl->menu_position;
     }
     if (!$objCategory->save()) {
         $strMsg = "Error saving category {$strCategory}";
         Yii::log("SOAP ERROR: {$strMsg} " . print_r($objCategory->getErrors(), true), CLogger::LEVEL_ERROR, 'application.' . __CLASS__ . "." . __FUNCTION__);
         throw new SoapFault($strMsg, WsSoapException::ERROR_UNKNOWN);
     }
     //After saving, update some key fields
     $objCategory->UpdateChildCount();
     $objCategory->request_url = $objCategory->GetSEOPath();
     if (!$objCategory->save()) {
         $strMsg = "Error saving category (after updating) {$strCategory}";
         Yii::log("SOAP ERROR: {$strMsg} " . print_r($objCategory->getErrors(), true), CLogger::LEVEL_ERROR, 'application.' . __CLASS__ . "." . __FUNCTION__);
         throw new SoapFault($strMsg, WsSoapException::ERROR_UNKNOWN);
     }
     Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS=1;')->execute();
     return self::OK;
 }