public function prepare()
{
if ($this->mode == 'create') {
$this->initialise(array('moduleID' => ucfirst(strtolower(Specialty::model()->findByPk($_REQUEST['Specialty']['id'])->abbreviation)) . ucfirst(strtolower(EventGroup::model()->findByPk($_REQUEST['EventGroup']['id'])->code)) . Yii::app()->request->getQuery('Specialty[id]') . preg_replace('/ /', '', ucfirst(strtolower($this->moduleSuffix))), 'moduleShortID' => ucfirst(strtolower(Specialty::model()->findByPk($_REQUEST['Specialty']['id'])->abbreviation)) . ucfirst(strtolower(EventGroup::model()->findByPk($_REQUEST['EventGroup']['id'])->code)) . Yii::app()->request->getQuery('Specialty[id]') . preg_replace('/ /', '', ucfirst(strtolower($this->moduleShortSuffix))), 'eventGroupName' => EventGroup::model()->findByPk($_REQUEST['EventGroup']['id'])->name));
} elseif ($this->mode == 'update') {
$this->event_type = EventType::model()->findByPk(@$_POST['EventTypeModuleEventType']);
$short_suffix = $this->getEventShortName($this->event_type);
$this->initialise(array('moduleID' => $this->event_type->class_name, 'moduleShortID' => Specialty::model()->findByPk($_REQUEST['Specialty']['id'])->abbreviation . EventGroup::model()->findByPk($_REQUEST['EventGroup']['id'])->code . $short_suffix, 'event_group' => EventGroup::model()->findByPk($_REQUEST['EventGroup']['id']), 'specialty' => Specialty::model()->findByPk($_REQUEST['Specialty']['id']), 'eventGroupName' => $this->event_type->name));
}
if ($this->mode == 'update') {
$current_class = $this->event_type->class_name;
$target_class = Yii::app()->getController()->target_class = ucfirst(strtolower($this->specialty->abbreviation)) . ucfirst(strtolower($this->event_group->code)) . Yii::app()->request->getQuery('Specialty[id]') . preg_replace('/ /', '', ucfirst(strtolower($this->moduleSuffix)));
if (@$_POST['generate'] == 'Generate') {
$this->handleViewChanges();
if ($current_class != $target_class) {
$this->handleModuleNameChange($current_class, $target_class);
}
}
}
$elements = $this->getElementsFromPost();
foreach ($this->files_to_process as $file) {
$destination_file = preg_replace('/EVENTNAME|EVENTTYPENAME|MODULENAME/', $this->moduleID, $file);
if ($file !== $this->moduleTemplateFile) {
if (CFileHelper::getExtension($file) === 'php' || CFileHelper::getExtension($file) === 'js' || CFileHelper::getExtension($file) === 'json' || CFileHelper::getExtension($file) === 'scss') {
if (preg_match('/' . preg_quote(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR) . 'migrations' . preg_quote(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR) . '/', $file)) {
if (preg_match('/_create\\.php$/', $file) && $this->mode == 'create') {
# $matches = Array();
if (file_exists($this->modulePath . '/migrations/') and $matches = $this->regExpFile("/m([0-9]+)\\_([0-9]+)\\_event_type_" . $this->moduleID . '/', $this->modulePath . '/migrations/')) {
// migration file exists, so overwrite it rather than creating a new timestamped file
$migrationid = $matches[1] . '_' . $matches[2];
} else {
$migrationid = gmdate('ymd_His');
}
$destination_file = preg_replace('/' . preg_quote(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR) . 'migrations' . preg_quote(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR) . '/', '/migrations/m' . $migrationid . '_', preg_replace('/_create/', '', $destination_file));
$content = $this->renderMigrations($file, $migrationid);
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
} elseif (preg_match('/_update\\.php$/', $file) && $this->mode == 'update') {
$elements_have_changed = false;
foreach ($_POST as $key => $value) {
if (preg_match('/^elementName[0-9]+$/', $key) || preg_match('/^elementId[0-9]+$/', $key)) {
$elements_have_changed = true;
}
}
if ($elements_have_changed) {
$migrationid = gmdate('ymd_His');
$destination_file = preg_replace("/\\/migrations\\//", '/migrations/m' . $migrationid . '_', preg_replace('/_update/', '', $destination_file));
$content = $this->renderMigrations($file, $migrationid);
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
}
}
} elseif (preg_match('/ELEMENTNAME|ELEMENTTYPENAME/', $file)) {
foreach ($elements as $element) {
$destination_file = preg_replace('/ELEMENTNAME|ELEMENTTYPENAME/', $element['class_name'], $file);
$content = $this->render($file, array('element' => $element));
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
}
} elseif (preg_match('/LOOKUPTABLE/', $file)) {
foreach ($elements as $element) {
foreach ($element['lookup_tables'] as $lookup_table) {
$destination_file = preg_replace('/LOOKUPTABLE/', $lookup_table['class'], $file);
$content = $this->render($file, array('lookup_table' => $lookup_table));
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
}
}
} elseif (preg_match('/MAPPINGTABLE/', $file)) {
foreach ($elements as $element) {
foreach ($element['mapping_tables'] as $mapping_table) {
$destination_file = preg_replace('/MAPPINGTABLE/', $mapping_table['class'], $file);
$content = $this->render($file, array('mapping_table' => $mapping_table));
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
}
}
} elseif (preg_match('/DEFAULTSTABLE/', $file)) {
foreach ($elements as $element) {
foreach ($element['defaults_tables'] as $defaults_table) {
$destination_file = preg_replace('/DEFAULTSTABLE/', $defaults_table['class'], $file);
$content = $this->render($file, array('defaults_table' => $defaults_table));
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
}
}
} elseif (preg_match('/\\.js$/', $file)) {
$content = $this->render($file, array('elements' => $elements));
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
} else {
$content = $this->render($file);
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
}
// an empty directory
} elseif (basename($file) === '.yii') {
$file = dirname($file);
$content = null;
} else {
$content = file_get_contents($file);
$this->files[] = new CCodeFile($this->modulePath . substr($destination_file, strlen($this->templatePath)), $content);
}
}
}
}