public function actionPurge() { $check = CategoryAddl::model()->findAll(); if (!empty($check)) { $sql = 'SELECT id FROM xlsws_category WHERE id NOT IN (SELECT id FROM `xlsws_category_addl`);'; $emptycats = Yii::app()->db->createCommand($sql)->queryAll(); foreach ($emptycats as $id) { $sqldelete = "DELETE FROM xlsws_product_category_assn WHERE category_id = " . $id['id'] . ";"; try { Yii::app()->db->createCommand($sqldelete)->execute(); $obj = Category::model()->findByPk($id); $obj->UpdateChildCount(); } catch (Exception $e) { Yii::app()->user->setFlash('error', Yii::t('admin', 'Could not purge categories. Product associations could not be removed.')); } } } unset($check); $sql3 = "DELETE xlsws_category_integration.* FROM xlsws_category_integration\n\t\t\t\tLEFT JOIN xlsws_category_addl ON xlsws_category_addl.id = xlsws_category_integration.category_id\n\t\t\t\tWHERE xlsws_category_addl.id IS NULL"; $sql1 = "DELETE xlsws_category.* FROM xlsws_category\n\t\t\t\tLEFT JOIN xlsws_category_addl ON xlsws_category_addl.id = xlsws_category.id\n\t\t\t\tWHERE xlsws_category_addl.id IS NULL"; $sql2 = "DELETE xlsws_family.* from xlsws_family left join xlsws_product on xlsws_family.id=xlsws_product.family_id where xlsws_product.id is null"; $success = $check = 0; try { Yii::app()->db->createCommand($sql3)->execute(); $check = 1; } catch (Exception $e) { Yii::app()->user->setFlash('error', Yii::t('admin', 'Could not purge categories. Error encountered unassigning Amazon/Google integrations.')); } if ($check) { try { Yii::app()->db->createCommand($sql1)->execute(); $success = 1; } catch (Exception $e) { Yii::app()->user->setFlash('error', Yii::t('admin', 'Could not purge categories. Cannot remove deleted categories that are still assigned to products.')); } } if ($success) { try { Yii::app()->db->createCommand($sql2)->execute(); $success = 1; } catch (Exception $e) { Yii::app()->user->setFlash('error', Yii::t('admin', 'Could not purge families.')); } } if ($success) { Yii::app()->user->setFlash('success', Yii::t('admin', 'Done. This option has removed any categories and families you deleted in Lightspeed that may have been left on Web Store. {time}.', array('{time}' => date('d F, Y h:i:sa')))); } $this->render("purge"); }
public static function LoadByNameParent($strName, $intParentId) { return CategoryAddl::model()->findByAttributes(array('name' => $strName, 'parent', $intParentId)); }
/** * 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 */ public function save_category_with_id($passkey, $intRowId, $intParentId, $strCategory, $strMetaKeywords, $strMetaDescription, $strCustomPage, $intPosition, $blbImage) { Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS=0;')->execute(); if (!$this->check_passkey($passkey)) { return self::FAIL_AUTH; } // Prepare values $strCategory = trim($strCategory); $strCustomPage = trim($strCustomPage); if (!$strCategory) { Yii::log("Could not save empty category", 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return self::UNKNOWN_ERROR; } $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()) { _xls_log("SOAP ERROR : Error saving category {$strCategory} " . print_r($objCategory->getErrors(), true)); return self::UNKNOWN_ERROR . " Error saving category {$strCategory} " . print_r($objCategory->getErrors(), true); } //After saving, update some key fields $objCategory->UpdateChildCount(); $objCategory->request_url = $objCategory->GetSEOPath(); if (!$objCategory->save()) { _xls_log("SOAP ERROR : Error saving category (after updating){$strCategory} " . print_r($objCategory->getErrors(), true)); return self::UNKNOWN_ERROR . " Error saving category (after updating){$strCategory} " . print_r($objCategory->getErrors(), true); } Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS=1;')->execute(); return self::OK; }