Guest User

Untitled

a guest
Apr 29th, 2019
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use Yii;
  6. use app\models\Attendence;
  7. use app\models\AttendenceSearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11.  
  12. /**
  13. * AttendenceController implements the CRUD actions for Attendence model.
  14. */
  15. class AttendenceController extends Controller
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function behaviors()
  21. {
  22. return [
  23. 'verbs' => [
  24. 'class' => VerbFilter::className(),
  25. 'actions' => [
  26. 'delete' => ['POST'],
  27. ],
  28. ],
  29. ];
  30. }
  31.  
  32.  
  33. /**
  34. * Lists all Attendence models.
  35. * @return mixed
  36. */
  37.  
  38. public function actionIndex()
  39. {
  40. $searchModel = new AttendenceSearch();
  41. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  42.  
  43. return $this->render('index', [
  44. 'searchModel' => $searchModel,
  45. 'dataProvider' => $dataProvider,
  46. ]);
  47. }
  48.  
  49. /**
  50. * Displays a single Attendence model.
  51. * @param integer $id
  52. * @return mixed
  53. * @throws NotFoundHttpException if the model cannot be found
  54. */
  55. public function actionView()
  56. {
  57. //$request = Yii::$app->request;
  58. //$id = $request->post('id');
  59. $headers = Yii::$app->request->headers;
  60. $id = $headers->get('id');
  61. $cmd = Yii::$app->db->createCommand('SELECT * FROM attendence WHERE id = :id');
  62. $post = $cmd->bindParam(':id', $id)->queryOne();
  63. $response = Yii::$app->response;
  64. $response->format = \yii\web\Response::FORMAT_JSON;
  65. $response->data = ['data' => $post ];
  66. }
  67.  
  68. public function actionView1($id)
  69. {
  70.  
  71. $cmd = Yii::$app->db->createCommand('SELECT * FROM attendence WHERE id = :id');
  72. $post = $cmd->bindParam(':id', $id)->queryOne();
  73. return $this->render('post',['dataProvider' => $post]);
  74. }
  75.  
  76. public function actionView2($id)
  77. {
  78. if($id == 'error1'){
  79. return $this->render('post',['dataProvider' => 'Error logging in:Invalid username or password' ]);
  80. }
  81. if($id == 'error2'){
  82. return $this->render('post',['dataProvider' => 'Error logging in:Invalid ' ]);
  83. }
  84. $cmd = Yii::$app->db->createCommand('SELECT * FROM attendence WHERE id = :id');
  85. $post = $cmd->bindParam(':id', $id)->queryOne();
  86. return $this->render('post',['dataProvider' => $post]);
  87. }
  88.  
  89.  
  90. /**
  91. * Creates a new Attendence model.
  92. * If creation is successful, the browser will be redirected to the 'view' page.
  93. * @return mixed
  94. */
  95. public function actionCreate()
  96. {
  97. $model = new Attendence();
  98.  
  99. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  100. return $this->redirect(['view', 'id' => $model->id]);
  101. }
  102.  
  103. return $this->render('create', [
  104. 'model' => $model,
  105. ]);
  106. }
  107.  
  108. public function actionPost()
  109. {
  110.  
  111. $request = Yii::$app->request;
  112. $username = $request->post('username');
  113. $password = $request->post('password');
  114. $error= 'error';
  115.  
  116. if(isset($username) && isset($password)){
  117. $cmd = Yii::$app->db->createCommand('SELECT * FROM attendence
  118. WHERE username = :username AND password = :password ');
  119. $post = $cmd->bindParam(':username', $username)
  120. ->bindParam(':password', $password )
  121. ->queryOne();
  122. //emplogin.php in views
  123.  
  124. if($post != false)
  125. return $this->redirect(['view2', 'id' => $post->id]);
  126. else
  127. return $this->redirect(['view2', 'id' => 'error1']);
  128. }
  129. else
  130. return $this->render('test');
  131. }
  132.  
  133. public function actionDocument() {
  134.  
  135. $model = new Attendence();
  136.  
  137. if($model->load(Yii::$app->request->post())){
  138. if ($model->validate()) {
  139. // all inputs are valid
  140. $username = $model->username;
  141. $password = $model->password;
  142. $cmd = Yii::$app->db->createCommand('SELECT * FROM attendence
  143. WHERE username = :username
  144. AND password = :password ');
  145. $post = $cmd->bindParam(':username', $username)
  146. ->bindParam(':password', $password)
  147. ->queryOne();
  148. return $this->render('post',['dataProvider' => $username]);
  149. }
  150. else {
  151. // validation failed: $errors is an array containing error messages
  152.  
  153. $errors = $model->errors;
  154. return $this->render('post',['dataProvider' => $errors]);
  155. }
  156.  
  157. }
  158.  
  159. return $this->render('document.php', [
  160. 'model' => $model,
  161. ]);
  162. }
  163.  
  164. /**
  165. * Updates an existing Attendence model.
  166. * If update is successful, the browser will be redirected to the 'view' page.
  167. * @param integer $id
  168. * @return mixed
  169. * @throws NotFoundHttpException if the model cannot be found
  170. */
  171. public function actionUpdate($id)
  172. {
  173. $model = $this->findModel($id);
  174.  
  175. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  176. return $this->redirect(['view', 'id' => $model->id]);
  177. }
  178.  
  179. return $this->render('update', [
  180. 'model' => $model,
  181. ]);
  182. }
  183.  
  184. /**
  185. * Deletes an existing Attendence model.
  186. * If deletion is successful, the browser will be redirected to the 'index' page.
  187. * @param integer $id
  188. * @return mixed
  189. * @throws NotFoundHttpException if the model cannot be found
  190. */
  191. public function actionDelete($id)
  192. {
  193. $this->findModel($id)->delete();
  194.  
  195. return $this->redirect(['index']);
  196. }
  197.  
  198. /**
  199. * Finds the Attendence model based on its primary key value.
  200. * If the model is not found, a 404 HTTP exception will be thrown.
  201. * @param integer $id
  202. * @return Attendence the loaded model
  203. * @throws NotFoundHttpException if the model cannot be found
  204. */
  205. protected function findModel($id)
  206. {
  207. if (($model = Attendence::findOne($id)) !== null) {
  208. return $model;
  209. }
  210.  
  211. throw new NotFoundHttpException('The requested page does not exist.');
  212. }
  213. }
Add Comment
Please, Sign In to add comment