Advertisement
DataCCIW

cust_CCIW_get_universal_search

Nov 11th, 2020 (edited)
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.46 KB | None | 0 0
  1. USE [ArenaDB]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[cust_CCIW_get_universal_search]    Script Date: 11/17/2020 3:53:59 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author:      Tony Visconti/Ethan Jordan
  10. -- Create date: 11-11-2020
  11. -- Description: Search for various objects in Arena
  12. -- =============================================
  13. ALTER PROCEDURE [dbo].[cust_CCIW_get_universal_search] @SearchEntry VARCHAR(50)
  14.     ,@OrganizationID INT
  15.     ,@ProfilePage1 VARCHAR(5) = '25'
  16.     ,@ProfilePage2 VARCHAR(5) = '376'
  17.     ,@AttributePage VARCHAR(5) = '2294'
  18.     ,@ReportPage VARCHAR(5) = '3201'
  19.     ,@AttendancePage VARCHAR(5) = '1437'
  20.     ,@CommsPage VARCHAR(5) = '21'
  21.     ,@FormsPage VARCHAR(5) = '3512'
  22.     ,@FormTemplatePage VARCHAR(5) = '3508'
  23.     ,@GroupPage VARCHAR(5) = '2338'
  24.     ,@MissionsPage VARCHAR(5) = '3205'
  25.     ,@AssignmentsPage VARCHAR(5) = '3210'
  26.     ,@CurrentUser INT = -1
  27.     ,@GlobalAdminID INT = 43
  28.     ,@Filter VARCHAR(5)
  29. AS
  30. BEGIN
  31.    
  32.     DECLARE @IsGlobalAdmin BIT = 0,
  33.     @AnchorHTMLStart VARCHAR(50) = '<a href="./default.aspx?page='
  34.    
  35.  
  36.     IF EXISTS (
  37.             SELECT *
  38.             FROM secu_person_role R
  39.             WHERE R.person_id = @CurrentUser AND R.role_id = @GlobalAdminID
  40.             )
  41.     BEGIN
  42.         SET @IsGlobalAdmin = 1
  43.     END
  44.     IF @SearchEntry = ''
  45.         SELECT 'Please Enter a Search Term'
  46.     ELSE
  47.         SELECT *
  48.         FROM (
  49.             SELECT 'Tag' AS Item_Type
  50.                 ,@AnchorHTMLStart + CASE WHEN Obj.profile_type = 4 THEN @ProfilePage2 ELSE @ProfilePage1 END + '&profile=' + CONVERT(VARCHAR, Obj.profile_id) + '">' + profile_name + '</a>' AS ITEM
  51.                 ,3 AS [Object_Type]
  52.                 ,profile_id AS [Object_Key]
  53.                 , '' AS [Public_Flag]
  54.             FROM core_profile Obj
  55.             WHERE profile_name LIKE '%' + @SearchEntry + '%' OR profile_desc LIKE '%' + @SearchEntry + '%'
  56.            
  57.             UNION
  58.            
  59.             SELECT 'Attribute'
  60.                 ,@AnchorHTMLStart + @AttributePage + '&AttributeGroup=' + CONVERT(VARCHAR, Obj.attribute_group_id) + '">' + attribute_name + '</a>'
  61.                 ,4 AS [Object_Type]
  62.                 ,attribute_id AS [Object_Key]
  63.                 , '' AS [Public_Flag]
  64.             FROM core_attribute Obj
  65.             WHERE attribute_name LIKE '%' + @SearchEntry + '%'
  66.            
  67.             UNION
  68.            
  69.             SELECT 'List'
  70.                 ,@AnchorHTMLStart + @ReportPage + '&reportid=' + CONVERT(VARCHAR, Obj.report_id) + '&reporttype=' + CONVERT(VARCHAR, Obj.report_type_id) + '">' + report_name + '</a>'
  71.                 ,-2 AS [Object_Type]
  72.                 ,report_id AS [Object_Key]
  73.                 , OBJ.report_public_status AS [Public_Flag]
  74.             FROM list_save_reports Obj
  75.             left join secu_login l on l.login_id = obj.created_by
  76.             WHERE report_name LIKE '%' + @SearchEntry + '%' OR report_desc LIKE '%' + @SearchEntry + '%'
  77.            
  78.             UNION
  79.            
  80.             SELECT 'Attendance'
  81.                 ,@AnchorHTMLStart + @AttendancePage + '&Type=' + CONVERT(VARCHAR, Obj.occurrence_type_id) + '">' + [type_name] + '</a>'
  82.                 ,-3 AS [Object_Type]
  83.                 ,occurrence_type_id AS [Object_Key]
  84.                 , '' AS [Public_Flag]
  85.             FROM core_occurrence_type Obj
  86.             WHERE [type_name] LIKE '%' + @SearchEntry + '%'
  87.            
  88.             UNION
  89.            
  90.             SELECT Distinct 'Communications' --Distinct here eliminate duplicate results for individuals with multiple logins
  91.                 ,@AnchorHTMLStart + @CommsPage + '&communicationid=' + CONVERT(VARCHAR, Obj.communication_id) + '">' + [subject] + '</a>'
  92.                 ,-1 AS [Object_Type] -- Note The ability to view a list of previous emails is controlled via module security. Once you have read access to the mailmerge module you can read any previous communication if you know the communication id.
  93.                 ,l.person_id AS [Object_Key]
  94.                 , '' AS [Public_Flag]
  95.             FROM core_communication Obj
  96.             left join secu_login l on l.login_id = obj.created_by
  97.             WHERE [subject] LIKE '%' + @SearchEntry + '%' OR text_message LIKE '%' + @SearchEntry + '%'
  98.            
  99.             UNION
  100.            
  101.             SELECT 'Forms'
  102.                 ,@AnchorHTMLStart + @FormsPage + '&form=' + CONVERT(VARCHAR, Obj.form_id) + '">' + form_title + '</a>'
  103.                 ,-4 AS [Object_Type]
  104.                 ,form_id AS [Object_Key]
  105.                 , '' AS [Public_Flag]
  106.             FROM cust_kfs_custom_form Obj
  107.             WHERE form_title LIKE '%' + @SearchEntry + '%'
  108.            
  109.             UNION
  110.            
  111.             SELECT 'Form Templates'
  112.                 ,@AnchorHTMLStart + @FormTemplatePage + '&formtemplateid=' + CONVERT(VARCHAR, Obj.form_template_id) + '">' + title + '</a>'
  113.                 ,99 AS [Object_Type]
  114.                 ,form_template_id AS [Object_Key]
  115.                 , '' AS [Public_Flag]
  116.             FROM cust_kfs_custom_form_template Obj
  117.             WHERE title LIKE '%' + @SearchEntry + '%'
  118.            
  119.             UNION
  120.            
  121.             SELECT 'Groups'
  122.                 ,@AnchorHTMLStart + @GroupPage + '&group=' + CONVERT(VARCHAR, Obj.group_id) + '">' + group_name + '</a>'
  123.                 ,-5 AS [Object_Type]
  124.                 ,group_id AS [Object_Key]
  125.                 , '' AS [Public_Flag]
  126.             FROM smgp_group Obj
  127.             WHERE group_name LIKE '%' + @SearchEntry + '%'
  128.            
  129.             UNION
  130.            
  131.             SELECT 'Missions Trips'
  132.                 ,@AnchorHTMLStart + @MissionsPage + '&mission=' + CONVERT(VARCHAR, Obj.mission_id) + '">' + [name] + '</a>'
  133.                 ,-6 AS [Object_Type]
  134.                 ,mission_id AS [Object_Key]
  135.                 , '' AS [Public_Flag]
  136.             FROM trip_mission Obj
  137.             WHERE [name] LIKE '%' + @SearchEntry + '%' OR [description] LIKE '%' + @SearchEntry + '%'
  138.            
  139.             UNION
  140.            
  141.             SELECT 'Assignments'
  142.                 ,@AnchorHTMLStart + @AssignmentsPage + '&assignmentID=' + CONVERT(VARCHAR, Obj.assignment_id) + '">' + title + '</a>'
  143.                 ,13 AS [Object_Type]
  144.                 ,assignment_id AS [Object_Key]
  145.                 , '' AS [Public_Flag]
  146.             FROM asgn_assignment Obj
  147.             WHERE title LIKE '%' + @SearchEntry + '%' OR [description] LIKE '%' + @SearchEntry + '%'
  148.             ) Results
  149.             -- -1 Comms, -2 Lists, -3 Attendance, -4 Forms, -5 Groups, -6 Mission Trips
  150.         LEFT JOIN secu_v_person_permissions P ON (
  151.                 P.Object_type = Results.[Object_Type]
  152.                 AND p.Object_key = Results.[Object_Key]
  153.                 AND p.operation_type = 0 -- Read Permission
  154.                 AND p.person_id = @CurrentUser
  155.                 AND P.organization_id = @OrganizationID
  156.                 ) -- Find out which objects the user had direct read access to
  157.        WHERE
  158.         (( p.Object_type IS NOT NULL -- Where the user has read permission
  159.           OR Results.[Object_Type] < -1 -- Return all objects that don't directly use permissions minus Communications
  160.           OR (Results.[Object_Type] = -1 AND @CurrentUser = Results.[Object_Key]) -- Return Communications if they were created by the CurrentUser executing the search
  161.         )
  162.         OR @IsGlobalAdmin = 1) -- If the user is a global admin return all results
  163.         AND ((@Filter = '') OR (Results.Object_Type = CONVERT(INT,@Filter)))-- Allows Filter to be NULL or Filter on Object Type
  164.         AND (Results.Public_Flag = 1 OR @IsGlobalAdmin = 1)
  165.         ORDER BY Item_Type
  166. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement