public function run() { $faker = Faker::create(); for ($i = 0; $i < 5; $i++) { $municipality = new Municipality(); $municipality->name = $faker->unique()->streetName; $municipality->status = 1; $municipality->save(); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $data = Input::all(); $municipality = new Municipality(); $municipality->district_id = $data['district_id']; $municipality->name = $data['name']; try { $municipality->save(); } catch (ValidationException $errors) { return Redirect::route('admin.municipalities.create')->withErrors($errors->getErrors())->withInput(); } return Redirect::route('admin.municipalities.create')->withErrors(array('mainSuccess' => 'Общината е успешно добавена.')); }
public static function createMunicipality($input) { $answer = []; $rules = ['name' => 'required']; $validation = Validator::make($input, $rules); if ($validation->fails()) { $answer['message'] = $validation; $answer['error'] = true; } else { $municipality = new Municipality(); $municipality->name = $input['name']; $municipality->status = 1; if ($municipality->save()) { $answer['message'] = 'Creado con exito!'; $answer['error'] = false; } else { $answer['message'] = 'MUNICIPALITY CREATE error, team noob!'; $answer['error'] = false; } } return $answer; }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { if ($this->CanAccess('create')) { $model = new Municipality(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Municipality'])) { $model->attributes = $_POST['Municipality']; if ($model->CanUpdate() && $model->save()) { // if AJAX request , we should not redirect the browser if (!Yii::app()->request->isAjaxRequest) { $this->redirect(array('view', 'id' => $model->id)); } else { // UNCOMMENT THIS IF YOU WANT TO RETURN ID OF THE NEWLY CREATED // OBJECT (USEFUL WHEN CREATING NEW OBJECTS VIA AJAX AND INFO ABOUT // THEN NEWLY CREATED OBJECT MUST BE SENT TO THE BROWSER) // echo CJSON::encode(array('error' => '', 'id' => $model->id)); // die(); } } } if (!Yii::app()->request->isAjaxRequest) { $this->render('create', array('model' => $model)); // IF YOU NEED DIFFERENT RENDERING FOR AJAX AND NON-AJAX CALLS, // USE THIS LINE AND DELETE THE LINE ABOVE // $this->render('create', array('model' => $model, 'ajaxRendering' => false)); } else { throw new CHttpException(400, Yii::t('app', 'Bad request. The request cannot be fulfilled.')); // IF YOU NEED DIFFERENT RENDERING FOR AJAX AND NON-AJAX CALLS, // USE THIS LINE AND DELETE THE LINE ABOVE // $this->renderPartial('create', array('model' => $model, 'ajaxRendering' => true)); } } else { throw new CHttpException(405, Yii::t('app', 'You do not have permissions to access this page.')); } }
public function import($content) { $header_cols = array('name' => 0, 'school_category_id' => 1, 'level_of_education' => 2, 'post' => 3, 'postal_code' => 4, 'municipality_id' => 5, 'region_id' => 6, 'country_id' => 7); header('Content-Type: text/html; charset=utf-8'); $lines = explode("\n", $content); for ($i = 1; $i < count($lines); ++$i) { if (trim($lines[$i]) == '') { continue; } $cols = explode(";", $lines[$i]); // država $country_id = 0; $country = Country::model()->find('country=:country', array(':country' => trim($cols[$header_cols['country_id']]))); if ($country == null) { echo Yii::t('app', 'Country does not exist! Country: ') . trim($cols[$header_cols['country_id']]); die; } $country_id = $country->id; // regija $region = Region::model()->find('name=:name and country_id=:country_id', array(':name' => trim($cols[$header_cols['region_id']]), ':country_id' => $country_id)); if ($region == null) { $region = new Region(); $region->country_id = $country_id; $region->name = trim($cols[$header_cols['region_id']]); $region->save(); } $region_id = $region->id; // občina $municipality = Municipality::model()->find('name=:name and country_id=:country_id', array(':name' => trim($cols[$header_cols['municipality_id']]), ':country_id' => $country_id)); if ($municipality == null) { $municipality = new Municipality(); $municipality->country_id = $country_id; $municipality->name = trim($cols[$header_cols['municipality_id']]); $municipality->save(); } $municipality_id = $municipality->id; $school_name = trim($cols[$header_cols['name']]); if ($school_name[0] == '"') { $school_name = mb_substr($school_name, 1, mb_strlen($school_name, 'UTF-8') - 2, 'UTF-8'); } $school_name = str_replace('""', '"', $school_name); $school = School::model()->find('name=:name and country_id=:country_id', array(':name' => $school_name, ':country_id' => $country_id)); if ($school == null) { $school = new School(); $school->name = $school_name; $school->country_id = $country_id; $school->municipality_id = $municipality_id; $school->region_id = $region_id; $school->post = trim($cols[$header_cols['post']]); $school->postal_code = trim($cols[$header_cols['postal_code']]); $school->school_category_id = (int) trim($cols[$header_cols['school_category_id']]); $school->level_of_education = (int) trim($cols[$header_cols['level_of_education']]); $school->save(); echo 'Imported: ', $school_name, "<br />\n"; if (count($school->errors) > 0) { print_r($school->errors); die; } } else { echo 'Already imported: ', $school_name, "<br />\n"; } } }
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"; }