Skip to content

Commit

Permalink
Merge branch '0.5.0' into factories
Browse files Browse the repository at this point in the history
Conflicts:
	plugins/pencilblue/controllers/api/content/article_api_controller.js
  • Loading branch information
brianhyder committed Sep 28, 2015
2 parents c5d9dd3 + d55856a commit 9a7e36a
Show file tree
Hide file tree
Showing 16 changed files with 454 additions and 435 deletions.
146 changes: 73 additions & 73 deletions include/service/entities/article_service.js
@@ -1,19 +1,19 @@
/*
Copyright (C) 2015 PencilBlue, LLC
Copyright (C) 2015 PencilBlue, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

//dependencies
var async = require('async');
Expand All @@ -36,9 +36,10 @@ module.exports = function ArticleServiceModule(pb) {
function ArticleService(siteUid, onlyThisSite){
this.object_type = ARTICLE_TYPE;
this.site = pb.SiteService.getCurrentSite(siteUid);
this.onlyThisSite = onlyThisSite;
this.siteQueryService = new pb.SiteQueryService({site: this.site, onlyThisSite: onlyThisSite});
}

/**
*
* @private
Expand All @@ -48,7 +49,7 @@ module.exports = function ArticleServiceModule(pb) {
* @type {String}
*/
var ARTICLE_TYPE = 'article';

/**
*
* @private
Expand Down Expand Up @@ -223,7 +224,7 @@ module.exports = function ArticleServiceModule(pb) {
*/
ArticleService.prototype.update = function(articleId, fields, options, cb) {
if(!util.isObject(fields)){
return cb(new Error('The fields parameter is required'));
return cb(new Error('The fields parameter is required'));
}

var where = pb.DAO.getIdWhere(articleId);
Expand All @@ -249,7 +250,7 @@ module.exports = function ArticleServiceModule(pb) {
cb = options;
options = cb;
}

var self = this;

if (this.getContentType() === ARTICLE_TYPE) {
Expand All @@ -272,18 +273,18 @@ module.exports = function ArticleServiceModule(pb) {

if(contentSettings.display_timestamp ) {
article.timestamp = pb.ContentService.getTimestampTextFromSettings(
article.publish_date,
contentSettings
article.publish_date,
contentSettings
);
}

if(article.article_layout.indexOf('^read_more^') > -1) {
if(articleCount > 1) {
article.article_layout = article.article_layout.substr(0, article.article_layout.indexOf('^read_more^')) + ' <a href="' + pb.config.siteRoot + '/article/' + article.url + '">' + contentSettings.read_more_text + '...</a>';
}
else {
article.article_layout = article.article_layout.split('^read_more^').join('');
}
if(articleCount > 1) {
article.article_layout = article.article_layout.substr(0, article.article_layout.indexOf('^read_more^')) + ' <a href="' + pb.config.siteRoot + '/article/' + article.url + '">' + contentSettings.read_more_text + '...</a>';
}
else {
article.article_layout = article.article_layout.split('^read_more^').join('');
}
}
else if(articleCount > 1 && contentSettings.auto_break_articles) {
var breakString = '<br>';
Expand All @@ -305,7 +306,7 @@ module.exports = function ArticleServiceModule(pb) {
else {
breakString = '</div>';
tempLayout = article.article_layout.split('<div><br></div>').join(breakString + '^dbl_pgf_break^')
.split('<div><br /></div>').join(breakString + '^dbl_pgf_break^');
.split('<div><br /></div>').join(breakString + '^dbl_pgf_break^');
}

// Split the layout by paragraphs and remove any empty indices
Expand Down Expand Up @@ -343,7 +344,7 @@ module.exports = function ArticleServiceModule(pb) {
}

article.layout = article.article_layout;
var mediaLoader = new MediaLoader();
var mediaLoader = new MediaLoader({site: self.site, onlyThisSite: self.onlyThisSite});
mediaLoader.start(article[this.getContentType()+'_layout'], options, function(err, newLayout) {
article.layout = newLayout;
delete article.article_layout;
Expand Down Expand Up @@ -465,29 +466,29 @@ module.exports = function ArticleServiceModule(pb) {
cb = opts;
opts = {};
}

var ts = new pb.TemplateService(opts);
ts.load('elements/article', function(err, articleTemplate) {
ts.load('elements/article/byline', function(err, bylineTemplate) {
cb(articleTemplate, bylineTemplate);
});
});
};

/**
* Retrieves the SEO metadata for the specified content.
* Retrieves the SEO metadata for the specified content.
* @method getMetaInfo
* @param {Object} article The article to retrieve information for
* @param {Function} cb A callback that takes two parameters. The first is
* an Error, if occurred. The second is an object that contains 4
* properties:
* title - the SEO title,
* description - the SEO description,
* keywords - an array of SEO keywords that describe the content,
* thumbnail - a URI path to the thumbnail image
* @param {Function} cb A callback that takes two parameters. The first is
* an Error, if occurred. The second is an object that contains 4
* properties:
* title - the SEO title,
* description - the SEO description,
* keywords - an array of SEO keywords that describe the content,
* thumbnail - a URI path to the thumbnail image
*/
ArticleService.prototype.getMetaInfo = function(article, cb) {
var serviceV2 = new pb.ArticleServiceV2({site: this.site});
var serviceV2 = new pb.ArticleServiceV2({site: this.site, onlyThisSite: this.onlyThisSite});
serviceV2.getMetaInfo(article, cb);
};

Expand All @@ -502,14 +503,14 @@ module.exports = function ArticleServiceModule(pb) {
if(util.isNullOrUndefined(article)) {
return cb('', '', '');
}

//log deprecation
pb.log.warn('ArticleService: ArticleService.getMetaInfo is deprecated and will be removed 0.5.0. Use the prototype function getMetaInfo instead');

//all wrapped up to ensure we can be multi-threaded here for backwards
//compatibility
(function(cb) {

var articleService = new ArticleService();
articleService.getMetaInfo(article, function(err, meta) {
if (util.isError(err)) {
Expand All @@ -520,15 +521,15 @@ module.exports = function ArticleServiceModule(pb) {
});
})(cb);
};

/**
* Provided the content descriptor and the content settings object the
* function indicates if comments should be allowed within the given
* Provided the content descriptor and the content settings object the
* function indicates if comments should be allowed within the given
* context of the content.
* @method allowComments
* @param {Object} contentSettings The settings object retrieved from the
* @param {Object} contentSettings The settings object retrieved from the
* content service
* @param {Object} content The page or article that should or should not
* @param {Object} content The page or article that should or should not
* have associated comments.
* @return {Boolean}
*/
Expand All @@ -544,13 +545,12 @@ module.exports = function ArticleServiceModule(pb) {
* @constructor
* @submodule Entities
*/
function MediaLoader() {

function MediaLoader(opts) {
/**
* @property mediaService
* @type {MediaService}
*/
this.mediaService = new pb.MediaService();
this.mediaService = new pb.MediaService(null, opts.site, opts.onlyThisSite);
};

/**
Expand All @@ -568,26 +568,26 @@ module.exports = function ArticleServiceModule(pb) {
if (!util.isObject(options.media)) {
options.media = {};
}

//scan for media that should be retrieved
var flags = this.scanForFlags(articleLayout);
if (flags.length === 0) {
return cb(null, articleLayout);
}

//reconcile what media is already cached and that which should be loaded
var idsToRetrieve = [];
flags.forEach(function(flag) {
if (!options.media[flag.id]) {
idsToRetrieve.push(flag.id);
};
});

//when all media is already cached just do the processing
if (idsToRetrieve.length === 0) {
return this.onMediaAvailable(articleLayout, options, cb);
}

//retrieve the media that we need
var self = this;
var opts = {
Expand All @@ -597,17 +597,17 @@ module.exports = function ArticleServiceModule(pb) {
if (util.isError(err)) {
return cb(err);
}

//cache the retrieved media
var idField = pb.DAO.getIdField();
media.forEach(function(mediaItem) {
options.media[mediaItem[idField].toString()] = mediaItem;
});

self.onMediaAvailable(articleLayout, options, cb);
});
};

/**
* @method onMediaAvailable
* @param {String} articleLayout
Expand All @@ -616,25 +616,25 @@ module.exports = function ArticleServiceModule(pb) {
*/
MediaLoader.prototype.onMediaAvailable = function(articleLayout, options, cb) {
var self = this;

this.getMediaTemplate(options, function(err, mediaTemplate) {
options.mediaTemplate = mediaTemplate;

async.whilst(
function() {return articleLayout.indexOf('^media_display_') >= 0;},
function(callback) {
self.replaceMediaTag(articleLayout, mediaTemplate, options.media, function(err, newArticleLayout) {
articleLayout = newArticleLayout;
callback();
});
},
function(err) {
cb(err, articleLayout);
}
function() {return articleLayout.indexOf('^media_display_') >= 0;},
function(callback) {
self.replaceMediaTag(articleLayout, mediaTemplate, options.media, function(err, newArticleLayout) {
articleLayout = newArticleLayout;
callback();
});
},
function(err) {
cb(err, articleLayout);
}
);
});
};

/**
* Retrieves the media template for rendering media
* @method getMediaTemplate
Expand All @@ -645,14 +645,14 @@ module.exports = function ArticleServiceModule(pb) {
if (options.mediaTemplate) {
return cb(null, options.mediaTemplate);
}

var templatePath = options.mediaTemplatePath || 'elements/media';
var ts = new pb.TemplateService(options);
ts.load('elements/media', cb);
};

/**
* Scans a string for media flags then parses them to return an array of
* Scans a string for media flags then parses them to return an array of
* each one that was found
* @method scanForFlags
* @param {String} layout
Expand All @@ -662,12 +662,12 @@ module.exports = function ArticleServiceModule(pb) {
if (!util.isString(layout)) {
return [];
}

var flags = [];
var index = 0;
while( (index = layout.indexOf('^media_display_')) >= 0) {
flags.push(pb.MediaService.extractNextMediaFlag(layout));

var nexPosition = layout.indexOf('^', index + 1);
layout = layout.substr(nexPosition);
}
Expand Down

0 comments on commit 9a7e36a

Please sign in to comment.