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