user->isGuest) { $this->goHome (); } else { if (Yii::$app->session ['userSessionTimeout'] < time ()) { Yii::$app->user->logout (); $this->goHome (); Yii::$app->session->setFlash ( 'success', 'You have been logged out!' ); } else { Yii::$app->session->set ( 'userSessionTimeout', time () + Yii::$app->params ['sessionTimeoutSeconds'] ); return true; } } return true; // or false to not run the action } /* * A helper function for action Profile, which helps to determine, which Personal information sheet * is to be shown. This is necessary, because the administrator can of course change his own personal * information, but also the information of all users. The defineUser() function returns therefore * the identity-interface of the users whose profile is to be shown. */ public function defineUser() { if (isset ( Yii::$app->authManager->getRolesByUser ( Yii::$app->user->identity->__get ( 'id' ) ) ['user'] )) { $this->user = Yii::$app->user->identity; } elseif (isset ( Yii::$app->authManager->getRolesByUser ( Yii::$app->user->identity->__get ( 'id' ) ) ['admin'] )) { if (isset ( $_GET ['user'] )) { $this->user = User::findByUsername ( $_GET ['user'] ); } else { $this->user = Yii::$app->user->identity; } } else { throw new \ErrorException ( 'Only admins and logged in users can acces this site!' . Yii::$app->authManager->getRolesByUser ( Yii::$app->user->identity->__get ( 'id' ) ) == 'user' ); } // return true; } /** * renders the main page of the Profile-site. * ('profile.php'). * * @return Ambigous The page to be rendered. */ public function actionProfile() { $this->defineUser (); $model = new AccountForm ( $this->user ); if ($model->load ( Yii::$app->request->post () )) { $this->user->saveNewUserData ( $_POST ['AccountForm'] ); } return $this->render ( 'profile', [ 'model' => $model ] ); } /** * The to the view 'personal.php' belonging action. * In the first place it renders the * form to change the users personal information. Once validated it simply renders the * 'profile.php' view, which is the main panel and simply enumerates the user infomation. * * @return Ambigous */ public function actionPersonal() { $this->layout = 'form'; $this->defineUser (); $model = new AccountForm ( $this->user ); if ($model->load ( Yii::$app->request->post () ) && $model->validate ()) { $this->user->saveNewUserData ( $_POST ['AccountForm'] ); $this->layout = 'main'; return $this->render ( 'profile', [ 'model' => $model ] ); } return $this->render ( 'personal', [ 'model' => $model ] ); } /** * The to the view 'delivery.php' belonging action. * Once the form is validated you are * beeing redirected to the main panel of the profile, which simply shows off the users * profile information. * * @return Ambigous */ public function actionDelivery() { $this->layout = 'form'; $this->defineUser (); $model = new AccountForm ( $this->user ); if ($model->load ( Yii::$app->request->post () ) && $model->validate ()) { $this->user->saveNewUserData ( $_POST ['AccountForm'] ); $this->layout = 'main'; return $this->render ( 'profile', [ 'model' => $model ] ); } $countries = DBInterface::getCountries (); $countryList = [ ]; foreach ( $countries as $i ) { $countryList [$i ['iso']] = $i ['country']; } return $this->render ( 'delivery', [ 'model' => $model, 'countryList' => $countryList ] ); } /** * See actionDelivery() and actionPersonal() * * @return Ambigous */ public function actionAddress() { $this->layout = 'form'; $model = new AccountForm ( $this->user = Yii::$app->user->identity ); $this->defineUser (); $model = new AccountForm ( $this->user ); if ($model->load ( Yii::$app->request->post () ) && $model->validate ()) { $this->user->saveNewUserData ( $_POST ['AccountForm'] ); $this->layout = 'main'; return $this->render ( 'profile', [ 'model' => $model ] ); } $countries = DBInterface::getCountries (); $countryList = [ ]; foreach ( $countries as $i ) { $countryList [$i ['iso']] = $i ['country']; } return $this->render ( 'address', [ 'model' => $model, 'countryList' => $countryList ] ); } /** * actionSearches() renders the site where the user can see his saved searches and * the searches he subscribed to. * * @return Ambigous */ public function actionSearches() { $this->layout = 'main'; $connection = new \yii\db\Connection ( Yii::$app->userdb ); $connection->open (); // Get the users searches and subscriptions $query = (new Query ())->select ( '*' )->from ( 'user_searches' )->where ( [ 'user_id' => Yii::$app->user->id ] )->all (); $connection->close (); return $this->render ( 'searches', [ 'query' => $query ] ); } /** * actionSettings() renders the site where the user can change his personal settings, like * hits_per_page and order of the results table. */ public function actionSettings() { $this->layout = 'main'; $model = new AccountForm ( $this->user = Yii::$app->user->identity ); if ($model->load ( Yii::$app->request->post () ) && $model->validate ()) { $model->user->saveUserSettings ( $_POST ['AccountForm'] ); return $this->render ( 'settings', [ 'model' => $model, 'message' => 'Your settings have been saved' ] ); } return $this->render ( 'settings', [ 'model' => $model ] ); } /** * actionAdmin renders the site where the administator can change the personal information * of all users. * admin.php contains the lists with all active and rejected users. Users who are * not validated yet are only shown on the validation site which is controlled by the * ValidationController. * * @return Ambigous */ public function actionAdmin() { // $this->layout ='form'; if (! Yii::$app->user->can ( 'validateUser' )) { $this->redirect ( 'account/profile' ); } $unvalidated_users = User::findAll ( [ 'status' => User::STATUS_ACTIVE ] ); $active_users = User::findAll ( [ 'status' => User::STATUS_ACTIVE ] ); $rejected_users = User::findAll ( [ 'status' => User::STATUS_REJECTED ] ); return $this->render ( 'admin', [ 'unvalidated_users' => $unvalidated_users, 'active_users' => $active_users, 'rejected_users' => $rejected_users ] ); } public function actionActivate($username) { $user = User::findByUsername ( $username ); $user->validateUser (); return $this->redirect ( 'admin' ); } /* * These two functions allow the user to save a search or to subscribe to a search * by clicking the corresponding button. */ /** * This function renders the site "save" and calls functions which are necessary to save * the user search paramteres. * "save.php" contains only one line, because it * is mainly destined to be the response to a Pjax request. * * @return Ambigous */ public function actionSave() { // Save the information into the session, to be able to use Pjax, which is a // wrapper-widget for an Ajax request. $nb_records = Yii::$app->session ['records']; $url = Yii::$app->session->get ( 'search', NULL ); if (empty ( $url )) Yii::$app->session->setFlash ( 'error', 'The save action did not work - your session might have timed out' ); else { Yii::$app->session->remove ( 'search' ); Yii::$app->session->remove ( 'records' ); Yii::$app->user->identity->saveUserSearches ( $url, $nb_records, false ); return $this->render ( 'save' ); } } /** * This function renders the site "save" and calls functions which are necessary to save * and subscribe to the user search paramteres. * "save.php" contains only one line, because it * is mainly destined to be the response to a Pjax request. * * @return Ambigous */ public function actionSubscribe() { $nb_records = Yii::$app->session ['records']; $url = Yii::$app->session->get ( 'search', NULL ); if (empty ( $url )) Yii::$app->session->setFlash ( 'error', 'The subscription did not work - your session might have timed out' ); else { Yii::$app->session->remove ( 'search' ); Yii::$app->session->remove ( 'records' ); Yii::$app->user->identity->saveUserSearches ( $url, $nb_records, true ); return $this->render ( 'save' ); } } /* * The following three functions allow a smooth administration of the users searches. * As the names indicate the functions enable the user to delete, subscribe * and unsubscribe to a search. This is done by automatically generated links. There are * no actual sites which belong to the actions, all are redirected to the main panel, which * is the view "searches.php". */ public function actionDelete($search_id) { Yii::$app->user->identity->deleteUserSearch ( $search_id ); return $this->redirect ( 'searches' ); } public function actionUnsubscribe($search_id) { Yii::$app->user->identity->subscribeUserSearch ( $search_id, 0 ); return $this->redirect ( 'searches' ); } public function actionSubscribe_table($search_id) { Yii::$app->user->identity->subscribeUserSearch ( $search_id, 1 ); return $this->redirect ( 'searches' ); } /** * This function renders the 'recent orders' page. * It gets the unit_ids from * the shopping and creates an array using solr to get the missing information * like the country, institution, ect. * * @return Ambigous the page to be rendered. */ public function actionOrders() { $query = DBInterface::getPreviousOrders ( Yii::$app->user->id ); $result = [ ]; foreach ( $query as $id ) { $config = SOLRQueryManager::getConfigDetails (); $query1 = SOLRQueryManager::createSearchQuery ( $config ); $unitID = $id ['unit_id']; $collectioncode = $id ['collection_code']; $institutioncode = $id ['institution_code']; $unitID = str_replace ( " ", "\ ", $unitID ); $unitID = str_replace ( "(", "(", $unitID ); $unitID = str_replace ( ")", ")", $unitID ); $unitID = str_replace ( ":", ":", $unitID ); $collectioncode = str_replace ( " ", "\ ", $collectioncode ); $collectioncode = str_replace ( "(", "(", $collectioncode ); $collectioncode = str_replace ( ")", ")", $collectioncode ); $collectioncode = str_replace ( ":", ":", $collectioncode ); $institutioncode = str_replace ( " ", "\ ", $institutioncode ); $institutioncode = str_replace ( "(", "(", $institutioncode ); $institutioncode = str_replace ( ")", ")", $institutioncode ); $institutioncode = str_replace ( ":", ":", $institutioncode ); $fq = "{!join from=tripleidstoreid to=tripleidstoreid fromIndex=" . Yii::$app->params ['solrsearchname'] . "}"; $fq.= "unitID:" . $unitID." AND collectioncode:".$collectioncode. " AND institutioncode:".$institutioncode; $res = SOLRQueryManager::fillAndExecuteQuery ( $query1, '*:*', $fq, [ ], False ); foreach ($res as $r){ $result[$id['unit_id']]=array('order_date'=>$id['order_date'], 'unit_id'=>$id['unit_id'], 'fullScientificName'=>$r->fullScientificName, 'country'=>$r->country, 'instCode'=>$r->institution ); } } return $this->render('orders', ['query'=>$result]); } }