@@ -1908,9 +1908,19 @@ function ($query) use ($search) {
19081908
19091909 public function scopeByFilter ($ query , array $ filters )
19101910 {
1911+ // Check if the filters are in Snipe-IT's original format (key => value)
1912+ if ($ this ->isLegacyFilterFormat ($ filters )) {
1913+ return $ this ->applyLegacyFilters ($ query , $ filters );
1914+ }
19111915 return $ this ->filterService ()->searchByFilter ($ query , $ filters );
19121916 }
19131917
1918+ private function isLegacyFilterFormat ($ filters )
1919+ {
1920+ // Snipe-IT filters are simple key/value (not arrays with 'field')
1921+ return !isset ($ filters [0 ]['field ' ]);
1922+ }
1923+
19141924 /**
19151925 * Query builder scope to order on model
19161926 *
@@ -2159,6 +2169,227 @@ public function scopeByDepreciationId($query, $search)
21592169 * @return \Illuminate\Database\Eloquent\Builder Modified query builder
21602170 */
21612171
2172+ public function applyLegacyFilters ($ query , $ filter )
2173+ {
2174+ return $ query ->where (
2175+ function ($ query ) use ($ filter ) {
2176+ foreach ($ filter as $ key => $ search_val ) {
2177+
2178+ $ fieldname = str_replace ('custom_fields. ' , '' , $ key );
2179+
2180+ if ($ fieldname == 'asset_tag ' ) {
2181+ $ query ->where ('assets.asset_tag ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2182+ }
2183+
2184+ if ($ fieldname == 'name ' ) {
2185+ $ query ->where ('assets.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2186+ }
2187+
2188+
2189+ if ($ fieldname =='serial ' ) {
2190+ $ query ->where ('assets.serial ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2191+ }
2192+
2193+ if ($ fieldname == 'purchase_date ' ) {
2194+ $ query ->where ('assets.purchase_date ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2195+ }
2196+
2197+ if ($ fieldname == 'purchase_cost ' ) {
2198+ $ query ->where ('assets.purchase_cost ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2199+ }
2200+
2201+ if ($ fieldname == 'notes ' ) {
2202+ $ query ->where ('assets.notes ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2203+ }
2204+
2205+ if ($ fieldname == 'order_number ' ) {
2206+ $ query ->where ('assets.order_number ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2207+ }
2208+
2209+ if ($ fieldname == 'status_label ' ) {
2210+ $ query ->whereHas (
2211+ 'assetstatus ' , function ($ query ) use ($ search_val ) {
2212+ $ query ->where ('status_labels.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2213+ }
2214+ );
2215+ }
2216+
2217+ if ($ fieldname == 'location ' ) {
2218+ $ query ->whereHas (
2219+ 'location ' , function ($ query ) use ($ search_val ) {
2220+ $ query ->where ('locations.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2221+ }
2222+ );
2223+ }
21622224
2225+ if ($ fieldname == 'rtd_location ' ) {
2226+ $ query ->whereHas (
2227+ 'defaultLoc ' , function ($ query ) use ($ search_val ) {
2228+ $ query ->where ('locations.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2229+ }
2230+ );
2231+ }
21632232
2164- }
2233+ if ($ fieldname == 'assigned_to ' ) {
2234+ $ query ->whereHasMorph (
2235+ 'assignedTo ' , [User::class], function ($ query ) use ($ search_val ) {
2236+ $ query ->where (
2237+ function ($ query ) use ($ search_val ) {
2238+ $ query ->where ('users.first_name ' , 'LIKE ' , '% ' .$ search_val .'% ' )
2239+ ->orWhere ('users.last_name ' , 'LIKE ' , '% ' .$ search_val .'% ' )
2240+ ->orWhere ('users.username ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2241+ }
2242+ );
2243+ }
2244+ )->orWhereHasMorph (
2245+ 'assignedTo ' , [Location::class], function ($ query ) use ($ search_val ) {
2246+ $ query ->where ('locations.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2247+ }
2248+ )->orWhereHasMorph (
2249+ 'assignedTo ' , [Asset::class], function ($ query ) use ($ search_val ) {
2250+ $ query ->where (
2251+ function ($ query ) use ($ search_val ) {
2252+ // Don't use the asset table prefix here because it will pull from the original asset,
2253+ // not the subselect we're doing here to get the assigned asset
2254+ $ query ->where ('name ' , 'LIKE ' , '% ' .$ search_val .'% ' )
2255+ ->orWhere ('asset_tag ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2256+ }
2257+ );
2258+ }
2259+ );
2260+ }
2261+
2262+
2263+ if ($ fieldname == 'manufacturer ' ) {
2264+ $ query ->whereHas (
2265+ 'model ' , function ($ query ) use ($ search_val ) {
2266+ $ query ->whereHas (
2267+ 'manufacturer ' , function ($ query ) use ($ search_val ) {
2268+ $ query ->where (
2269+ function ($ query ) use ($ search_val ) {
2270+ $ query ->where ('manufacturers.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2271+ }
2272+ );
2273+ }
2274+ );
2275+ }
2276+ );
2277+ }
2278+
2279+ if ($ fieldname == 'category ' ) {
2280+ $ query ->whereHas (
2281+ 'model ' , function ($ query ) use ($ search_val ) {
2282+ $ query ->whereHas (
2283+ 'category ' , function ($ query ) use ($ search_val ) {
2284+ $ query ->where (
2285+ function ($ query ) use ($ search_val ) {
2286+ $ query ->where ('categories.name ' , 'LIKE ' , '% ' .$ search_val .'% ' )
2287+ ->orWhere ('models.name ' , 'LIKE ' , '% ' .$ search_val .'% ' )
2288+ ->orWhere ('models.model_number ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2289+ }
2290+ );
2291+ }
2292+ );
2293+ }
2294+ );
2295+ }
2296+
2297+ if ($ fieldname == 'model ' ) {
2298+ $ query ->whereHas (
2299+ 'model ' , function ($ query ) use ($ search_val ) {
2300+ $ query ->where ('models.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2301+ }
2302+ );
2303+ }
2304+
2305+
2306+ if ($ fieldname == 'model_number ' ) {
2307+ $ query ->whereHas (
2308+ 'model ' , function ($ query ) use ($ search_val ) {
2309+ $ query ->where ('models.model_number ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2310+ }
2311+ );
2312+ }
2313+
2314+
2315+ if ($ fieldname == 'company ' ) {
2316+ $ query ->whereHas (
2317+ 'company ' , function ($ query ) use ($ search_val ) {
2318+ $ query ->where ('companies.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2319+ }
2320+ );
2321+ }
2322+
2323+ if ($ fieldname == 'supplier ' ) {
2324+ $ query ->whereHas (
2325+ 'supplier ' , function ($ query ) use ($ search_val ) {
2326+ $ query ->where ('suppliers.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2327+ }
2328+ );
2329+ }
2330+
2331+ if ($ fieldname == 'status_label ' ) {
2332+ $ query ->whereHas (
2333+ 'assetstatus ' , function ($ query ) use ($ search_val ) {
2334+ $ query ->where ('status_labels.name ' , 'LIKE ' , '% ' .$ search_val .'% ' );
2335+ }
2336+ );
2337+ }
2338+
2339+ if ($ fieldname == 'jobtitle ' ) {
2340+ $ query ->where (function ($ query ) use ($ search_val ) {
2341+ if (is_array ($ search_val )) {
2342+ $ query ->whereHasMorph (
2343+ 'assignedTo ' ,
2344+ [User::class],
2345+ function ($ query ) use ($ search_val ) {
2346+ $ query ->whereIn ('users.jobtitle ' , $ search_val );
2347+ }
2348+ );
2349+ } else {
2350+ $ query ->whereHasMorph (
2351+ 'assignedTo ' ,
2352+ [User::class],
2353+ function ($ query ) use ($ search_val ) {
2354+ $ query ->where (function ($ query ) use ($ search_val ) {
2355+ $ query ->where ('users.jobtitle ' , 'LIKE ' , '% ' . $ search_val . '% ' );
2356+ });
2357+ }
2358+ );
2359+ }
2360+ });
2361+ }
2362+
2363+
2364+ /**
2365+ * THIS CLUNKY BIT IS VERY IMPORTANT
2366+ *
2367+ * Although inelegant, this section matters a lot when querying against fields that do not
2368+ * exist on the asset table. There's probably a better way to do this moving forward, for
2369+ * example using the Schema:: methods to determine whether or not a column actually exists,
2370+ * or even just using the $searchableRelations variable earlier in this file.
2371+ *
2372+ * In short, this set of statements tells the query builder to ONLY query against an
2373+ * actual field that's being passed if it doesn't meet known relational fields. This
2374+ * allows us to query custom fields directly in the assets table
2375+ * (regardless of their name) and *skip* any fields that we already know can only be
2376+ * searched through relational searches that we do earlier in this method.
2377+ *
2378+ * For example, we do not store "location" as a field on the assets table, we store
2379+ * that relationship through location_id on the assets table, therefore querying
2380+ * assets.location would fail, as that field doesn't exist -- plus we're already searching
2381+ * against those relationships earlier in this method.
2382+ *
2383+ * - snipe
2384+ */
2385+
2386+ if (($ fieldname !='category ' ) && ($ fieldname !='model_number ' ) && ($ fieldname !='rtd_location ' ) && ($ fieldname !='location ' ) && ($ fieldname !='supplier ' )
2387+ && ($ fieldname !='status_label ' ) && ($ fieldname !='assigned_to ' ) && ($ fieldname !='model ' ) && ($ fieldname !='jobtitle ' ) && ($ fieldname !='company ' ) && ($ fieldname !='manufacturer ' )
2388+ ) {
2389+ $ query ->where ('assets. ' .$ fieldname , 'LIKE ' , '% ' . $ search_val . '% ' );
2390+ }
2391+ }
2392+ }
2393+ );
2394+ }
2395+ }
0 commit comments