UNPKG

sheercms

Version:

Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.

463 lines (403 loc) 14.7 kB
app.service('GroupService', function($q, $http, $modal, HelperService, DSCacheFactory) { var self = this; var groupCache = DSCacheFactory('groupCache', { maxAge: 1800000, // Items added to this cache expire after 30 minutes. cacheFlushInterval: 3600000, // This cache will clear itself every hour. deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire. storageMode: 'localStorage' // This cache will sync itself with `localStorage`. }); self.list = function (branch, parentId) { var d = $q.defer(); var branches = []; if (branch.indexOf(',') > -1) branches = branch.split(','); else branches.push(branch); var groups = []; async.eachSeries(branches, function (branch, done) { try { getGroups(branch, parentId).then(function(result) { if (result.groups) { for(var j = 0; j < result.groups.length; j++) { groups.push(result.groups[j]); } } done(); }); } catch(e) { logger.error(e); done(e); } }, function (err) { if (err) d.resolve({success: false, message: err.message}); else d.resolve({success: true, groups: groups}); }); return d.promise; }; function getGroups(branch, parentId) { var d = $q.defer(); var url = '/cms/groups/list?branch=' + branch; var data = groupCache.get(url); if (data) { d.resolve(data); } else { $http({ url: url, method: 'get', params: {} }).success(function (data) { if (data) { if (data.groups) { for(var i = 0; i < data.groups.length; i++) data.groups[i].cats = HelperService.join(data.groups[i].categories, ', '); } groupCache.put(url, data); } d.resolve(data); }).error(function (data, status) { d.reject(data); }); } return d.promise; } self.getGroup = function(groupId) { var d = $q.defer(); $http({ url:"/cms/groups/get", method: 'get', params: { groupId: groupId }}).success(function(data) { if (data.group) { data.group.cats = HelperService.join(data.group.categories, ', '); } console.log('group data: ' + JSON.stringify(data)); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.create = function(group) { var d = $q.defer(); var json = JSON.stringify(group); $http.post("/cms/groups/create", {data: json}).success(function(data) { //clear the cache for this parent list clearCache(group.branch); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.update = function(group) { var d = $q.defer(); //convert the categories string to an array if (group.cats && group.cats.length > 0) { group.categories = HelperService.splitTrim(group.cats, ','); } else group.categories = []; var json = JSON.stringify(group); $http.post("/cms/groups/update", {data: json}).success(function(data) { //clear the cache for this parent list clearCache(group.branch); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.delete = function(group) { var d = $q.defer(); $http.post("/cms/groups/delete", {groupId: group._id}).success(function(data) { //clear the cache for this parent list clearCache(group.branch); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.recalculate = function(group) { var d = $q.defer(); $http.post("/cms/groups/recalculate", {groupId: group._id}).success(function(data) { //clear the cache for this parent list clearCache(group.branch); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; /* positions: [{groupId, position}] */ self.reorder = function(groups, branch, parentId) { var d = $q.defer(); var arr = []; for(var i = 0; i < groups.length; i++) { var group = groups[i]; var p = { groupId: group._id, name: group.name, position: (i + 1) }; arr.push(p); } var json = JSON.stringify(arr); $http.post("/cms/groups/reorder", {data: json}).success(function(data) { //clear the cache for this parent list clearCache(branch); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.clearAllCache = function() { groupCache.removeAll(); console.log("All group cache cleared."); }; self.clearCache = function(branch) { clearCache(branch); }; function clearCache(branch) { groupCache.remove('/cms/groups/list?branch=' + branch); } }); var GroupPropsDialogController = function ($scope, $modalInstance, GroupService, RoleService, group, types) { $scope.group = null; $scope.original = null; $scope.canDelete = false; $scope.perms = { role: [], selectedRole: null }; $scope.dialog = { title: "Group Properties", message: "", messageType: "", tab: "general" }; $scope.views = [ { name: 'list', text: 'List', checked: false }, { name: 'gallery', text: 'Gallery', checked: false }, { name: 'calendar', text: 'Calendar', checked: false } ]; $scope.sortByOptions = [ { text: 'Name', value: 'name'}, { text: 'Date Created', value: 'created'}, { text: 'Date Modified', value: 'modified'}, { text: 'Start Date', value: 'startdate'} ]; $scope.sortDirOptions = [ { text: 'Ascending', value: 'asc'}, { text: 'Descending', value: 'desc'} ]; $scope.resizeModeOptions = [ { text: 'None', value: 'none'}, { text: 'Max', value: 'max'}, { text: 'Crop', value: 'crop'}, { text: 'Stretch', value: 'stretch'} ]; $scope.resizeQualityOptions = []; $scope.types = []; $scope.selectedTypes = []; $scope.changeTab = function(name) { $scope.dialog.tab = name; } $scope.delete = function() { if (confirm('Are you sure you want to delete this group?')) { setMessage("Deleting group...", "alert-info"); GroupService.delete($scope.group).then(function(result) { if (result && result.success === true) { $modalInstance.close(true); } else if (result) { setMessage(result.message, "alert-danger"); } else { setMessage("Error deleting the group!", "alert-danger"); } }); } } $scope.recalculate = function() { setMessage("Recalculating the group item count...", "alert-info"); GroupService.recalculate($scope.group).then(function(result) { if (result && result.success === true) { setMessage("Group item count recalculated to: " + result.itemCount, "alert-info"); } else if (result) { setMessage(result.message, "alert-danger"); } else { setMessage("Error recalculating the group item count!", "alert-danger"); } }); }; $scope.urlPatternHelp = function() { alert('Item ID: {id}\nItem Name: {n}\nDate Year: {dy}\nDate Month: {dm}\nDate Day: {dd}\nExample: /blog/{dy}/{dm}/{n}'); }; $scope.ok = function() { //set the allowed types var typeIds = []; for(var i = 0; i < $scope.selectedTypes.length; i++) { typeIds.push($scope.selectedTypes[i]._id); } $scope.group.contentTypes = typeIds; //set the views var views = []; for(var i = 0; i < $scope.views.length; i++) { if ($scope.views[i].checked === true) views.push($scope.views[i].name); } $scope.group.views = views; //save the changes setMessage("Saving group properties...", "alert-info"); GroupService.update($scope.group).then(function(result) { if (result && result.success === true) { $modalInstance.close(true); } else if (result) { setMessage(result.message, "alert-danger"); } else { setMessage("Error saving the group properties!", "alert-danger"); } }); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; $scope.addPermRole = function() { if ($scope.perms.selectedRole) { if (!$scope.group.perms) $scope.group.perms = []; //see if this role is already in the list of permissions for(var i = 0; i < $scope.group.perms.length; i++) { var p = $scope.group.perms[i]; if (p.role === $scope.perms.selectedRole) { alert('This role already has permissions on this group!'); return; } } //add this role to the permissions $scope.group.perms.push(new Permission($scope.perms.selectedRole)); } else alert('You must select a role from the dropdown!'); }; $scope.removePerm = function(perm, index) { if ($scope.group.perms && $scope.group.perms.length > index) { $scope.group.perms.splice(index, 1); } }; function init() { $scope.original = group; //load the quality options $scope.resizeQualityOptions = []; for(var i = 0; i <= 100; i+=2) $scope.resizeQualityOptions.push(i); //create a copy of the group for editing var copy = { _id: group._id, parentId: group.parentId, name: group.name, desc: group.desc, branch: group.branch, icon: group.icon, itemCount: group.itemCount, childCount: group.childCount, contentTypes: group.contentTypes, perms: group.perms, urlPattern: group.urlPattern, views: group.views, sortBy: group.sortBy || 'name', sortDir: group.sortDir || 'asc', position: group.position, created: group.created, modified: group.modified, mediaSettings: group.mediaSettings, cats: group.cats, categories: group.categories }; $scope.group = copy; //load only the non-abstract types with the correct branch $scope.types = []; if (types) { for(var i = 0; i < types.length; i++) { var t = types[i]; if (t.abstract !== true) { //check the branch with the media type if ($scope.group.branch === 'content' && (t.mediaType == null || t.mediaType === '' || t.mediaType === 'None')) $scope.types.push(t); else if ($scope.group.branch === 'media' && t.mediaType && t.mediaType !== '' && t.mediaType !== 'None') $scope.types.push(t); } } } //set the selected templates if (copy.contentTypes) { for(var i = 0; i < copy.contentTypes.length; i++) { //find the template object with this id for(var j = 0; j < $scope.types.length; j++) { if ($scope.types[j]._id == copy.contentTypes[i]) { $scope.selectedTypes.push($scope.types[j]); break; } } } } //load the views array if (copy.views && copy.views.length > 0) { for(var i = 0; i < copy.views.length; i++) { var name = copy.views[i]; for(var j = 0; j < $scope.views.length; j++) { if ($scope.views[j].name == name) { $scope.views[j].checked = true; break; } } } } else { //check the list view for(var j = 0; j < $scope.views.length; j++) { if ($scope.views[j].name == 'list') { $scope.views[j].checked = true; break; } } } $scope.canDelete = (copy.itemCount === 0 && copy.childCount === 0); loadRoles(); } function loadRoles() { RoleService.getRoles(true).then(function(data) { $scope.perms.roles = []; if (data.success === false) { setMessage(data.message, (data.success === true ? "alert-success" : "alert-danger"), true); } else if (data.roles) { for(var i = 0; i < data.roles.length; i++) { $scope.perms.roles.push(data.roles[i].name); } } }); } function setMessage(msg, type) { $scope.dialog.messageType = type; $scope.dialog.message = msg; } function Permission(role) { var self = this; self.role = role; self.create = false; self.read = false; self.update = false; self.delete = false; self.publish = false; } init(); };