Skip to content

Commit

Permalink
Merge pull request #50 from AdFabConnect/issue-46
Browse files Browse the repository at this point in the history
Issue 46
  • Loading branch information
gregorybesson committed Nov 29, 2016
2 parents 34f5de1 + 327c395 commit 9cf608b
Show file tree
Hide file tree
Showing 50 changed files with 2,170 additions and 1,007 deletions.
14 changes: 14 additions & 0 deletions src/cli/cms/data/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,18 @@ export function getTagAbeWithTab(text, tab) {

export function validDataAbe(str){
return str.replace(/\[([0-9]*)\]/g, '$1')
}

export function getWorkflowFromOperationsUrl(str){
let regUrl = /\/abe\/operations\/(.*?)\/(.*?)\//
var workflow = 'draft'
var match = str.match(regUrl)
if (match != null && match[1] != null) {
workflow = match[1]
}
var postUrl = str.replace(regUrl, '')
return {
workflow: workflow,
postUrl: postUrl
}
}
8 changes: 5 additions & 3 deletions src/cli/cms/editor/handlebars/listPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ export default function listPage(file, index, text) {
res += `<td align="center">
<div class="row icons-action">`


if(file.publish != null) {
res += `<a href="/abe/unpublish${file.abe_meta.link}"
res += `<a href="/abe/operations/publish/unpublish${file.abe_meta.link}"
title="${text.unpublish}"
class="icon" data-unpublish="true" data-text="${text.confirmUnpublish} ${file.abe_meta.link}"
title="unpublish">
<span class="glyphicon glyphicon-eye-close"></span>
</a>`
}

res += `<a href="/abe/delete${file.abe_meta.link}"


res += `<a href="/abe/operations/${file.abe_meta.status}/delete${file.abe_meta.link}"
title="${text.delete}"
class="icon"
data-delete="true"
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cms/editor/handlebars/printInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function printInput (params, root) {
}

var disabled = ''
if (!User.utils.isUserAllowedOnRoute(userWorkflow, `/abe/save/${params.status}/edit`)) {
if (!User.utils.isUserAllowedOnRoute(userWorkflow, `/abe/operations/${params.status}/edit`)) {
disabled = 'disabled="disabled"'
}
if (params.tab == 'slug') {
Expand Down
10 changes: 9 additions & 1 deletion src/cli/cms/operations/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import {
Manager,
coreUtils,
cmsTemplates,
cmsOperations,
Expand All @@ -14,6 +15,13 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate
var postUrl = path.join('/', pathCreate, name)
postUrl = coreUtils.slug.clean(postUrl)

var postExist = Manager.instance.postExist(postUrl)
if (postExist) {
var postJson = cmsData.revision.getDocumentRevision(postUrl)
resolve(postJson)
return
}

var json = (forceJson) ? forceJson : {}
json = cmsData.metas.create(json, template, postUrl)

Expand All @@ -24,10 +32,10 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate
var templateText = cmsTemplates.template.getTemplate(template)
json = cmsData.values.removeDuplicate(templateText, json)
}

var resHook = abeExtend.hooks.instance.trigger('beforeFirstSave', postUrl, req.body, json)
postUrl = resHook.postUrl
json = resHook.json

var p2 = cmsOperations.post.draft(
postUrl,
json,
Expand Down
42 changes: 40 additions & 2 deletions src/cli/cms/operations/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function unpublish(filePath) {
delete json.abe_meta.publish
}

var p = draft(
var p = cmsOperations.post.draft(
filePath,
json,
'draft'
Expand All @@ -126,6 +126,44 @@ export function unpublish(filePath) {
return p
}

export function edit(filePath, json, workflow) {
var p
if (workflow === 'publish') {
p = cmsOperations.post.publish(
filePath,
json
)
}else {
p = cmsOperations.post.draft(
filePath,
json,
workflow
)
}

return p
}

export function submit(filePath, json, workflow) {
var submitToWorkflow = 'draft'
var found = false
Array.prototype.forEach.call(config.users.workflow, (flow) => {
if (found) {
found = false
submitToWorkflow = flow
}
if (workflow === flow) {
found = true
}
})

return cmsOperations.post.edit(
filePath,
json,
submitToWorkflow
)
}

export function reject(filePath, json, workflow) {
abeExtend.hooks.instance.trigger('beforeReject', filePath)

Expand All @@ -147,7 +185,7 @@ export function reject(filePath, json, workflow) {
if(json.abe_meta.publish != null) {
delete json.abe_meta.publish
}
var p2 = draft(
var p2 = cmsOperations.post.draft(
filePath,
json,
rejectToWorkflow
Expand Down
18 changes: 0 additions & 18 deletions src/cli/cms/operations/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,3 @@ export function removeFile(file, json) {
fse.removeSync(json)
}
}

export function olderRevisionByType(filePath, type) {

const folder = path.dirname(filePath)
const file = path.basename(filePath)
const extension = path.extname(filePath)

const files = coreUtils.file.getFilesSync(folder, false, extension)
Array.prototype.forEach.call(files, (fileItem) => {
const fname = cmsData.fileAttr.delete(fileItem)
const ftype = cmsData.fileAttr.get(fileItem).s
if(fname === file && ftype === type){
const fileDraft = fileItem.replace(/-abe-./, '-abe-d')
cmsOperations.remove.removeFile(fileItem, coreUtils.file.changePath(fileItem, config.data.url).replace(extension, '.json'))
cmsOperations.remove.removeFile(fileDraft, coreUtils.file.changePath(fileDraft, config.data.url).replace(extension, '.json'))
}
})
}
36 changes: 19 additions & 17 deletions src/cli/cms/templates/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
config,
coreUtils,
cmsData,
abeExtend
abeExtend,
cmsTemplates
} from '../../'

export function getTemplatesAndPartials(templatesPath) {
Expand Down Expand Up @@ -54,24 +55,25 @@ export function getAbeImport(text) {
}

export function includePartials(text) {
var abeImports = getAbeImport(text)
var abeImports = cmsTemplates.template.getAbeImport(text)

Array.prototype.forEach.call(abeImports, (abeImport) => {
var obj = cmsData.attributes.getAll(abeImport, {})


var file = obj.file
var partial = ''
file = path.join(config.root, config.partials, file)
if(coreUtils.file.exist(file)) {
partial = includePartials(fse.readFileSync(file, 'utf8'))
partial = cmsTemplates.template.includePartials(fse.readFileSync(file, 'utf8'))
}
text = text.replace(cmsData.regex.escapeTextToRegex(abeImport, 'g'), partial)
})

return text
}

function translate(text) {
export function translate(text) {
var importReg = /({{abe.*type=[\'|\"]translate.*}})/g

var matches = text.match(importReg)
Expand Down Expand Up @@ -127,9 +129,9 @@ export function getTemplate (file) {
file = path.join(config.root, config.templates.url, file + '.' + config.files.templates.extension)
if(coreUtils.file.exist(file)) {
text = fse.readFileSync(file, 'utf8')
text = includePartials(text)
text = translate(text)
text = addOrder(text)
text = cmsTemplates.template.includePartials(text)
text = cmsTemplates.template.translate(text)
text = cmsTemplates.template.addOrder(text)
}else {
text = `[ ERROR ] template ${file + '.' + config.files.templates.extension} doesn't exist anymore`
}
Expand Down Expand Up @@ -187,13 +189,13 @@ export function recurseWhereVariables (where) {
var arRight
switch(where.operator) {
case 'AND':
arLeft = recurseWhereVariables(where.left)
arRight = recurseWhereVariables(where.right)
arLeft = cmsTemplates.template.recurseWhereVariables(where.left)
arRight = cmsTemplates.template.recurseWhereVariables(where.right)
return arLeft.concat(arRight)
break
case 'OR':
arLeft = recurseWhereVariables(where.left)
arRight = recurseWhereVariables(where.right)
arLeft = cmsTemplates.template.recurseWhereVariables(where.left)
arRight = cmsTemplates.template.recurseWhereVariables(where.right)
return arLeft.concat(arRight)
break
default:
Expand All @@ -209,7 +211,7 @@ export function getTemplatesTexts(templatesList) {
var p = new Promise((resolve) => {
Array.prototype.forEach.call(templatesList, (file) => {
var template = fse.readFileSync(file, 'utf8')
template = includePartials(template)
template = cmsTemplates.template.includePartials(template)
var name = file.replace(path.join(config.root, config.templates.url, path.sep), '').replace(`.${config.files.templates.extension}`, '')
templates.push({
name: name,
Expand Down Expand Up @@ -238,7 +240,7 @@ export function execRequestColumns(tpl) {
})
}
if(typeof request.where !== 'undefined' && request.where !== null) {
ar = ar.concat(recurseWhereVariables(request.where))
ar = ar.concat(cmsTemplates.template.recurseWhereVariables(request.where))
}
}
})
Expand All @@ -250,7 +252,7 @@ export function getAbeRequestWhereKeysFromTemplates(templatesList) {
var whereKeys = []
var p = new Promise((resolve) => {
Array.prototype.forEach.call(templatesList, (file) => {
whereKeys = whereKeys.concat(execRequestColumns(file.template))
whereKeys = whereKeys.concat(cmsTemplates.template.execRequestColumns(file.template))
})
whereKeys = whereKeys.filter(function (item, pos) {return whereKeys.indexOf(item) == pos})
resolve(whereKeys)
Expand All @@ -271,7 +273,7 @@ export function setAbeSlugDefaultValueIfDoesntExist(templateText) {
export function getAbeSlugFromTemplates(templatesList) {
var slugs = {}
Array.prototype.forEach.call(templatesList, (file) => {
var templateText = setAbeSlugDefaultValueIfDoesntExist(file.template)
var templateText = cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist(file.template)
var matchesSlug = cmsData.regex.getTagAbeWithType(templateText, 'slug')
var obj = cmsData.attributes.getAll(matchesSlug[0], {})
slugs[file.name] = obj.sourceString
Expand All @@ -295,7 +297,7 @@ export function getAbePrecontribFromTemplates(templatesList) {
var slugMatch = cmsData.regex.getTagAbeWithType(file.template, 'slug')
var templateText = file.template
if(slugMatch == null || slugMatch[0] == null) {
templateText = setAbePrecontribDefaultValueIfDoesntExist(file.template)
templateText = cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist(file.template)
}

var matchesTabSlug = cmsData.regex.getTagAbeWithTab(templateText, 'slug')
Expand All @@ -307,7 +309,7 @@ export function getAbePrecontribFromTemplates(templatesList) {
})
})

precontributionTemplate = addOrder(precontributionTemplate)
precontributionTemplate = cmsTemplates.template.addOrder(precontributionTemplate)

return {
fields: fields,
Expand Down
14 changes: 14 additions & 0 deletions src/cli/core/manager/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ class Manager {
return this
}

/**
* When a post is modified or created, this method is called so that the manager updates the list with the updated/new item
* @param {String} pathFile The full path to the post
*/
postExist(postUrl){
var parentRelativePath = cmsData.fileAttr.delete(postUrl.split('/').join(path.sep).replace(`.${config.files.templates.extension}`, '.json')).replace(/^\//, '')
const found = coreUtils.array.find(this._list, 'parentRelativePath', parentRelativePath)

if (found.length > 0) {
return true
}
return false
}

/**
* When a post is modified or created, this method is called so that the manager updates the list with the updated/new item
* @param {String} pathFile The full path to the post
Expand Down
32 changes: 18 additions & 14 deletions src/cli/users/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,31 +244,35 @@ export function getUserWorkflow(status, role) {
type = (type != null) ? type : flow
return {
status: flow,
url: `/abe/save/${type}/${action}`
url: `/abe/operations/${type}/${action}`
}
}

if (config.users.enable) {
var before = null
var found = false
var found = null
Array.prototype.forEach.call(config.users.workflow, (flow) => {
if (found) {
flows.push(addFlow(flow, flow, 'submit'))
found = false

if (found != null) {
flows.push(addFlow(flow, found, 'submit'))
found = null
}

if (status == flow) {
found = true
if (before != null) {
if (flow == 'publish') {
flows.push(addFlow('edit', 'draft', 'submit'))
}else {
flows.push(addFlow('reject', before, 'reject'))
}
found = flow
if (flow != 'draft' && flow != 'publish') {
flows.push(addFlow('reject', flow, 'reject'))
}
if (flow == 'publish') {
flows.push(addFlow('edit', 'draft', 'edit'))
}else {
flows.push(addFlow('save', flow, 'edit'))
}
flows.push(addFlow(flow, flow, 'edit'))
}
before = flow
})
if (found != null) {
flows.push(addFlow('save', 'publish', 'edit'))
}
}else {
flows = [addFlow('draft', 'draft', 'submit'), addFlow('publish', 'publish', 'submit')]
}
Expand Down
Loading

0 comments on commit 9cf608b

Please sign in to comment.