PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/controllers/PersonController.php

https://gitlab.com/Griffolion/Final-Year-Project
PHP | 339 lines | 247 code | 32 blank | 60 comment | 33 complexity | df32b391ffcb613cb8c3bd5b135372fd MD5 | raw file
  1. <?php
  2. class PersonController extends RController {
  3. /**
  4. * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  5. * using two-column layout. See 'protected/views/layouts/column2.php'.
  6. */
  7. public $layout = '//layouts/column1';
  8. /**
  9. * @return array action filters
  10. */
  11. public function filters() {
  12. return array(
  13. 'accessControl', // perform access control for CRUD operations
  14. 'postOnly + delete', // we only allow deletion via POST request
  15. 'rights', // rights management
  16. );
  17. }
  18. /**
  19. * Specifies the access control rules.
  20. * This method is used by the 'accessControl' filter.
  21. * @return array access control rules
  22. */
  23. public function accessRules() {
  24. return array(
  25. array('allow', // allow all users to perform 'index' and 'view' actions
  26. 'actions' => array('index', 'view', 'loadmodelajax'),
  27. 'users' => array('*'),
  28. ),
  29. array('allow', // allow authenticated user to perform 'create' and 'update' actions
  30. 'actions' => array('create', 'update', 'updateChild', 'newFM', 'newEA', 'newLE', 'newMI', 'DeleteLinkRecord'),
  31. 'users' => array('@'),
  32. ),
  33. array('allow', // allow admin user to perform 'admin' and 'delete' actions
  34. 'actions' => array('admin', 'delete'),
  35. 'users' => array('admin'),
  36. ),
  37. array('deny', // deny all users
  38. 'users' => array('*'),
  39. ),
  40. );
  41. }
  42. /**
  43. * Displays a particular model.
  44. * @param integer $id the ID of the model to be displayed
  45. */
  46. public function actionView($id) {
  47. $this->render('view', array(
  48. 'model' => $this->loadModel($id),
  49. ));
  50. }
  51. public function actionDeleteLinkRecord($thisID, $linkData, $linkName, $Entity) {
  52. if (!Yii::app()->request->isPostRequest || !Yii::app()->request->isAjaxRequest) {
  53. throw new CHttpException(400, 'Invalid request.');
  54. }
  55. $criteria = new CDbCriteria();
  56. $criteria->addCondition('Person = ' . $thisID . ' && ' . $linkName . ' = ' . $linkData);
  57. $toDelete;
  58. switch ($Entity) {
  59. case "Event":
  60. $toDelete = EventAttendance::model()->find($criteria);
  61. break;
  62. case "Family":
  63. $toDelete = FamilyMembership::model()->find($criteria);
  64. break;
  65. case "LifeEvent":
  66. $toDelete = LifeEventOccurrance::model()->find($criteria);
  67. break;
  68. case "Ministry":
  69. $toDelete = MinistryInvolvement::model()->find($criteria);
  70. break;
  71. case "Contribution":
  72. $toDelete = Contribution::model()->find($criteria);
  73. break;
  74. default:
  75. throw new CHttpException(400, 'Entity not recognised');
  76. }
  77. $toDelete->delete();
  78. }
  79. /**
  80. * Creates a new model.
  81. * If creation is successful, the browser will be redirected to the 'view' page.
  82. */
  83. public function actionCreate() {
  84. $model = new Person;
  85. // Uncomment the following line if AJAX validation is needed
  86. // $this->performAjaxValidation($model);
  87. if (isset($_POST['Person'])) {
  88. $params = array();
  89. parse_str($_POST['Person'], $params); // Parsing JSON object back to PHP array
  90. $model->attributes = $params['Person']; // Massive assignment to model from JSON parsed array
  91. if ($model->validate()) {
  92. $command = Yii::app()->db->createCommand();
  93. $command->insert('person', // $model->save() didn't work, threw that memory leak error we saw before
  94. array( 'Title' => $model->Title // So we had to resort to the good old fashioned way
  95. , 'firstName' => $model->firstName
  96. , 'middleName' => $model->middleName
  97. , 'lastName' => $model->lastName
  98. , 'DOB' => $model->DOB
  99. , 'Address1' => $model->Address1
  100. , 'Address2' => $model->Address2
  101. , 'Address3' => $model->Address3
  102. , 'City' => $model->City
  103. , 'ZIP' => $model->ZIP
  104. , 'State' => $model->State
  105. , 'Occupation' => $model->Occupation
  106. , 'homePhone' => $model->homePhone
  107. , 'cellPhone' => $model->cellPhone
  108. , 'workPhone' => $model->workPhone
  109. , 'homeEmail' => $model->homeEmail
  110. , 'workEmail' => $model->workEmail
  111. , 'memberStatus' => $model->memberStatus
  112. , 'dateJoined' => $model->dateJoined
  113. , 'Gender' => $model->Gender
  114. , 'maritalStatus' => $model->maritalStatus
  115. , 'Notes' => $model->Notes
  116. , 'Active' => $model->Active,));
  117. /* To my knowledge, there's no decent way to get back a full
  118. * Person model after inserting to DB, so I had to get the model
  119. * by selecting the row with the latest dateCreated stamp */
  120. $Details = Person::model()->findBySql("SELECT * FROM person "
  121. . "ORDER BY dateCreated DESC "
  122. . "LIMIT 1;");
  123. /* Why couldn't I just put through $model? Good question, all I
  124. * know is that it didn't work */
  125. $this->renderPartial('_viewAjax', array(
  126. 'Details' => $Details,
  127. 'relatedData' => $Details->pullRelated(),
  128. 'createSuccess' => true,
  129. ),false, true);
  130. } else {
  131. print_r($params);
  132. echo "failure";
  133. }
  134. die();
  135. }
  136. //If wanting to create a new record
  137. if (isset($_POST['create'])) {
  138. $model = new Person();
  139. $this->renderPartial('_form', array(
  140. 'model' => $model,
  141. ),false, true);
  142. }
  143. }
  144. /**
  145. * Updates a particular model.
  146. * If update is successful, the browser will be redirected to the 'view' page.
  147. * @param integer $id the ID of the model to be updated
  148. */
  149. public function actionUpdate() {
  150. if (isset($_POST['scenario'])) {
  151. // Getting the values from Yii::app() request functionality, rather straight from _POST
  152. $val = Yii::app()->request->getParam('value');
  153. $pk = Yii::app()->request->getParam('pk');
  154. $attribute = Yii::app()->request->getParam('name');
  155. if (empty($attribute)) { // Throw exception if the attribute name is undefined for whatever reason - if this happens, it might be an error with js
  156. throw new CException(Yii::t('TbEditableSaver.editable', 'Property "attribute" should be defined.'));
  157. }
  158. if (empty($pk)) { // Throw exception if the primary key is undefined for whatever reason - if this happens, it might be an error with js
  159. throw new CException(Yii::t('TbEditableSaver.editable', 'Property "primaryKey" should be defined.'));
  160. }
  161. $model = CActiveRecord::model('Person')->findByPk($pk); // Initialise a model straight to the existing one based on the PK
  162. $model->setAttribute($attribute, $val); // Set the attribute in question to the new value
  163. $model->validate(); // Validate the model so as to generate any potential new errors from the attribute change
  164. if ($model->hasErrors()) { // If the model contains errors (boolean)
  165. $msg = ''; // String initialised to contain error messages
  166. foreach ($model->getErrors($attribute) as $attribute => $error) { // For each error for the attribute in question
  167. $msg .= $error . "\n"; // Append msg with the error and a new line tag so as to display multiple potential errors
  168. }
  169. throw new CHttpException(400, $msg); // Throw a new CHttpException with our message
  170. }
  171. //If model validated with the new value
  172. $command = Yii::app()->db->createCommand(); // create a new DB command
  173. // Update the database
  174. $command->update('person', // UPDATE tablename
  175. array($attribute => $val,), // ATTRIBUTES ...
  176. 'ID = :id', // WHERE ...
  177. array(':id' => $pk)); // VALUES ...
  178. }
  179. }
  180. /**
  181. * Deletes a particular model.
  182. * If deletion is successful, the browser will load in the AJAX deletion page.
  183. * @param integer $id the ID of the model to be deleted
  184. */
  185. public function actionDelete() {
  186. // we only allow deletion via POST request
  187. if (isset($_POST["pk"])) {
  188. $p = CActiveRecord::model('Person')->findByPk($_POST['pk']);
  189. if ($p->Active == 'Inactive') {
  190. throw new CHttpException(400,'Person is already deactivated');
  191. }
  192. else {
  193. $p->setAttribute('Active', 'Inactive');
  194. if ($p->save()) {
  195. $this->renderPartial('_viewAjaxDeleted', array(
  196. 'deleted' => true
  197. ),false, true);
  198. }
  199. else {
  200. throw new CHttpException(400,'Error saving change back to database');
  201. }
  202. }
  203. }
  204. else {
  205. throw new CHttpException(400,'Delete denied: request must be POST');
  206. }
  207. }
  208. /**
  209. * Lists all models.
  210. */
  211. public function actionIndex() {
  212. $dataProvider = new CActiveDataProvider('Person');
  213. $model = new Person('search');
  214. $model->unsetAttributes();
  215. if (isset($_GET['Person'])) {
  216. $model->attributes = $_GET['Person'];
  217. }
  218. $this->render('index', array('model' => $model,
  219. 'dataProvider' => $dataProvider
  220. ));
  221. }
  222. public function actionNewFM() {
  223. if (isset($_POST['Family']) && isset($_POST['Person'])) {
  224. $Link = new FamilyMembership();
  225. $Link->Family = $_POST['Family'];
  226. $Link->Person = $_POST['Person'];
  227. $Link->save();
  228. }
  229. }
  230. public function actionNewMI() {
  231. if (isset($_POST['Ministry']) && isset($_POST['Person']) && isset($_POST['Capacity'])) {
  232. $Link = new MinistryInvolvement();
  233. $Link->setAttribute('Ministry', $_POST['Ministry']);
  234. $Link->setAttribute('Person', $_POST['Person']);
  235. $Link->setAttribute('Active', 'Active');
  236. $Link->setAttribute('Capacity',$_POST['Capacity']);
  237. $Link->save();
  238. }
  239. }
  240. public function actionNewEA() {
  241. if (isset($_POST['Event']) && isset($_POST['Person'])) {
  242. $Link = new EventAttendance();
  243. $Link->Event = $_POST['Event'];
  244. $Link->Person = $_POST['Person'];
  245. $Link->save();
  246. }
  247. }
  248. public function actionNewLE() {
  249. if (isset($_POST['lifeEvent']) && isset($_POST['Person'])) {
  250. $Link = new LifeEventOccurrance();
  251. $Link->lifeEvent = $_POST['lifeEvent'];
  252. $Link->Person = $_POST['Person'];
  253. $Link->save();
  254. }
  255. }
  256. public function actionUpdateChild() {
  257. // Wanting to view an existing person
  258. if (isset($_POST['pk'])) {
  259. $Details = Person::model()->findByPk($_POST['pk']);
  260. $relatedData = $Details->pullRelated();
  261. $this->renderPartial('_viewAjax', array(
  262. 'Details' => $Details,
  263. 'relatedData' => $relatedData,
  264. ),false, true);
  265. }
  266. }
  267. /**
  268. * Manages all models.
  269. */
  270. public function actionAdmin() {
  271. $model = new Person('search');
  272. $model->unsetAttributes(); // clear any default values
  273. if (isset($_GET['Person'])) {
  274. $model->attributes = $_GET['Person'];
  275. }
  276. $this->render('admin', array(
  277. 'model' => $model,
  278. ));
  279. }
  280. public function actionLoadModelAjax($id) {
  281. $this->renderPartial('_viewAjax', array('model' => $this->loadModel($id)), false, true);
  282. }
  283. /**
  284. * Returns the data model based on the primary key given in the GET variable.
  285. * If the data model is not found, an HTTP exception will be raised.
  286. * @param integer $id the ID of the model to be loaded
  287. * @return Person the loaded model
  288. * @throws CHttpException
  289. */
  290. public function loadModel($id) {
  291. $model = Person::model()->findByPk($id);
  292. if ($model === null) {
  293. throw new CHttpException(404, 'The requested page does not exist.');
  294. }
  295. return $model;
  296. }
  297. /**
  298. * Performs the AJAX validation.
  299. * @param Person $model the model to be validated
  300. */
  301. protected function performAjaxValidation($model) {
  302. if (isset($_POST['ajax']) && $_POST['ajax'] === 'person-form') {
  303. echo CActiveForm::validate($model);
  304. Yii::app()->end();
  305. }
  306. }
  307. }