public static function SyncZavodiWhereKategorijaActive() { $schoolCategories = SchoolCategory::model()->findAll('active=:active', array(':active' => 1)); $kategorije = array(); $kategorija_map = array(); foreach ($schoolCategories as $schoolCategory) { $kategorije[] = $schoolCategory->name; $kategorija_map[$schoolCategory->name] = $schoolCategory->id; } echo "Current categories for sync:<br />"; pre_print($kategorije); $list = self::GetRegZavod(); if (!isset($list['return'])) { return array(); } $list = $list['return']; echo "Current schools to sync:<br />"; $country = Country::model()->find('country=:country', array(':country' => 'Slovenija')); if ($country == null) { echo "Add country Slovenija!<br />\n"; die; } $country_id = $country->id; // cache all občine $municipalities = Municipality::model()->findAll('country_id=:country_id', array(':country_id' => $country_id)); $municipality_map = array(); foreach ($municipalities as $municipality) { $municipality_map[$municipality->name] = $municipality->id; } // cache all regije $regions = Region::model()->findAll('country_id=:country_id', array(':country_id' => $country_id)); $region_map = array(); foreach ($regions as $region) { $region_map[$region->name] = $region->id; } $counter = 0; $updated = 0; $inserted = 0; for ($i = 0; $i < count($list); ++$i) { if (in_array($list[$i]['KATEGORIJA'], $kategorije)) { $counter++; $el = $list[$i]; $school = School::model()->find('name=:name and country_id=:country_id', array(':name' => trim($el['ZAVOD_NAZIV']), ':country_id' => $country_id)); if ($school == null) { $school = new School(); $school->name = trim($el['ZAVOD_NAZIV']); $school->country_id = $country_id; $inserted++; } $school->school_category_id = $kategorija_map[trim($el['KATEGORIJA'])]; // občina if (!isset($municipality_map[trim($el['OBCINANAZIV'])])) { $municipality = new Municipality(); $municipality->name = trim($el['OBCINANAZIV']); $municipality->country_id = $country_id; $municipality->save(); $municipality_map[trim($el['OBCINANAZIV'])] = $municipality->id; } $school->municipality_id = $municipality_map[trim($el['OBCINANAZIV'])]; // regija if (!isset($region_map[trim($el['REGIJANAZIV'])])) { $region = new Region(); $region->name = trim($el['REGIJANAZIV']); $region->country_id = $country_id; $region->save(); $region_map[trim($el['REGIJANAZIV'])] = $region->id; } $school->region_id = $region_map[trim($el['REGIJANAZIV'])]; $school->post = trim($el['POSTANAZIV']); $school->postal_code = trim($el['POSTASIFRA']); $school->identifier = trim($el['ZAVPRS']); $school->headmaster = trim($el['ZAVRAVN']); if (isset($el['ZAVDAVST'])) { $school->tax_number = trim($el['ZAVDAVST']); } if ($school->save()) { $updated++; } } } echo 'Found schools to sync: ', $counter, "<br />\n"; echo 'New schools imported: ', $inserted, "<br />\n"; echo 'Updated schools: ', $updated - $inserted, "<br />\n"; }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = SchoolCategory::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.')); } return $model; }