public function deleteOffice($id) { $deleteoffice = Office::find($id); $usersInOffice = User::where('office_id', $id)->count(); if ($usersInOffice == 0) { $deleteoffice->delete(); $users = User::where('office_id', $id)->get(); foreach ($users as $user) { $user->office_id = 0; $user->save(); } return Redirect::to('/offices')->with('success', 'Successfully deleted'); } else { return Redirect::to('/offices')->with('invalid', 'Unable to delete non-empty office. '); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { // Если админ, то выбираем все опросы. if ($isAdmin = Users::findOne(['profile_id' => Yii::$app->user->identity->username, 'profile_office_role' => 'admin'])) { $officeIds = ArrayHelper::map(Office::find()->all(), 'id', 'id'); } elseif ($isCommercialDirector = Users::findAll(['profile_id' => Yii::$app->user->identity->username, 'profile_office_role' => 'commercial_director'])) { // берем ID отделений, где регион отделения === региону, где пользователь коммерч.директор $regionIds = ArrayHelper::map($isCommercialDirector, 'region_id', 'region_id'); $officeIds = ArrayHelper::map(Office::findAll(['region_id' => $regionIds]), 'id', 'id'); } else { // берем ID отделений, где пользователь является управляющим, или где он назначен им. $officeIds = $this->getOffiсeIds(Yii::$app->user->identity->username); } $query = AnswerList::find()->joinWith('questionList')->where(['do_id' => $officeIds]); //$query = AnswerList::find()->innerJoinWith('questionList'); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $sort = $dataProvider->getSort(); $sort->attributes['officeName'] = ['asc' => ['questionlist_office.name' => SORT_ASC], 'desc' => ['questionlist_office.name' => SORT_DESC], 'label' => 'Отделение']; $sort->attributes['statusName'] = ['asc' => ['status' => SORT_ASC], 'desc' => ['status' => SORT_DESC], 'label' => 'Статус']; $dataProvider->setSort($sort); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); $query->joinWith(['questionlist_office']); return $dataProvider; } $query->andFilterWhere(['questionlist_answer_list.id' => $this->id, 'question_list_id' => $this->question_list_id, 'scores' => $this->scores]); $query->andFilterWhere(['>=', 'date_from', $this->date_from])->andFilterWhere(['<=', 'date_to', $this->date_to]); if ($this->statusName) { $query->andFilterWhere(['like', 'status', $this->statusName]); } else { $query->andFilterWhere(['not like', 'status', 'archive']); } $query->joinWith(['office' => function ($q) { $q->andFilterWhere(['like', 'questionlist_office.name', $this->officeName]); }]); return $dataProvider; }
</h4> </td> </tr> <tr> <td class="proc-headers" width="20%"><h5><strong>Requisitioner<strong></h5></td> <td class="proc-data"> <?php $user = User::find($purchase->requisitioner); ?> {{ $user->lastname . ", " . $user->firstname }} </td> <td class="proc-data"><strong>Office<strong></td> <td class="proc-data"> <?php $office = Office::find($purchase->office); ?> {{ $office->officeName }} </td> </tr> <tr> <td class="proc-headers"><h5><strong>Project / Purpose</strong></h5></td> <td class="proc-data">{{ $purchase->projectPurpose }}</td> <td class="proc-headers"><h5><strong>Project Type</strong></h5></td> <td class="proc-data">{{ $purchase->projectType }}</td> </tr> <tr> <td class="proc-data"><strong>ABC Amount</strong></td>
protected function validateAndSave($id = null) { $validator = Validator::make(Input::all(), Office::$rules); Log::info(Input::all()); // attempt validation if ($validator->fails()) { Session::flash('errorMessage', 'Your office was not saved'); // validation failed, redirect to the post create page with validation errors and old inputs return Redirect::back()->withInput()->withErrors($validator); } if ($id == null) { $office = new Office(); } else { $office = Office::find($id); } $office->address = Input::get('address'); $office->phone_number = Input::get('phone_number'); $office->latitude = Input::get('latitude'); $office->longitude = Input::get('longitude'); if (Input::hasFile('image')) { $officeImage = Input::file('image'); $destinationPath = 'img/uploads/'; $imageExtension = $officeImage->getClientOriginalExtension(); $fileName = uniqid() . '.' . $imageExtension; $officeImage->move($destinationPath, $fileName); if (File::exists($office->image)) { File::delete($office->image); } $office->image = "/" . $destinationPath . $fileName; } else { if ($office->image == null) { $office->image = 'http://placehold.it/225x150'; } } $office->save(); Session::flash('successMessage', 'Office was successfully saved'); return Redirect::action('OfficesController@index'); }
public function updateProfile($id) { $user = User::find($id); if ($user === null) { return $this->statusResponse(['errors' => 'No User found']); } // password if (Input::has('password') && Input::has('password_confirmation')) { $input = ['password' => Input::get('password'), 'password_confirmation' => Input::get('password_confirmation')]; $user->password = $input['password']; $user->password_confirmation = $input['password_confirmation']; } if (Input::has('firstname')) { $user->firstname = Input::get('firstname'); } if (Input::has('lastname')) { $user->lastname = Input::get('lastname'); } if (Input::has('office_location')) { $office = Office::find(Input::get('office_location')); if ($office) { $user->office()->associate($office); } if (Input::get('office_location') == -1) { $user->office_id = null; } } $user->hobby = Input::get('hobby'); $user->discipline = Input::get('discipline'); $user->onboarded = 1; if ($user->save()) { return $this->statusResponse(['notice' => 'Profile updated']); } return $this->statusResponse(['errors' => $user->errors()->all()]); }