bookmark.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. <?php
  2. // PLUGIN INFORMATION
  3. $GLOBALS['plugins'][]['Bookmark'] = array( // Plugin Name
  4. 'name' => 'Bookmark', // Plugin Name
  5. 'author' => 'leet1994', // Who wrote the plugin
  6. 'category' => 'Utilities', // One to Two Word Description
  7. 'link' => '', // Link to plugin info
  8. 'license' => 'personal,business', // License Type use , for multiple
  9. 'idPrefix' => 'BOOKMARK', // html element id prefix
  10. 'configPrefix' => 'BOOKMARK', // config file prefix for array items without the hypen
  11. 'dbPrefix' => 'BOOKMARK', // db prefix
  12. 'version' => '0.1.0', // SemVer of plugin
  13. 'image' => 'plugins/images/bookmark.png', // 1:1 non transparent image for plugin
  14. 'settings' => true, // does plugin need a settings page? true or false
  15. 'homepage' => false // Is plugin for use on homepage? true or false
  16. );
  17. // Logo image under Public Domain from https://openclipart.org/detail/182527/open-book
  18. class Bookmark extends Organizr
  19. {
  20. public function writeLog($type = 'error', $message = null, $username = null)
  21. {
  22. parent::writeLog($type, "Plugin 'Bookmark': " . $message, $username);
  23. }
  24. public function _checkRequest($request)
  25. {
  26. $result = false;
  27. if ($this->config['BOOKMARK-enabled'] && $this->hasDB()) {
  28. /*
  29. $response = [
  30. array(
  31. 'function' => 'query',
  32. 'query' => 'DROP TABLE IF EXISTS `BOOKMARK-categories`'
  33. ),
  34. array(
  35. 'function' => 'query',
  36. 'query' => 'DROP TABLE IF EXISTS `BOOKMARK-tabs`'
  37. )
  38. ];
  39. $this->processQueries($response);
  40. //*/
  41. if (!$this->_checkDatabaseTablesExist()) {
  42. $this->_createDatabaseTables();
  43. }
  44. $result = true;
  45. }
  46. return $result;
  47. }
  48. protected function _checkDatabaseTablesExist()
  49. {
  50. $response = [
  51. array(
  52. 'function' => 'fetchSingle',
  53. 'query' => array(
  54. "SELECT `name` FROM `sqlite_master` WHERE `type` = 'table' AND `name` = 'BOOKMARK-categories'"
  55. ),
  56. 'key' => 'BOOKMARK-categories'
  57. ),
  58. array(
  59. 'function' => 'fetchSingle',
  60. 'query' => array(
  61. "SELECT `name` FROM `sqlite_master` WHERE `type` = 'table' AND `name` = 'BOOKMARK-tabs'"
  62. ),
  63. 'key' => 'BOOKMARK-tabs'
  64. ),
  65. ];
  66. $data = $this->processQueries($response);
  67. return ($data["BOOKMARK-categories"] != false && $data["BOOKMARK-tabs"] != false);
  68. }
  69. protected function _createDatabaseTables()
  70. {
  71. $response = [
  72. array(
  73. 'function' => 'query',
  74. 'query' => 'CREATE TABLE `BOOKMARK-categories` (
  75. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  76. `order` INTEGER,
  77. `category` TEXT UNIQUE,
  78. `category_id` INTEGER,
  79. `default` INTEGER
  80. );'
  81. ),
  82. array(
  83. 'function' => 'query',
  84. 'query' => 'CREATE TABLE `BOOKMARK-tabs` (
  85. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  86. `order` INTEGER,
  87. `category_id` INTEGER,
  88. `name` TEXT,
  89. `url` TEXT,
  90. `enabled` INTEGER,
  91. `group_id` INTEGER,
  92. `image` TEXT,
  93. `background_color` TEXT,
  94. `text_color` TEXT
  95. );'
  96. )
  97. ];
  98. $this->processQueries($response);
  99. }
  100. public function _getSettings()
  101. {
  102. return array(
  103. 'custom' => '
  104. <div class="row">
  105. <div class="col-lg-12">
  106. <div class="panel panel-info">
  107. <div class="panel-heading">
  108. <span lang="en">Notice</span>
  109. </div>
  110. <div class="panel-wrapper collapse in" aria-expanded="true">
  111. <div class="panel-body">
  112. <ul class="list-icons">
  113. <li><i class="fa fa-chevron-right text-info"></i> Add tab that points to <i>api/v2/plugins/bookmark/page</i> and set it\'s type to <i>Organizr</i>.</li>
  114. <li><i class="fa fa-chevron-right text-info"></i> Create Bookmark categories in the new area in <i>Tab Editor</i>.</li>
  115. <li><i class="fa fa-chevron-right text-info"></i> Create Bookmark tabs in the new area in <i>Tab Editor</i>.</li>
  116. <li><i class="fa fa-chevron-right text-info"></i> Open your custom Bookmark page via menu.</li>
  117. </ul>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. '
  124. );
  125. }
  126. public function _getPage()
  127. {
  128. $script = '
  129. <script>
  130. var styles = `
  131. #BOOKMARK-wrapper {
  132. display: flex;
  133. flex-direction: column;
  134. justify-content: flex-start;
  135. }
  136. .BOOKMARK-category {
  137. text-align: center;
  138. margin-bottom: 40px;
  139. }
  140. .BOOKMARK-category-title {
  141. font-weight: 500;
  142. color: #ddd;
  143. font-size: large;
  144. }
  145. .BOOKMARK-category-content {
  146. width: 80%;
  147. margin: 0 auto;
  148. display: flex;
  149. flex-flow: row wrap;
  150. justify-content: center;
  151. }
  152. .BOOKMARK-tab {
  153. display: inline-flex;
  154. justify-content: space-between;
  155. align-items: center;
  156. margin: 10px 10px 0 10px;
  157. height: 50px;
  158. width: 200px;
  159. overflow: hidden;
  160. border: 1px solid;
  161. border-radius: 5px;
  162. }
  163. .BOOKMARK-tab img {
  164. width: 100px;
  165. max-width: 50px;
  166. height: 100%;
  167. flex-grow: 33;
  168. object-fit: contain;
  169. padding: 5px;
  170. }
  171. .BOOKMARK-tab span {
  172. flex-grow: 67;
  173. padding: 0 5px;
  174. color: white;
  175. text-align: left;
  176. font-weight: 500;
  177. }
  178. `;
  179. var styleSheet = document.createElement("style");
  180. styleSheet.type = "text/css";
  181. styleSheet.innerText = styles;
  182. document.head.appendChild(styleSheet);
  183. </script>';
  184. $bookmarks = '<div id="BOOKMARK-wrapper">';
  185. foreach ($this->_getAllCategories() as $category) {
  186. $tabs = $this->_getRelevantTabsForCategory($category['category_id']);
  187. if (count($tabs) == 0) continue;
  188. $bookmarks .= '<div class="BOOKMARK-category">
  189. <div class="BOOKMARK-category-title">
  190. ' . $category['category'] . '
  191. </div>
  192. <div class="BOOKMARK-category-content">';
  193. foreach ($tabs as $tab) {
  194. $bookmarks .= '<a href="'.$tab['url'].'" target="_SELF">
  195. <div class="BOOKMARK-tab"
  196. style="border-color: '.$this->adjustBrightness($tab['background_color'], 0.3).'; background: linear-gradient(90deg, '.$this->adjustBrightness($tab['background_color'], -0.3).' 0%, '.$tab['background_color'].' 70%, '.$this->adjustBrightness($tab['background_color'], 0.1).' 100%);">
  197. '.$this->_iconPrefix($tab['image']).'
  198. <span style="color: '.$tab['text_color'].';">'.$tab['name'].'</span>
  199. </div>
  200. </a>';
  201. }
  202. $bookmarks .= '</div></div>';
  203. }
  204. $bookmarks .= '</div>';
  205. return $script . $bookmarks;
  206. }
  207. protected function _iconPrefix($source)
  208. {
  209. $tabIcon = explode("::", $source);
  210. $icons = array(
  211. "materialize" => "mdi mdi-",
  212. "fontawesome" => "fa fa-",
  213. "themify" => "ti-",
  214. "simpleline" => "icon-",
  215. "weathericon" => "wi wi-",
  216. "alphanumeric" => "fa-fw",
  217. );
  218. if (is_array($tabIcon) && count($tabIcon) == 2) {
  219. if ($tabIcon[0] !== 'url' && $tabIcon[0] !== 'alphanumeric') {
  220. return '<i class="' . $icons[$tabIcon[0]] . $tabIcon[1] . ' fa-fw"></i>';
  221. } else if ($tabIcon[0] == 'alphanumeric') {
  222. return '<i class="fa-fw">' . $tabIcon[1] . '</i>';
  223. } else {
  224. return '<img class="fa-fw" src="' . $tabIcon[1] . '" alt="tabIcon" />';
  225. }
  226. } else {
  227. return '<img class="fa-fw" src="' . $source . '" alt="tabIcon" />';
  228. }
  229. }
  230. protected function _getAllCategories()
  231. {
  232. $response = [
  233. array(
  234. 'function' => 'fetchAll',
  235. 'query' => 'SELECT * FROM `BOOKMARK-categories` ORDER BY `order` ASC'
  236. )
  237. ];
  238. return $this->processQueries($response);
  239. }
  240. protected function _getRelevantTabsForCategory($category_id)
  241. {
  242. $response = [
  243. array(
  244. 'function' => 'fetchAll',
  245. 'query' => array(
  246. "SELECT * FROM `BOOKMARK-tabs` WHERE `enabled`='1' AND `category_id`=? AND `group_id`>=? ORDER BY `order` ASC",
  247. $category_id,
  248. $this->getUserLevel()
  249. )
  250. )
  251. ];
  252. return $this->processQueries($response);
  253. }
  254. public function _getTabs()
  255. {
  256. $response = [
  257. array(
  258. 'function' => 'fetchAll',
  259. 'query' => 'SELECT * FROM `BOOKMARK-tabs` ORDER BY `order` ASC',
  260. 'key' => 'tabs'
  261. ),
  262. array(
  263. 'function' => 'fetchAll',
  264. 'query' => 'SELECT * FROM `BOOKMARK-categories` ORDER BY `order` ASC',
  265. 'key' => 'categories'
  266. ),
  267. array(
  268. 'function' => 'fetchAll',
  269. 'query' => 'SELECT * FROM `groups` ORDER BY `group_id` ASC',
  270. 'key' => 'groups'
  271. )
  272. ];
  273. return $this->processQueries($response);
  274. }
  275. // Tabs
  276. public function _getSettingsTabEditorBookmarkTabsPage()
  277. {
  278. $iconSelectors = '
  279. $(".bookmarkTabIconIconList").select2({
  280. ajax: {
  281. url: \'api/v2/icon\',
  282. data: function (params) {
  283. var query = {
  284. search: params.term,
  285. page: params.page || 1
  286. }
  287. return query;
  288. },
  289. processResults: function (data, params) {
  290. params.page = params.page || 1;
  291. return {
  292. results: data.response.data.results,
  293. pagination: {
  294. more: (params.page * 20) < data.response.data.total
  295. }
  296. };
  297. },
  298. //cache: true
  299. },
  300. placeholder: \'Search for an icon\',
  301. templateResult: formatIcon,
  302. templateSelection: formatIcon
  303. });
  304. $(".bookmarkTabIconImageList").select2({
  305. ajax: {
  306. url: \'api/v2/image/select\',
  307. data: function (params) {
  308. var query = {
  309. search: params.term,
  310. page: params.page || 1
  311. }
  312. return query;
  313. },
  314. processResults: function (data, params) {
  315. params.page = params.page || 1;
  316. return {
  317. results: data.response.data.results,
  318. pagination: {
  319. more: (params.page * 20) < data.response.data.total
  320. }
  321. };
  322. },
  323. //cache: true
  324. },
  325. placeholder: \'Search for an image\',
  326. templateResult: formatImage,
  327. templateSelection: formatImage
  328. });
  329. ';
  330. return '
  331. <script>
  332. buildBookmarkTabEditor();
  333. !function(a){function f(a,b){if(!(a.originalEvent.touches.length>1)){a.preventDefault();var c=a.originalEvent.changedTouches[0],d=document.createEvent("MouseEvents");d.initMouseEvent(b,!0,!0,window,1,c.screenX,c.screenY,c.clientX,c.clientY,!1,!1,!1,!1,0,null),a.target.dispatchEvent(d)}}if(a.support.touch="ontouchend"in document,a.support.touch){var e,b=a.ui.mouse.prototype,c=b._mouseInit,d=b._mouseDestroy;b._touchStart=function(a){var b=this;!e&&b._mouseCapture(a.originalEvent.changedTouches[0])&&(e=!0,b._touchMoved=!1,f(a,"mouseover"),f(a,"mousemove"),f(a,"mousedown"))},b._touchMove=function(a){e&&(this._touchMoved=!0,f(a,"mousemove"))},b._touchEnd=function(a){e&&(f(a,"mouseup"),f(a,"mouseout"),this._touchMoved||f(a,"click"),e=!1)},b._mouseInit=function(){var b=this;b.element.bind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),c.call(b)},b._mouseDestroy=function(){var b=this;b.element.unbind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),d.call(b)}}}(jQuery);
  334. $( \'#bookmarkTabEditorTable\' ).sortable({
  335. stop: function () {
  336. $(\'input.order\').each(function(idx) {
  337. $(this).val(idx + 1);
  338. });
  339. var newTabs = $( "#submit-bookmark-tabs-form" ).serializeToJSON();
  340. newBookmarkTabsGlobal = newTabs;
  341. $(\'.saveBookmarkTabOrderButton\').removeClass(\'hidden\');
  342. //submitTabOrder(newTabs);
  343. }
  344. });
  345. $( \'#bookmarkTabEditorTable\' ).disableSelection();
  346. ' . $iconSelectors . '
  347. </script>
  348. <div class="panel bg-org panel-info">
  349. <div class="panel-heading">
  350. <span lang="en">Bookmark Tab Editor</span>
  351. <button type="button" class="btn btn-info btn-circle pull-right popup-with-form m-r-5" href="#new-bookmark-tab-form" data-effect="mfp-3d-unfold"><i class="fa fa-plus"></i> </button>
  352. <button onclick="submitBookmarkTabOrder(newBookmarkTabsGlobal)" class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right animated loop-animation rubberBand m-r-20 saveBookmarkTabOrderButton hidden" type="button"><span class="btn-label"><i class="fa fa-save"></i></span><span lang="en">Save Tab Order</span></button>
  353. </div>
  354. <div class="table-responsive">
  355. <form id="submit-bookmark-tabs-form" onsubmit="return false;">
  356. <table class="table table-hover manage-u-table">
  357. <thead>
  358. <tr>
  359. <th width="70" class="text-center">#</th>
  360. <th lang="en">NAME</th>
  361. <th lang="en">CATEGORY</th>
  362. <th lang="en">GROUP</th>
  363. <th lang="en" style="text-align:center">ACTIVE</th>
  364. <th lang="en" style="text-align:center">EDIT</th>
  365. <th lang="en" style="text-align:center">DELETE</th>
  366. </tr>
  367. </thead>
  368. <tbody id="bookmarkTabEditorTable">
  369. <td class="text-center" colspan="12"><i class="fa fa-spin fa-spinner"></i></td>
  370. </tbody>
  371. </table>
  372. </form>
  373. </div>
  374. </div>
  375. <form id="new-bookmark-tab-form" class="mfp-hide white-popup-block mfp-with-anim">
  376. <h1 lang="en">Add New Tab</h1>
  377. <fieldset style="border:0;">
  378. <div class="form-group">
  379. <label class="control-label" for="new-bookmark-tab-form-inputNameNew" lang="en">Tab Name</label>
  380. <input type="text" class="form-control" id="new-bookmark-tab-form-inputNameNew" name="name" required="" autofocus>
  381. </div>
  382. <div class="form-group">
  383. <label class="control-label" for="new-bookmark-tab-form-inputURLNew" lang="en">Tab URL</label>
  384. <input type="text" class="form-control" id="new-bookmark-tab-form-inputURLNew" name="url" required="">
  385. </div>
  386. <div class="row">
  387. <div class="form-group col-lg-4">
  388. <label class="control-label" for="new-bookmark-tab-form-chooseImage" lang="en">Choose Image</label>
  389. <select class="form-control bookmarkTabIconImageList" id="new-bookmark-tab-form-chooseImage" name="chooseImage"><option lang="en">Select or type Image</option></select>
  390. </div>
  391. <div class="form-group col-lg-4">
  392. <label class="control-label" for="new-bookmark-tab-form-chooseIcon" lang="en">Choose Icon</label>
  393. <select class="form-control bookmarkTabIconIconList" id="new-bookmark-tab-form-chooseIcon" name="chooseIcon"><option lang="en">Select or type Icon</option></select>
  394. </div>
  395. <div class="form-group col-lg-4">
  396. <label class="control-label" for="new-bookmark-tab-form-chooseBlackberry" lang="en">Choose Blackberry Theme Icon</label>
  397. <button id="new-bookmark-tab-form-chooseBlackberry" class="btn btn-xs btn-primary waves-effect waves-light form-control" onclick="showBlackberryThemes(\'new-bookmark-tab-form-inputImageNew\');" type="button">
  398. <i class="fa fa-search"></i>&nbsp; <span lang="en">Choose</span>
  399. </button>
  400. </div>
  401. </div>
  402. <div class="form-group">
  403. <label class="control-label" for="new-bookmark-tab-form-inputImageNew" lang="en">Tab Image</label>
  404. <input type="text" class="form-control" id="new-bookmark-tab-form-inputImageNew" name="image" required="">
  405. </div>
  406. <div class="row">
  407. <div class="form-group col-lg-4">
  408. <label class="control-label" for="new-bookmark-tab-form-inputBackgroundColorNew" lang="en">Background Color</label>
  409. <input type="text" class="form-control" id="new-bookmark-tab-form-inputBackgroundColorNew" name="background_color" required="">
  410. </div>
  411. <div class="form-group col-lg-4">
  412. <label class="control-label" for="new-bookmark-tab-form-inputTextColorNew" lang="en">Text Color</label>
  413. <input type="text" class="form-control" id="new-bookmark-tab-form-inputTextColorNew" name="text_color" required="">
  414. </div>
  415. </div>
  416. </fieldset>
  417. <button class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right row b-none addNewBookmarkTab" type="button"><span class="btn-label"><i class="fa fa-plus"></i></span><span lang="en">Add Tab</span></button>
  418. <div class="clearfix"></div>
  419. </form>
  420. <form id="edit-bookmark-tab-form" class="mfp-hide white-popup-block mfp-with-anim">
  421. <input type="hidden" name="id" value="x">
  422. <span class="hidden" id="originalBookmarkTabName"></span>
  423. <h1 lang="en">Edit Tab</h1>
  424. <fieldset style="border:0;">
  425. <div class="form-group">
  426. <label class="control-label" for="edit-bookmark-tab-form-inputName" lang="en">Tab Name</label>
  427. <input type="text" class="form-control" id="edit-bookmark-tab-form-inputName" name="name" required="" autofocus>
  428. </div>
  429. <div class="form-group">
  430. <label class="control-label" for="edit-bookmark-tab-form-inputURL" lang="en">Tab URL</label>
  431. <input type="text" class="form-control" id="edit-bookmark-tab-form-inputURL" name="url" required="">
  432. </div>
  433. <div class="row">
  434. <div class="form-group col-lg-4">
  435. <label class="control-label" for="edit-bookmark-tab-form-chooseImage" lang="en">Choose Image</label>
  436. <select class="form-control bookmarkTabIconImageList" id="edit-bookmark-tab-form-chooseImage" name="chooseImage"><option lang="en">Select or type Image</option></select>
  437. </div>
  438. <div class="form-group col-lg-4">
  439. <label class="control-label" for="edit-bookmark-tab-form-chooseIcon" lang="en">Choose Icon</label>
  440. <select class="form-control bookmarkTabIconIconList" id="edit-bookmark-tab-form-chooseIcon" name="chooseIcon"><option lang="en">Select or type Icon</option></select>
  441. </div>
  442. <div class="form-group col-lg-4">
  443. <label class="control-label" for="edit-bookmark-tab-form-chooseBlackberry" lang="en">Choose Blackberry Theme Icon</label>
  444. <button id="edit-bookmark-tab-form-chooseBlackberry" class="btn btn-xs btn-primary waves-effect waves-light form-control" onclick="showBlackberryThemes(\'edit-bookmark-tab-form-inputImage\');" type="button">
  445. <i class="fa fa-search"></i>&nbsp; <span lang="en">Choose</span>
  446. </button>
  447. </div>
  448. </div>
  449. <div class="form-group">
  450. <label class="control-label" for="edit-bookmark-tab-form-inputImage" lang="en">Tab Image</label>
  451. <input type="text" class="form-control" id="edit-bookmark-tab-form-inputImage" name="image" required="">
  452. </div>
  453. <div class="row">
  454. <div class="form-group col-lg-4">
  455. <label class="control-label" for="new-bookmark-tab-form-inputBackgroundColor" lang="en">Background Color</label>
  456. <input type="text" class="form-control" id="new-bookmark-tab-form-inputBackgroundColor" name="background_color" required="">
  457. </div>
  458. <div class="form-group col-lg-4">
  459. <label class="control-label" for="new-bookmark-tab-form-inputTextColor" lang="en">Text Color</label>
  460. <input type="text" class="form-control" id="new-bookmark-tab-form-inputTextColor" name="text_color" required="">
  461. </div>
  462. </div>
  463. </fieldset>
  464. <button class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right row b-none editBookmarkTab" type="button"><span class="btn-label"><i class="fa fa-check"></i></span><span lang="en">Edit Tab</span></button>
  465. <div class="clearfix"></div>
  466. </form>
  467. ';
  468. }
  469. public function _isBookmarkTabNameTaken($name, $id = null)
  470. {
  471. if ($id) {
  472. $response = [
  473. array(
  474. 'function' => 'fetchAll',
  475. 'query' => array(
  476. 'SELECT * FROM `BOOKMARK-tabs` WHERE `name` LIKE ? AND `id` != ?',
  477. $name,
  478. $id
  479. )
  480. ),
  481. ];
  482. } else {
  483. $response = [
  484. array(
  485. 'function' => 'fetchAll',
  486. 'query' => array(
  487. 'SELECT * FROM `BOOKMARK-tabs` WHERE `name` LIKE ?',
  488. $name
  489. )
  490. ),
  491. ];
  492. }
  493. return $this->processQueries($response);
  494. }
  495. public function _getNextBookmarkTabOrder()
  496. {
  497. $response = [
  498. array(
  499. 'function' => 'fetchSingle',
  500. 'query' => array(
  501. 'SELECT `order` from `BOOKMARK-tabs` ORDER BY `order` DESC'
  502. )
  503. ),
  504. ];
  505. return $this->processQueries($response);
  506. }
  507. public function _getBookmarkTabById($id)
  508. {
  509. $response = [
  510. array(
  511. 'function' => 'fetch',
  512. 'query' => array(
  513. 'SELECT * FROM `BOOKMARK-tabs` WHERE `id` = ?',
  514. $id
  515. )
  516. ),
  517. ];
  518. return $this->processQueries($response);
  519. }
  520. public function _getTabByIdCheckUser($id)
  521. {
  522. $tabInfo = $this->_getBookmarkTabById($id);
  523. if ($tabInfo) {
  524. if ($this->qualifyRequest($tabInfo['group_id'], true)) {
  525. return $tabInfo;
  526. }
  527. } else {
  528. $this->setAPIResponse('error', 'id not found', 404);
  529. return false;
  530. }
  531. }
  532. public function _deleteTab($id)
  533. {
  534. $response = [
  535. array(
  536. 'function' => 'query',
  537. 'query' => array(
  538. 'DELETE FROM `BOOKMARK-tabs` WHERE id = ?',
  539. $id
  540. )
  541. ),
  542. ];
  543. $tabInfo = $this->_getBookmarkTabById($id);
  544. if ($tabInfo) {
  545. $this->writeLog('success', 'Tab Delete Function - Deleted Tab [' . $tabInfo['name'] . ']', $this->user['username']);
  546. $this->setAPIResponse('success', 'Tab deleted', 204);
  547. return $this->processQueries($response);
  548. } else {
  549. $this->setAPIResponse('error', 'id not found', 404);
  550. return false;
  551. }
  552. }
  553. public function _addTab($array)
  554. {
  555. if (!$array) {
  556. $this->setAPIResponse('error', 'no data was sent', 422);
  557. return null;
  558. }
  559. $array = $this->checkKeys($this->getTableColumnsFormatted('BOOKMARK-tabs'), $array);
  560. $array['group_id'] = ($array['group_id']) ?? $this->getDefaultGroupId();
  561. $array['category_id'] = ($array['category_id']) ?? $this->_getDefaultBookmarkCategoryId();
  562. $array['enabled'] = ($array['enabled']) ?? 0;
  563. $array['order'] = ($array['order']) ?? $this->_getNextBookmarkTabOrder() + 1;
  564. if (array_key_exists('name', $array)) {
  565. if ($this->_isBookmarkTabNameTaken($array['name'])) {
  566. $this->setAPIResponse('error', 'Tab name: ' . $array['name'] . ' is already taken', 409);
  567. return false;
  568. }
  569. } else {
  570. $this->setAPIResponse('error', 'Tab name was not supplied', 422);
  571. return false;
  572. }
  573. if (!array_key_exists('url', $array)) {
  574. $this->setAPIResponse('error', 'Tab url was not supplied', 422);
  575. return false;
  576. }
  577. if (!array_key_exists('image', $array)) {
  578. $this->setAPIResponse('error', 'Tab image was not supplied', 422);
  579. return false;
  580. }
  581. if (array_key_exists('background_color', $array)) {
  582. if(!$this->_checkColorHexCode($array['background_color'])){
  583. $this->setAPIResponse('error', 'Tab background color is invalid', 422);
  584. return false;
  585. }
  586. }
  587. else{
  588. $this->setAPIResponse('error', 'Tab background color was not supplied', 422);
  589. return false;
  590. }
  591. if (array_key_exists('text_color', $array)) {
  592. if(!$this->_checkColorHexCode($array['text_color'])){
  593. $this->setAPIResponse('error', 'Tab text color is invalid', 422);
  594. return false;
  595. }
  596. }
  597. else{
  598. $this->setAPIResponse('error', 'Tab text color was not supplied', 422);
  599. return false;
  600. }
  601. $response = [
  602. array(
  603. 'function' => 'query',
  604. 'query' => array(
  605. 'INSERT INTO [BOOKMARK-tabs]',
  606. $array
  607. )
  608. ),
  609. ];
  610. $this->setAPIResponse(null, 'Tab added');
  611. $this->writeLog('success', 'Tab Editor Function - Added Tab for [' . $array['name'] . ']', $this->user['username']);
  612. return $this->processQueries($response);
  613. }
  614. public function _updateTab($id, $array)
  615. {
  616. if (!$id || $id == '') {
  617. $this->setAPIResponse('error', 'id was not set', 422);
  618. return null;
  619. }
  620. if (!$array) {
  621. $this->setAPIResponse('error', 'no data was sent', 422);
  622. return null;
  623. }
  624. $tabInfo = $this->_getBookmarkTabById($id);
  625. if ($tabInfo) {
  626. $array = $this->checkKeys($tabInfo, $array);
  627. } else {
  628. $this->setAPIResponse('error', 'No tab info found', 404);
  629. return false;
  630. }
  631. if (array_key_exists('name', $array)) {
  632. if ($this->_isBookmarkTabNameTaken($array['name'], $id)) {
  633. $this->setAPIResponse('error', 'Tab name: ' . $array['name'] . ' is already taken', 409);
  634. return false;
  635. }
  636. }
  637. if (array_key_exists('background_color', $array)) {
  638. if(!$this->_checkColorHexCode($array['background_color'])){
  639. $this->setAPIResponse('error', 'Tab background color is invalid', 422);
  640. return false;
  641. }
  642. }
  643. if (array_key_exists('text_color', $array)) {
  644. if(!$this->_checkColorHexCode($array['text_color'])){
  645. $this->setAPIResponse('error', 'Tab text color is invalid', 422);
  646. return false;
  647. }
  648. }
  649. $response = [
  650. array(
  651. 'function' => 'query',
  652. 'query' => array(
  653. 'UPDATE `BOOKMARK-tabs` SET',
  654. $array,
  655. 'WHERE id = ?',
  656. $id
  657. )
  658. ),
  659. ];
  660. $this->setAPIResponse(null, 'Tab info updated');
  661. $this->writeLog('success', 'Tab Editor Function - Edited Tab Info for [' . $tabInfo['name'] . ']', $this->user['username']);
  662. return $this->processQueries($response);
  663. }
  664. public function _updateTabOrder($array)
  665. {
  666. if (count($array) >= 1) {
  667. foreach ($array as $tab) {
  668. if (count($tab) !== 2) {
  669. $this->setAPIResponse('error', 'data is malformed', 422);
  670. break;
  671. }
  672. $id = $tab['id'] ?? null;
  673. $order = $tab['order'] ?? null;
  674. if ($id && $order) {
  675. $response = [
  676. array(
  677. 'function' => 'query',
  678. 'query' => array(
  679. 'UPDATE `BOOKMARK-tabs` set `order` = ? WHERE `id` = ?',
  680. $order,
  681. $id
  682. )
  683. ),
  684. ];
  685. $this->processQueries($response);
  686. $this->setAPIResponse(null, 'Tab Order updated');
  687. } else {
  688. $this->setAPIResponse('error', 'data is malformed', 422);
  689. }
  690. }
  691. } else {
  692. $this->setAPIResponse('error', 'data is empty or not in array', 422);
  693. return false;
  694. }
  695. }
  696. // Categories
  697. public function _getSettingsTabEditorBookmarkCategoriesPage()
  698. {
  699. return '
  700. <script>
  701. buildBookmarkCategoryEditor();
  702. $( \'#bookmarkCategoryEditorTable\' ).sortable({
  703. stop: function () {
  704. var inputs = $(\'input.order\');
  705. var nbElems = inputs.length;
  706. inputs.each(function(idx) {
  707. $(this).val(idx + 1);
  708. });
  709. submitBookmarkCategoryOrder();
  710. }
  711. });
  712. </script>
  713. <div class="panel bg-org panel-info">
  714. <div class="panel-heading">
  715. <span lang="en">Bookmark Category Editor</span>
  716. <button type="button" class="btn btn-success btn-circle pull-right popup-with-form m-r-5" href="#new-bookmark-category-form" data-effect="mfp-3d-unfold"><i class="fa fa-plus"></i> </button>
  717. </div>
  718. <div class="table-responsive">
  719. <form id="submit-bookmark-categories-form" onsubmit="return false;">
  720. <table class="table table-hover manage-u-table">
  721. <thead>
  722. <tr>
  723. <th lang="en">NAME</th>
  724. <th lang="en" style="text-align:center">TABS</th>
  725. <th lang="en" style="text-align:center">DEFAULT</th>
  726. <th lang="en" style="text-align:center">EDIT</th>
  727. <th lang="en" style="text-align:center">DELETE</th>
  728. </tr>
  729. </thead>
  730. <tbody id="bookmarkCategoryEditorTable"><td class="text-center" colspan="6"><i class="fa fa-spin fa-spinner"></i></td></tbody>
  731. </table>
  732. </form>
  733. </div>
  734. </div>
  735. <form id="new-bookmark-category-form" class="mfp-hide white-popup-block mfp-with-anim">
  736. <h1 lang="en">Add New Bookmark Category</h1>
  737. <fieldset style="border:0;">
  738. <div class="form-group">
  739. <label class="control-label" for="new-bookmark-category-form-inputNameNew" lang="en">Category Name</label>
  740. <input type="text" class="form-control" id="new-bookmark-category-form-inputNameNew" name="category" required="" autofocus>
  741. </div>
  742. </fieldset>
  743. <button class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right row b-none addNewBookmarkCategory" type="button"><span class="btn-label"><i class="fa fa-plus"></i></span><span lang="en">Add Category</span></button>
  744. <div class="clearfix"></div>
  745. </form>
  746. <form id="edit-bookmark-category-form" class="mfp-hide white-popup-block mfp-with-anim">
  747. <input type="hidden" name="id" value="">
  748. <h1 lang="en">Edit Category</h1>
  749. <fieldset style="border:0;">
  750. <div class="form-group">
  751. <label class="control-label" for="edit-bookmark-category-form-inputName" lang="en">Category Name</label>
  752. <input type="text" class="form-control" id="edit-bookmark-category-form-inputName" name="category" required="" autofocus>
  753. </div>
  754. </fieldset>
  755. <button class="btn btn-sm btn-info btn-rounded waves-effect waves-light pull-right row b-none editBookmarkCategory" type="button"><span class="btn-label"><i class="fa fa-plus"></i></span><span lang="en">Edit Category</span></button>
  756. <div class="clearfix"></div>
  757. </form>
  758. ';
  759. }
  760. public function _getDefaultBookmarkCategoryId()
  761. {
  762. $response = [
  763. array(
  764. 'function' => 'fetchSingle',
  765. 'query' => array(
  766. 'SELECT `category_id` FROM `BOOKMARK-categories` WHERE `default` = 1'
  767. )
  768. ),
  769. ];
  770. return $this->processQueries($response);
  771. }
  772. public function _getNextBookmarkCategoryOrder()
  773. {
  774. $response = [
  775. array(
  776. 'function' => 'fetchSingle',
  777. 'query' => array(
  778. 'SELECT `order` from `BOOKMARK-categories` ORDER BY `order` DESC'
  779. )
  780. ),
  781. ];
  782. return $this->processQueries($response);
  783. }
  784. public function _getNextBookmarkCategoryId()
  785. {
  786. $response = [
  787. array(
  788. 'function' => 'fetchSingle',
  789. 'query' => array(
  790. 'SELECT `category_id` from `BOOKMARK-categories` ORDER BY `category_id` DESC'
  791. )
  792. ),
  793. ];
  794. return $this->processQueries($response);
  795. }
  796. public function _isBookmarkCategoryNameTaken($name, $id = null)
  797. {
  798. if ($id) {
  799. $response = [
  800. array(
  801. 'function' => 'fetchAll',
  802. 'query' => array(
  803. 'SELECT * FROM `BOOKMARK-categories` WHERE `category` LIKE ? AND `id` != ?',
  804. $name,
  805. $id
  806. )
  807. ),
  808. ];
  809. } else {
  810. $response = [
  811. array(
  812. 'function' => 'fetchAll',
  813. 'query' => array(
  814. 'SELECT * FROM `BOOKMARK-categories` WHERE `category` LIKE ?',
  815. $name
  816. )
  817. ),
  818. ];
  819. }
  820. return $this->processQueries($response);
  821. }
  822. public function _getBookmarkCategoryById($id)
  823. {
  824. $response = [
  825. array(
  826. 'function' => 'fetch',
  827. 'query' => array(
  828. 'SELECT * FROM `BOOKMARK-categories` WHERE `id` = ?',
  829. $id
  830. )
  831. ),
  832. ];
  833. return $this->processQueries($response);
  834. }
  835. public function _clearBookmarkCategoryDefault()
  836. {
  837. $response = [
  838. array(
  839. 'function' => 'query',
  840. 'query' => array(
  841. 'UPDATE `BOOKMARK-categories` SET `default` = 0'
  842. )
  843. ),
  844. ];
  845. return $this->processQueries($response);
  846. }
  847. public function _addCategory($array)
  848. {
  849. if (!$array) {
  850. $this->setAPIResponse('error', 'no data was sent', 422);
  851. return null;
  852. }
  853. $array = $this->checkKeys($this->getTableColumnsFormatted('BOOKMARK-categories'), $array);
  854. $array['default'] = ($array['default']) ?? 0;
  855. $array['order'] = ($array['order']) ?? $this->_getNextBookmarkCategoryOrder() + 1;
  856. $array['category_id'] = ($array['category_id']) ?? $this->_getNextBookmarkCategoryId() + 1;
  857. if (array_key_exists('category', $array)) {
  858. if ($this->_isBookmarkCategoryNameTaken($array['category'])) {
  859. $this->setAPIResponse('error', 'Category name: ' . $array['category'] . ' is already taken', 409);
  860. return false;
  861. }
  862. } else {
  863. $this->setAPIResponse('error', 'Category name was not supplied', 422);
  864. return false;
  865. }
  866. $response = [
  867. array(
  868. 'function' => 'query',
  869. 'query' => array(
  870. 'INSERT INTO [BOOKMARK-categories]',
  871. $array
  872. )
  873. ),
  874. ];
  875. $this->setAPIResponse(null, 'Category added');
  876. $this->writeLog('success', 'Category Editor Function - Added Category for [' . $array['category'] . ']', $this->user['username']);
  877. $result = $this->processQueries($response);
  878. $this->_correctDefaultCategory();
  879. return $result;
  880. }
  881. public function _updateCategory($id, $array)
  882. {
  883. if (!$id || $id == '') {
  884. $this->setAPIResponse('error', 'id was not set', 422);
  885. return null;
  886. }
  887. if (!$array) {
  888. $this->setAPIResponse('error', 'no data was sent', 422);
  889. return null;
  890. }
  891. $categoryInfo = $this->_getBookmarkCategoryById($id);
  892. if ($categoryInfo) {
  893. $array = $this->checkKeys($categoryInfo, $array);
  894. } else {
  895. $this->setAPIResponse('error', 'No category info found', 404);
  896. return false;
  897. }
  898. if (array_key_exists('category', $array)) {
  899. if ($this->_isBookmarkCategoryNameTaken($array['category'], $id)) {
  900. $this->setAPIResponse('error', 'Category name: ' . $array['category'] . ' is already taken', 409);
  901. return false;
  902. }
  903. }
  904. if (array_key_exists('default', $array)) {
  905. if ($array['default']) {
  906. $this->_clearBookmarkCategoryDefault();
  907. }
  908. }
  909. $response = [
  910. array(
  911. 'function' => 'query',
  912. 'query' => array(
  913. 'UPDATE `BOOKMARK-categories` SET',
  914. $array,
  915. 'WHERE id = ?',
  916. $id
  917. )
  918. ),
  919. ];
  920. $this->setAPIResponse(null, 'Category info updated');
  921. $this->writeLog('success', 'Category Editor Function - Edited Category Info for [' . $categoryInfo['category'] . ']', $this->user['username']);
  922. $result = $this->processQueries($response);
  923. $this->_correctDefaultCategory();
  924. return $result;
  925. }
  926. public function _updateCategoryOrder($array)
  927. {
  928. if (count($array) >= 1) {
  929. foreach ($array as $category) {
  930. if (count($category) !== 2) {
  931. $this->setAPIResponse('error', 'data is malformed', 422);
  932. break;
  933. }
  934. $id = $category['id'] ?? null;
  935. $order = $category['order'] ?? null;
  936. if ($id && $order) {
  937. $response = [
  938. array(
  939. 'function' => 'query',
  940. 'query' => array(
  941. 'UPDATE `BOOKMARK-categories` set `order` = ? WHERE `id` = ?',
  942. $order,
  943. $id
  944. )
  945. ),
  946. ];
  947. $this->processQueries($response);
  948. $this->setAPIResponse(null, 'Category Order updated');
  949. } else {
  950. $this->setAPIResponse('error', 'data is malformed', 422);
  951. }
  952. }
  953. } else {
  954. $this->setAPIResponse('error', 'data is empty or not in array', 422);
  955. return false;
  956. }
  957. }
  958. public function _deleteCategory($id)
  959. {
  960. $response = [
  961. array(
  962. 'function' => 'query',
  963. 'query' => array(
  964. 'DELETE FROM `BOOKMARK-categories` WHERE id = ?',
  965. $id
  966. )
  967. ),
  968. ];
  969. $categoryInfo = $this->_getBookmarkCategoryById($id);
  970. if ($categoryInfo) {
  971. $this->writeLog('success', 'Category Delete Function - Deleted Category [' . $categoryInfo['category'] . ']', $this->user['username']);
  972. $this->setAPIResponse('success', 'Category deleted', 204);
  973. $result = $this->processQueries($response);
  974. $this->_correctDefaultCategory();
  975. return $result;
  976. } else {
  977. $this->setAPIResponse('error', 'id not found', 404);
  978. return false;
  979. }
  980. }
  981. protected function _correctDefaultCategory(){
  982. if($this->_getDefaultBookmarkCategoryId() == null){
  983. $response = [
  984. array(
  985. 'function' => 'query',
  986. 'query' => 'UPDATE `BOOKMARK-categories` SET `default` = 1 WHERE `category_id` = (SELECT `category_id` FROM `BOOKMARK-categories` ORDER BY `category_id` ASC LIMIT 0,1)'
  987. )
  988. ];
  989. return $this->processQueries($response);
  990. }
  991. }
  992. protected function _checkColorHexCode($hex){
  993. return preg_match('/^\#([0-9a-fA-F]{3}){1,2}$/', $hex);
  994. }
  995. /**
  996. * Increases or decreases the brightness of a color by a percentage of the current brightness.
  997. *
  998. * @param string $hexCode Supported formats: `#FFF`, `#FFFFFF`, `FFF`, `FFFFFF`
  999. * @param float $adjustPercent A number between -1 and 1. E.g. 0.3 = 30% lighter; -0.4 = 40% darker.
  1000. *
  1001. * @return string
  1002. *
  1003. * @author maliayas
  1004. * @link https://stackoverflow.com/questions/3512311/how-to-generate-lighter-darker-color-with-php
  1005. */
  1006. protected function adjustBrightness($hexCode, $adjustPercent) {
  1007. $hexCode = ltrim($hexCode, '#');
  1008. if (strlen($hexCode) == 3) {
  1009. $hexCode = $hexCode[0] . $hexCode[0] . $hexCode[1] . $hexCode[1] . $hexCode[2] . $hexCode[2];
  1010. }
  1011. $hexCode = array_map('hexdec', str_split($hexCode, 2));
  1012. foreach ($hexCode as & $color) {
  1013. $adjustableLimit = $adjustPercent < 0 ? $color : 255 - $color;
  1014. $adjustAmount = ceil($adjustableLimit * $adjustPercent);
  1015. $color = str_pad(dechex($color + $adjustAmount), 2, '0', STR_PAD_LEFT);
  1016. }
  1017. return '#' . implode($hexCode);
  1018. }
  1019. }