4
0

plugin.php 38 KB

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