public function validateUniqueCode($attribute, $param) { $criteria = new CDbCriteria(); $criteria->compare('code', $this->code); $criteria->compare('locale', $this->locale); $result = ExceptionMessage::model()->findAll($criteria); if (is_array($result) && count($result)) { $this->addError('code', 'This code has been duplicated'); } }
/** * Install exceptions for module in packages/1.0/locale/errors.php * * @param bool $reverse * * @return bool */ public function installErrors($reverse = false) { if ($reverse == false) { //Install $localeId = app()->getLocale()->id; $file = $this->getPackageFolder() . ($localeId == 'en_us' ? '' : $localeId . '/') . 'errors.php'; if (file_exists($file)) { $errors = array(); include_once $file; if (count($this->exceptions) == 0) { $this->exceptions = $errors; } $migration = new XMigration(); //Insert new error codes of module foreach ($this->exceptions as $code => $message) { if (ExceptionMessage::model()->count('code=:code', array(':code' => $code)) == 0) { $migration->insert(SITE_OWNER . '_exception_message', array('code' => $code, 'message' => $message, 'locale' => $localeId, 'creation_datetime' => date('m/d/Y h:i:s a', time()), 'last_update' => null)); } } } return true; } else { //Uninstall return true; } }
/** * Export all exceptions in module * * @param $moduleId module's id * @param string $version version * * @return XException */ public function actionExportModuleErrorMessages($moduleId, $version = '1.0') { $module = Module::model()->findByPk($moduleId); if (is_object($module)) { //Find all locales $locales = Yii::app()->db->createCommand()->selectDistinct('locale')->from(SITE_OWNER . '_exception_message')->where('code LIKE \'' . strtoupper($module->name == 'Xpress' || $module->name == 'Admin' ? 'System' : $module->name) . '_%\'')->queryAll(); // $moduleInfo = app()->getModule($module->name); if (is_object($moduleInfo)) { $packageFolderPath = $moduleInfo->getPackageFolder() . '../'; //Check package folder if (!makeDir($packageFolderPath) && !is_dir($packageFolderPath) || !is_writeable($packageFolderPath)) { return $this->result = errorHandler()->commonException()->fileNotWritable($packageFolderPath); } foreach ($locales as $locale) { $locale['locale'] = trim($locale['locale']); //Find all exception with locale $exceptions = ExceptionMessage::model()->findAll('code LIKE \'' . strtoupper($module->name == 'Xpress' || $module->name == 'Admin' ? 'System' : $module->name) . '_%\' AND locale=:locale ORDER BY code', array(':locale' => $locale['locale'])); //Generate content $items = '$errors = [' . "\n"; foreach ($exceptions as $exception) { $items .= '\'' . $exception->code . '\'=>' . '\'' . $exception->message . '\',' . "\n"; } $items .= "\n" . '];'; $content = "<?php\n/**\n * This file contains all errors in the module and serves as the errorCode => message dictionary.\n * To define an errorCode, you must prefix it with the module ID (in upper case) following by ERR,\n * i.e. SYSTEM_ERR_ so that your code is unique across the system.\n * To define an error message, use {key} for the content to be replaced with the content from\n * errorData array at time the error is logged.\n *\n * Before you define an error, make sure you refer to XCommonExceptionLogger class for all the\n * common errors and their messages that InditionCMS has already defined for you.\n */\n {$items}\n "; //Generate errors.php file $versionFolderPath = $packageFolderPath . $version . '/'; if ($locale['locale'] != 'en_us') { $errorFolderPath = trim($versionFolderPath . $locale['locale']); $errorFilePath = $errorFolderPath . '/errors.php'; } else { $errorFolderPath = $versionFolderPath; $errorFilePath = $errorFolderPath . '/errors.php'; } //Check version folder path if (!makeDir($versionFolderPath) && !is_dir($versionFolderPath) || !is_writeable($versionFolderPath)) { return $this->result = errorHandler()->commonException()->fileNotWritable($versionFolderPath); } //Check error folder path if (!makeDir($errorFolderPath) && !is_dir($errorFolderPath) || !is_writeable($errorFolderPath)) { return $this->result = errorHandler()->commonException()->fileNotWritable($errorFolderPath); } try { file_put_contents($errorFilePath, $content); } catch (Exception $ex) { errorHandler()->commonException()->fileNotWritable($errorFilePath); } $this->result = $errorFilePath; } } else { return $this->result = errorHandler()->commonException()->modelWithGivenIdNotFound('Module', $moduleId); } } }