]> BookStack Code Mirror - bookstack/blob - app/helpers.php
0b9a6afc6347a420185e917d6a7547921bd0fd84
[bookstack] / app / helpers.php
1 <?php
2
3 use BookStack\Ownable;
4
5 if (!function_exists('versioned_asset')) {
6     /**
7      * Get the path to a versioned file.
8      *
9      * @param  string $file
10      * @return string
11      *
12      * @throws \InvalidArgumentException
13      */
14     function versioned_asset($file)
15     {
16         static $manifest = null;
17
18         if (is_null($manifest)) {
19             $manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true);
20         }
21
22         if (isset($manifest[$file])) {
23             return baseUrl($manifest[$file]);
24         }
25
26         if (file_exists(public_path($file))) {
27             return baseUrl($file);
28         }
29
30         throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
31     }
32 }
33
34 /**
35  * Check if the current user has a permission.
36  * If an ownable element is passed in the jointPermissions are checked against
37  * that particular item.
38  * @param $permission
39  * @param Ownable $ownable
40  * @return mixed
41  */
42 function userCan($permission, Ownable $ownable = null)
43 {
44     if ($ownable === null) {
45         return auth()->user() && auth()->user()->can($permission);
46     }
47
48     // Check permission on ownable item
49     $permissionService = app(\BookStack\Services\PermissionService::class);
50     return $permissionService->checkOwnableUserAccess($ownable, $permission);
51 }
52
53 /**
54  * Helper to access system settings.
55  * @param $key
56  * @param bool $default
57  * @return mixed
58  */
59 function setting($key, $default = false)
60 {
61     $settingService = app('BookStack\Services\SettingService');
62     return $settingService->get($key, $default);
63 }
64
65 /**
66  * Helper to create url's relative to the applications root path.
67  * @param $path
68  * @return string
69  */
70 function baseUrl($path)
71 {
72     $path = trim($path, '/');
73     return rtrim(config('app.url'), '/') . '/' . $path;
74 }
75
76 /**
77  * Generate a url with multiple parameters for sorting purposes.
78  * Works out the logic to set the correct sorting direction
79  * Discards empty parameters and allows overriding.
80  * @param $path
81  * @param array $data
82  * @param array $overrideData
83  * @return string
84  */
85 function sortUrl($path, $data, $overrideData = [])
86 {
87     $queryStringSections = [];
88     $queryData = array_merge($data, $overrideData);
89     
90     // Change sorting direction is already sorted on current attribute
91     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
92         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
93     } else {
94         $queryData['order'] = 'asc';
95     }
96     
97     foreach ($queryData as $name => $value) {
98         $trimmedVal = trim($value);
99         if ($trimmedVal === '') continue;
100         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
101     }
102
103     if (count($queryStringSections) === 0) return $path;
104
105     return $path . '?' . implode('&', $queryStringSections);
106 }