From 94b342b49bcc2f06cd6bfdd6d1449a70a831f887 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Sat, 29 Nov 2014 04:48:42 +0000
Subject: [PATCH] forms: Remove object specific routines

Add generic routing to get object's dynamic form entries.
---
 include/ajax.orgs.php           |  4 ++--
 include/ajax.users.php          |  4 ++--
 include/class.dynamic_forms.php | 14 ++------------
 include/class.user.php          |  2 +-
 4 files changed, 7 insertions(+), 17 deletions(-)

diff --git a/include/ajax.orgs.php b/include/ajax.orgs.php
index b8309a55e..aa6937bc3 100644
--- a/include/ajax.orgs.php
+++ b/include/ajax.orgs.php
@@ -255,7 +255,7 @@ class OrgsAjaxAPI extends AjaxController {
     }
 
     function manageForms($org_id) {
-        $forms = DynamicFormEntry::forOrganization($org_id);
+        $forms = DynamicFormEntry::forObject($org_id, 'O');
         $info = array('action' => '#orgs/'.Format::htmlchars($org_id).'/forms/manage');
         include(STAFFINC_DIR . 'templates/form-manage.tmpl.php');
     }
@@ -271,7 +271,7 @@ class OrgsAjaxAPI extends AjaxController {
             Http::response(422, "Send updated forms list");
 
         // Add new forms
-        $forms = DynamicFormEntry::forOrganization($org_id);
+        $forms = DynamicFormEntry::forObject($org_id, 'O');
         foreach ($_POST['forms'] as $sort => $id) {
             $found = false;
             foreach ($forms as $e) {
diff --git a/include/ajax.users.php b/include/ajax.users.php
index f211ff6e6..02a3b1668 100644
--- a/include/ajax.users.php
+++ b/include/ajax.users.php
@@ -412,7 +412,7 @@ class UsersAjaxAPI extends AjaxController {
     }
 
     function manageForms($user_id) {
-        $forms = DynamicFormEntry::forUser($user_id);
+        $forms = DynamicFormEntry::forObject($user_id, 'U');
         $info = array('action' => '#users/'.Format::htmlchars($user_id).'/forms/manage');
         include(STAFFINC_DIR . 'templates/form-manage.tmpl.php');
     }
@@ -428,7 +428,7 @@ class UsersAjaxAPI extends AjaxController {
             Http::response(422, "Send updated forms list");
 
         // Add new forms
-        $forms = DynamicFormEntry::forUser($user_id);
+        $forms = DynamicFormEntry::forObject($user_id, 'U');
         foreach ($_POST['forms'] as $sort => $id) {
             $found = false;
             foreach ($forms as $e) {
diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 7c9e84b51..53f3760e0 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -920,11 +920,6 @@ class DynamicFormEntry extends VerySimpleModel {
         $this->object_id = $ticket_id;
     }
 
-    function forClient($user_id) {
-        return DynamicFormEntry::objects()
-            ->filter(array('object_id'=>$user_id, 'object_type'=>'U'));
-    }
-
     function setClientId($user_id) {
         $this->object_type = 'U';
         $this->object_id = $user_id;
@@ -934,14 +929,9 @@ class DynamicFormEntry extends VerySimpleModel {
         $this->object_id = $object_id;
     }
 
-    function forUser($user_id) {
-        return DynamicFormEntry::objects()
-            ->filter(array('object_id'=>$user_id, 'object_type'=>'U'));
-    }
-
-    function forOrganization($org_id) {
+    function forObject($object_id, $object_type) {
         return DynamicFormEntry::objects()
-            ->filter(array('object_id'=>$org_id, 'object_type'=>'O'));
+            ->filter(array('object_id'=>$object_id, 'object_type'=>$object_type));
     }
 
     function render($staff=true, $title=false, $options=array()) {
diff --git a/include/class.user.php b/include/class.user.php
index 8e6382e9d..001000437 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -287,7 +287,7 @@ class User extends UserModel {
 
     function getDynamicData($create=true) {
         if (!isset($this->_entries)) {
-            $this->_entries = DynamicFormEntry::forClient($this->id)->all();
+            $this->_entries = DynamicFormEntry::forObject($this->id, 'U')->all();
             if (!$this->_entries && $create) {
                 $g = UserForm::getNewInstance();
                 $g->setClientId($this->id);
-- 
GitLab