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(); } }
public static function updateMunicipality($input, $id) { $answer = []; $rules = ['name' => 'required']; $validation = Validator::make($input, $rules); if ($validation->fails()) { $answer['message'] = $validation; $answer['error'] = true; } else { $municipality = Municipality::find($id); $municipality->name = $input['name']; if (Input::has('status')) { $municipality->status = $input['status']; } else { $municipality->status = 0; } if ($municipality->save()) { $answer['message'] = 'Editado con exito!'; $answer['error'] = false; } else { $answer['message'] = 'MUNICIPALITY UPDATE error, team noob!'; $answer['error'] = false; } } return $answer; }
/** * Display a listing of the resource. * * @return Response */ public function index() { $statistics = (object) array('schools_count' => School::all()->count(), 'municipalities_count' => Municipality::all()->count(), 'visitor_likes_count' => VisitorLikes::all()->count(), 'visitor_comments_count' => VisitorComments::all()->count()); $home_schools = School::orderBy('created_at', 'desc')->take(5)->get(); $home_comments = VisitorComments::orderBy('created_at', 'desc')->take(5)->get(); return View::make('admin.home')->with('home_schools', $home_schools)->with('home_comments', $home_comments)->with('statistics', $statistics); }
protected static function boot() { parent::boot(); static::creating(function ($model) { $data = array('name' => $model->name); $rules = array('name' => 'required|min:3|max:50'); $validator = Validator::make($data, $rules); if ($validator->fails()) { throw new ValidationException(null, null, null, $validator->messages()); } else { return $model->validate(); } }); static::updating(function ($model) { $data = array('name' => $model->name); $rules = array('name' => 'required|min:3|max:50'); $validator = Validator::make($data, $rules); if ($validator->fails()) { throw new ValidationException(null, null, null, $validator->messages()); } else { return true; } }); static::deleting(function ($model) { $municipalities = Municipality::where('district_id', '=', $model->id)->get(); foreach ($municipalities as $municipality) { $municipality = Municipality::find($municipality->id)->delete(); } return true; }); }
public function validate() { if (is_null($this::where('name', '=', $this->name)->first())) { if (!is_null(Municipality::where('id', '=', $this->municipality_id)->first())) { return true; } else { throw new ValidationException(null, null, null, array('municipality_id' => "Грешен ID на общината.")); } } else { throw new ValidationException(null, null, null, array('name' => "Градът/селото вече съществува.")); } }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $municipality = Municipality::find($id); if (!is_null($municipality)) { if ($municipality->delete()) { return Redirect::route('admin.municipalities.index')->withErrors(array('mainSuccess' => 'Общината е успешно изтрита.')); } else { return Redirect::route('admin.municipalities.index')->withErrors(array('mainError' => 'Грешка с базата данни.')); } } else { return Redirect::route('admin.municipalities.index')->withErrors(array('mainError' => 'Общината не е намерена.')); } }
<?php echo $form->labelEx($model, 'region_id'); ?> <?php $data = Region::model()->GetRegionsICanEdit(); echo CHtml::activeDropDownList($model, 'region_id', CHtml::listData($data, 'id', 'name'), array('empty' => Yii::t('app', 'choose'))); echo $form->error($model, 'region_id'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'municipality_id'); ?> <?php $data = Municipality::model()->GetMunicipalityICanEdit(); echo CHtml::activeDropDownList($model, 'municipality_id', CHtml::listData($data, 'id', 'name'), array('empty' => Yii::t('app', 'choose'))); echo $form->error($model, 'municipality_id'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'tax_number'); ?> <?php echo $form->textField($model, 'tax_number', array('size' => 12, 'maxlength' => 12)); echo $form->error($model, 'tax_number'); ?> </div>
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"; }
/** * 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 = Municipality::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.')); } return $model; }
public static function listFilters() { $filters_data['all'] = array(array('name' => 'Вид', 'list' => Type::select('id', 'name')->get()->toArray()), array('name' => 'Финансиране', 'list' => Financing::select('id', 'name')->get()->toArray()), array('name' => 'Специалности', 'list' => Specialty::select('id', 'name')->get()->toArray())); if (Input::has('district') == false && Input::has('municipality') == false && Input::has('city') == false) { $filters_data['all'][] = array('name' => 'Област', 'list' => District::select('id', 'name')->get()->toArray()); } if (Input::has('district') == true && Input::has('municipality') == false && Input::has('city') == false) { $district = Input::get('district'); if (is_array(Input::get('district'))) { $district = end($district); } $filters_data['all'][] = array('name' => 'Община', 'previous' => 'Област', 'list' => Municipality::select('id', 'name')->where('district_id', '=', $district)->get()->toArray()); $filters_data['previous_location'] = URL::route('schools.index', Input::except('district', 'municipality', 'city')); } if (Input::has('district') == true && Input::has('municipality') == true || Input::has('municipality') == true || Input::has('city') == true) { $municipality = Input::get('municipality'); if (is_array(Input::get('municipality'))) { $municipality = end($municipality); } $filters_data['all'][] = array('name' => 'Град/село', 'previous' => 'Община', 'list' => City::select('id', 'name')->where('municipality_id', '=', $municipality)->get()->toArray()); $filters_data['previous_location'] = URL::route('schools.index', Input::except('municipality', 'city')); } $input_filter = array_filter(Input::only('city', 'district', 'financing', 'municipality', 'specialty', 'type')); if (!empty($input_filter)) { $filters_data['clean_all_filters'] = URL::route('schools.index', array_filter(Input::only('search'))); } return $filters_data; }