bookmark.php 33 KB

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