From 4472269f3c78b437526c54539ba3fcdeba15d2f1 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 12 May 2023 14:06:53 -0400 Subject: [PATCH] Finish roughing in UserPreference in api --- web/api/app/Config/routes.php | 1 + .../Controller/UserPreferenceController.php | 50 +++++++++++++------ web/api/app/Model/UserPreference.php | 2 +- web/api/app/View/UserPreference/json/add.ctp | 1 + .../json/edit.ctp | 0 .../json/index.ctp | 0 .../json/view.ctp | 0 .../xml/add.ctp | 0 .../xml/edit.ctp | 0 .../xml/index.ctp | 0 .../xml/view.ctp | 0 web/api/app/View/UserPreferences/json/add.ctp | 1 - 12 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 web/api/app/View/UserPreference/json/add.ctp rename web/api/app/View/{UserPreferences => UserPreference}/json/edit.ctp (100%) rename web/api/app/View/{UserPreferences => UserPreference}/json/index.ctp (100%) rename web/api/app/View/{UserPreferences => UserPreference}/json/view.ctp (100%) rename web/api/app/View/{UserPreferences => UserPreference}/xml/add.ctp (100%) rename web/api/app/View/{UserPreferences => UserPreference}/xml/edit.ctp (100%) rename web/api/app/View/{UserPreferences => UserPreference}/xml/index.ctp (100%) rename web/api/app/View/{UserPreferences => UserPreference}/xml/view.ctp (100%) delete mode 100644 web/api/app/View/UserPreferences/json/add.ctp diff --git a/web/api/app/Config/routes.php b/web/api/app/Config/routes.php index 91be6d6ec..efb102add 100644 --- a/web/api/app/Config/routes.php +++ b/web/api/app/Config/routes.php @@ -40,6 +40,7 @@ Router::mapResources('snapshots'); Router::mapResources('states'); Router::mapResources('users'); + Router::mapResources('user_preference'); Router::mapResources('zonepresets'); Router::mapResources('zones'); diff --git a/web/api/app/Controller/UserPreferenceController.php b/web/api/app/Controller/UserPreferenceController.php index 9ad9b8c4f..f41fb52ef 100644 --- a/web/api/app/Controller/UserPreferenceController.php +++ b/web/api/app/Controller/UserPreferenceController.php @@ -18,7 +18,7 @@ class UserPreferenceController extends AppController { parent::beforeFilter(); global $user; # We already tested for auth in appController, so we just need to test for specific permission - $canView = (!$user) || ($user->Users() != 'None'); + $canView = $user && ($user->System() != 'None'); if (!$canView) { throw new UnauthorizedException(__('Insufficient Privileges')); return; @@ -42,10 +42,10 @@ class UserPreferenceController extends AppController { $find_array = array( 'conditions' => &$conditions, ); - $user_preference = $this->UserPreference->find('all', $find_array); + $user_preferences = $this->UserPreference->find('all', $find_array); $this->set(array( - 'user_preference' => $user_preference, - '_serialize' => array('user_preference') + 'user_preferences' => $user_preferences, + '_serialize' => array('user_preferences') )); } @@ -59,9 +59,9 @@ class UserPreferenceController extends AppController { public function view($id = null) { $this->UserPreference->recursive = -1; if (!$this->UserPreference->exists($id)) { - throw new NotFoundException(__('Invalid user data')); + throw new NotFoundException(__('Invalid user preference')); } - $options = array('conditions' => array('User_Data.' . $this->UserPreference->primaryKey => $id)); + $options = array('conditions' => array('User_Preferences.' . $this->UserPreference->primaryKey => $id)); $user_preference = $this->UserPreference->find('first', $options); $this->set(array( 'user_preference' => $user_preference, @@ -75,13 +75,33 @@ class UserPreferenceController extends AppController { * @return void */ public function add() { + $data = $this->request->data; + if ($this->RequestHandler->requestedWith('json')) { + $data = $this->request->input('json_decode', true) ; + } + $message = ''; if ($this->request->is('post')) { - $this->UserPreference->create(); - if ($this->UserPreference->save($this->request->data)) { - } - } - $users = $this->UserPreference->User->find('list'); - $this->set(compact('users')); + $exists = $this->UserPreference->find('first', ['conditions'=>['UserId'=>$data['UserId'],'Name'=>$data['Name']]]); + if ($exists) { + $this->UserPreference->id = $exists['UserPreference']['Id']; + $rc = $this->UserPreference->save($data); + } else { + $this->UserPreference->create(); + $rc = $this->UserPreference->save($data); + } + if ($rc) { + $message = 'Success'; + } else { + $message = 'Failure'; + ZM\Warning($this->validationErrors); + } + } else { + ZM\Error("NOT POST in add()"); + } + $this->set(array( + 'message' => $message, + '_serialize' => array('message') + )); } /** @@ -99,11 +119,11 @@ class UserPreferenceController extends AppController { if ($this->UserPreference->save($this->request->data)) { } } else { - $options = array('conditions' => array('User_Data.' . $this->UserPreference->primaryKey => $id)); + $options = array('conditions' => array('User_Preferences.'.$this->UserPreference->primaryKey => $id)); $this->request->data = $this->UserPreference->find('first', $options); } - $users = $this->UserPreference->User->find('list'); - $this->set(compact('users')); + $preference = $this->UserPreference; + $this->set(compact('user_preference')); } /** diff --git a/web/api/app/Model/UserPreference.php b/web/api/app/Model/UserPreference.php index 5c3971455..80a250fae 100644 --- a/web/api/app/Model/UserPreference.php +++ b/web/api/app/Model/UserPreference.php @@ -12,7 +12,7 @@ class UserPreference extends AppModel { * * @var mixed False or table name */ - public $useTable = 'User_Data'; + public $useTable = 'User_Preferences'; /** * Primary key field diff --git a/web/api/app/View/UserPreference/json/add.ctp b/web/api/app/View/UserPreference/json/add.ctp new file mode 100644 index 000000000..779ccd629 --- /dev/null +++ b/web/api/app/View/UserPreference/json/add.ctp @@ -0,0 +1 @@ +echo json_encode($message); diff --git a/web/api/app/View/UserPreferences/json/edit.ctp b/web/api/app/View/UserPreference/json/edit.ctp similarity index 100% rename from web/api/app/View/UserPreferences/json/edit.ctp rename to web/api/app/View/UserPreference/json/edit.ctp diff --git a/web/api/app/View/UserPreferences/json/index.ctp b/web/api/app/View/UserPreference/json/index.ctp similarity index 100% rename from web/api/app/View/UserPreferences/json/index.ctp rename to web/api/app/View/UserPreference/json/index.ctp diff --git a/web/api/app/View/UserPreferences/json/view.ctp b/web/api/app/View/UserPreference/json/view.ctp similarity index 100% rename from web/api/app/View/UserPreferences/json/view.ctp rename to web/api/app/View/UserPreference/json/view.ctp diff --git a/web/api/app/View/UserPreferences/xml/add.ctp b/web/api/app/View/UserPreference/xml/add.ctp similarity index 100% rename from web/api/app/View/UserPreferences/xml/add.ctp rename to web/api/app/View/UserPreference/xml/add.ctp diff --git a/web/api/app/View/UserPreferences/xml/edit.ctp b/web/api/app/View/UserPreference/xml/edit.ctp similarity index 100% rename from web/api/app/View/UserPreferences/xml/edit.ctp rename to web/api/app/View/UserPreference/xml/edit.ctp diff --git a/web/api/app/View/UserPreferences/xml/index.ctp b/web/api/app/View/UserPreference/xml/index.ctp similarity index 100% rename from web/api/app/View/UserPreferences/xml/index.ctp rename to web/api/app/View/UserPreference/xml/index.ctp diff --git a/web/api/app/View/UserPreferences/xml/view.ctp b/web/api/app/View/UserPreference/xml/view.ctp similarity index 100% rename from web/api/app/View/UserPreferences/xml/view.ctp rename to web/api/app/View/UserPreference/xml/view.ctp diff --git a/web/api/app/View/UserPreferences/json/add.ctp b/web/api/app/View/UserPreferences/json/add.ctp deleted file mode 100644 index 911ffeaad..000000000 --- a/web/api/app/View/UserPreferences/json/add.ctp +++ /dev/null @@ -1 +0,0 @@ -echo json_encode($user_preference);