API Docs for: 0.8.0
Show:

File: include/repository/plugin_repository.js

  1. /*
  2. Copyright (C) 2016 PencilBlue, LLC
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. 'use strict';
  18.  
  19. //dependencies
  20. var async = require('async');
  21. var util = require('../util.js');
  22.  
  23. module.exports = function PluginRepositoryModule(pb) {
  24.  
  25. /**
  26. * @private
  27. * @static
  28. * @readonly
  29. * @property PLUGIN_COLL
  30. * @type {String}
  31. */
  32. var PLUGIN_COLL = 'plugin';
  33.  
  34. /**
  35. * @private
  36. * @static
  37. * @readonly
  38. * @property GLOBAL_SITE
  39. * @type {String}
  40. */
  41. var GLOBAL_SITE = pb.SiteService.GLOBAL_SITE;
  42.  
  43. /**
  44. * @private
  45. * @static
  46. * @readonly
  47. * @property SITE_FIELD
  48. * @type {String}
  49. */
  50. var SITE_FIELD = pb.SiteService.SITE_FIELD;
  51.  
  52. /**
  53. * Empty constructor because this object uses static methods.
  54. * @class PluginRepository
  55. * @constructor
  56. */
  57. function PluginRepository() {}
  58.  
  59. /**
  60. * Retrieves the plugins that have themes associated with them from both site and global level.
  61. * @method loadPluginsWithThemesAvailableToThisSite
  62. * @static
  63. * @param {String} site - site unique id
  64. * @param {Function} cb - the callback function
  65. */
  66. PluginRepository.loadPluginsWithThemesAvailableToThisSite = function (site, cb) {
  67. var dao = new pb.DAO();
  68. var hasATheme = getHasThemeQuery();
  69. var belongsToSite = getBelongsToSiteQuery(site);
  70. var belongsToGlobal = getBelongsToSiteQuery(GLOBAL_SITE);
  71. var siteWhere = {
  72. $and: [hasATheme, belongsToSite]
  73. };
  74. var globalWhere = {
  75. $and: [hasATheme, belongsToGlobal]
  76. };
  77. var tasks = {
  78. sitePlugins: function (callback) {
  79. dao.q(PLUGIN_COLL, {where: siteWhere}, callback);
  80. },
  81. globalPlugins: function (callback) {
  82. dao.q(PLUGIN_COLL, {where: globalWhere}, callback);
  83. }
  84. };
  85. async.parallel(tasks, function (err, results) {
  86. if (err) {
  87. cb(err, null);
  88. } else {
  89. var sitePlugins = results.sitePlugins || [];
  90. var globalPlugins = results.globalPlugins || [];
  91. var resultArray = mergeSitePluginsWithGlobalPlugins(sitePlugins, globalPlugins);
  92. cb(null, resultArray);
  93. }
  94. });
  95. };
  96.  
  97. /**
  98. * Retrieves the plugins that have themes associated with them from site level.
  99. * @method loadPluginsWithThemesOwnedByThisSite
  100. * @static
  101. * @param {String} site - site unique id
  102. * @param {Function} cb - the callback function
  103. */
  104. PluginRepository.loadPluginsWithThemesOwnedByThisSite = function (site, cb) {
  105. var dao = new pb.DAO();
  106. var hasATheme = getHasThemeQuery();
  107. var belongsToSite = getBelongsToSiteQuery(site);
  108. var where = {
  109. $and: [hasATheme, belongsToSite]
  110. };
  111. dao.q(PLUGIN_COLL, {where: where}, cb);
  112. };
  113.  
  114. /**
  115. * Loads the plugin object on the site level from the database.
  116. * @method loadPluginOwnedByThisSite
  117. * @static
  118. * @param {String} pluginID - plugin unique id
  119. * @param {String} site - site unique id
  120. * @param {Function} cb - the callback function
  121. */
  122. PluginRepository.loadPluginOwnedByThisSite = function (pluginID, site, cb) {
  123. var hasCorrectIdentifier = getCorrectIdQuery(pluginID);
  124. var belongsToThisSite = getBelongsToSiteQuery(site);
  125.  
  126. var where = {
  127. $and: [hasCorrectIdentifier, belongsToThisSite]
  128. };
  129.  
  130. var dao = new pb.DAO();
  131. dao.loadByValues(where, PLUGIN_COLL, cb);
  132. };
  133.  
  134. /**
  135. * Loads the plugin object on the site level first. If blank, attempts to load plugin object from the global level.
  136. * @method loadPluginAvailableToThisSite
  137. * @static
  138. * @param {String} pluginID - plugin unique id
  139. * @param {String} site - site unqiue id
  140. * @param {Function} cb - the callback function
  141. */
  142. PluginRepository.loadPluginAvailableToThisSite = function (pluginID, site, cb) {
  143. PluginRepository.loadPluginOwnedByThisSite(pluginID, site, function (err, plugin) {
  144. if (util.isError(err)) {
  145. cb(err, null);
  146. return;
  147. }
  148.  
  149. if (!plugin) {
  150. if (site && site !== GLOBAL_SITE) {
  151. PluginRepository.loadPluginOwnedByThisSite(pluginID, GLOBAL_SITE, cb);
  152. return;
  153. }
  154. cb(err, null);
  155. }
  156.  
  157. cb(err, plugin);
  158. });
  159. };
  160.  
  161. /**
  162. * Load all plugin objects included in pluginIDs on a site level.
  163. * @method loadIncludedPluginsOwnedByThisSite
  164. * @static
  165. * @param {Array} pluginIDs - array of plugin unique ids
  166. * @param {String} site - site unique id
  167. * @param {Function} cb - callback function
  168. */
  169. PluginRepository.loadIncludedPluginsOwnedByThisSite = function (pluginIDs, site, cb) {
  170. if (!pluginIDs || !pluginIDs.length) {
  171. pluginIDs = [];
  172. }
  173. var idIsInTheList = getIdsInListQuery(pluginIDs);
  174. var belongsToThisSite = getBelongsToSiteQuery(site);
  175. var where = {
  176. $and: [idIsInTheList, belongsToThisSite]
  177. };
  178. var opts = {
  179. select: pb.DAO.PROJECT_ALL,
  180. where: where,
  181. order: [['created', pb.DAO.ASC]]
  182. };
  183. var dao = new pb.DAO();
  184. dao.q(PLUGIN_COLL, opts, cb);
  185. };
  186.  
  187. /**
  188. * Loads plugin objects of plugin IDs that are not include in pluginIDs on the site level.
  189. * @method loadPluginsNotIncludedOwnedByThisSite
  190. * @static
  191. * @param {Array} pluginIDs - array of plugin unique ids to exclude
  192. * @param {String} site - site unique id
  193. * @param {Function} cb - callback function
  194. */
  195. PluginRepository.loadPluginsNotIncludedOwnedByThisSite = function (pluginIDs, site, cb) {
  196. if (!pluginIDs || !pluginIDs.length) {
  197. pluginIDs = [];
  198. }
  199. var idIsNotInTheList = getIdsNotInListQuery(pluginIDs);
  200. var belongsToThisSite = getBelongsToSiteQuery(site);
  201. var where = {
  202. $and: [idIsNotInTheList, belongsToThisSite]
  203. };
  204. var opts = {
  205. select: pb.DAO.PROJECT_ALL,
  206. where: where,
  207. order: [['created', pb.DAO.ASC]]
  208. };
  209. var dao = new pb.DAO();
  210. dao.q(PLUGIN_COLL, opts, cb);
  211. };
  212.  
  213. /**
  214. * Load the entire plugin collection on both site and global levels.
  215. * @method loadPluginsAcrossAllSites
  216. * @static
  217. * @param {Function} cb - the callback function
  218. */
  219. PluginRepository.loadPluginsAcrossAllSites = function (cb) {
  220. var dao = new pb.DAO();
  221. dao.q(PLUGIN_COLL, cb);
  222. };
  223.  
  224. function getIdsNotInListQuery(pluginIDs) {
  225. return {uid: {'$nin': pluginIDs}};
  226. }
  227.  
  228. function getIdsInListQuery(pluginIDs) {
  229. return {uid: {'$in': pluginIDs}};
  230. }
  231.  
  232. function getHasThemeQuery() {
  233. return {theme: {$exists: true}};
  234. }
  235.  
  236. function getCorrectIdQuery(pluginID) {
  237. var hasCorrectIdentifier = {
  238. $or: [
  239. {},
  240. {
  241. uid: pluginID
  242. }
  243. ]
  244. };
  245. hasCorrectIdentifier.$or[0][pb.DAO.getIdField()] = pluginID;
  246. return hasCorrectIdentifier;
  247. }
  248.  
  249. function getBelongsToSiteQuery(site) {
  250. var belongsToThisSite = {};
  251. if (!site || site === GLOBAL_SITE) {
  252. var hasNoSite = {};
  253. hasNoSite[SITE_FIELD] = {$exists: false};
  254.  
  255. var siteIsGlobal = {};
  256. siteIsGlobal[SITE_FIELD] = GLOBAL_SITE;
  257.  
  258. belongsToThisSite = {
  259. $or: [
  260. hasNoSite,
  261. siteIsGlobal
  262. ]
  263. };
  264. } else {
  265. belongsToThisSite = {};
  266. belongsToThisSite[SITE_FIELD] = site;
  267. }
  268. return belongsToThisSite;
  269. }
  270.  
  271. function mergeSitePluginsWithGlobalPlugins(sitePlugins, globalPlugins) {
  272. var resultArray = [].concat(sitePlugins);
  273.  
  274. for (var j = 0; j < globalPlugins.length; j++) {
  275. var exists = false;
  276. for (var i = 0; i < sitePlugins.length; i++) {
  277. if (pluginsHaveSameID(globalPlugins[j], sitePlugins[i])) {
  278. exists = true;
  279. }
  280. }
  281. if (!exists) {
  282. resultArray.push(globalPlugins[j]);
  283. }
  284. }
  285. return resultArray;
  286. }
  287.  
  288. function pluginsHaveSameID(pluginOne, pluginTwo) {
  289. var otherIDField = pb.DAO.getIdField();
  290. if (pluginOne.uid && pluginTwo.uid) {
  291. if (pluginOne.uid === pluginTwo.uid) {
  292. return true;
  293. }
  294. }
  295. if (pluginOne[otherIDField] && pluginTwo[otherIDField]) {
  296. if (pluginOne[otherIDField] === pluginTwo[otherIDField]) {
  297. return true;
  298. }
  299. }
  300. return false;
  301. }
  302.  
  303. return PluginRepository;
  304. };
  305.