Пример #1
0
      function done() {
        
        // Schema
        var ContentVersion = new calipso.lib.mongoose.Schema({
          contentId:{type: String}
          // All other properties are dynamically mapped, hence use of .set / .get
        });

        calipso.lib.mongoose.model('ContentVersion', ContentVersion);
        
        // Version event listeners        
        calipso.e.post('CONTENT_CREATE',module.name,saveVersion);
        calipso.e.post('CONTENT_UPDATE',module.name,saveVersion);

        // Form alteration
        if(calipso.modules.content.fn.originalContentForm) {
          // We have already altered the form, so lets set it back before altering it
          calipso.modules.content.fn.contentForm = calipso.modules.content.fn.originalContentForm;  
        }
        
        // Now, lets alter the form
        calipso.modules.content.fn.originalContentForm = calipso.modules.content.fn.contentForm;
        calipso.modules.content.fn.contentForm = function() {            
          var form = calipso.modules.content.fn.originalContentForm();
          form.sections.push(contentVersionFormSection);          
          return form;
        }
       
        next();

      }
Пример #2
0
        u.save(function(err) {
          if(err) {
            
            req.flash('error',req.t('Could not save user because {msg}.',{msg:err.message}));
            if(res.statusCode != 302) {
              // Redirect to old page
              res.redirect('/user/profile/' + username + '/edit');
            }
            
          } else {

            calipso.e.post_emit('USER_UPDATE',u);
            
            // Update session details if your account
            if(req.session.user && (req.session.user.username === username)) { // Allows for name change
              createUserSession(req, u, function(err) {
                  if(err) calipso.error("Error saving session: " + err);                 
              });
            }
            
            // Redirect to new page
            res.redirect('/user/profile/' + u.username);

          }
          // If not already redirecting, then redirect
          next();
        });
Пример #3
0
/**
 * Simple template page function
 */
function templatePage(req, res, template, block, next) {

  // Set any variables
  var myVariable = "Hello World";

  // Create a content item
  var item = {
    id: "NA",
    type: 'content',
    meta: {
      variable: myVariable
    }
  };
  
  // Raise a ping
  calipso.e.custom_emit('TEMPLATE_EVENT','PING',{req:req}, function(options) {
  
    // Render the item via the template provided above
    calipso.theme.renderItem(req, res, template, block, {
      item: item
    },next);

  });

  
};
Пример #4
0
                     c.save(function(err) {
                       if(err) {
                         
                         var errorMsg = '';
                         if(err.errors) {
                           for(var error in err.errors) {
                             errorMessage = error + " " + err.errors[error] + '\r\n';
                           }
                         } else {
                           errorMessage = err.message;
                         }
                         req.flash('error',req.t('Could not update content because {msg}',{msg:errorMessage}));
                         if(res.statusCode != 302) {  // Don't redirect if we already are, multiple errors
                           res.redirect('back');
                         }
                         next();
                         
                       } else {
                          
                         req.flash('info',req.t('Content saved.'));
                         
                         // Raise CONTENT_CREATE event
                         calipso.e.post_emit('CONTENT_UPDATE',c,function(c) {
                           if(returnTo) {
                             res.redirect(returnTo);
                           } else {
                             // use the reference to the originally id deifned by req.moduleParams.id
                             res.redirect('/content/show/' + id);
                           }
                           next();
                         });
                         
                       }                        
 
                     });
Пример #5
0
 .on('data', function(data) {
     var result = JSON.parse(data);
     if(!result.ok) {
       calipso.e.post_emit('CONTENT_INDEX',content);
       calipso.error("Error elastic search indexing: " + result); 
     }
 })
Пример #6
0
 function(data) {
     var result = JSON.parse(data);
     if(!result.ok) {
       calipso.e.post_emit('CONTENT_REMOVE_INDEX',content);
       calipso.error("Error elastic search removing: " + result); 
     }
 }).exec();
Пример #7
0
 c.save(function(err) {
   if(err) {
     calipso.debug(err);
     // TODO : err.errors is an object that contains actual fields, can pass back actual errors
     // To the form
     req.flash('error',req.t('Could not save content because {msg}.',{msg:err.message}));
     if(res.statusCode != 302) {
         res.redirect('/content/new?type='+form.content.contentType);
     }
     next();
   } else {
     req.flash('info',req.t('Content saved.'));
     
     // Raise CONTENT_CREATE event
     calipso.e.post_emit('CONTENT_CREATE',c,function(c) {
         
       if(returnTo) {
         res.redirect(returnTo);
       } else {
         res.redirect('/content/show/' + c._id);
       }
       next();
     });
     
   }
   
 });
Пример #8
0
  calipso.form.process(req,function(form) {

     if(form) {

      var ContentType = calipso.lib.mongoose.model('ContentType');

      var c = new ContentType(form.contentType);
      c.ispublic = form.contentType.contentType.ispublic === "Yes" ? true : false;

      var saved;

      calipso.e.pre_emit('CONTENT_TYPE_CREATE',c);
      
      c.save(function(err) {

        if(err) {
          req.flash('error',req.t('Could not save content type because {msg}.',{msg:err.message}));
          if(res.statusCode != 302) {
            res.redirect('/content/type/new');
          }
        } else {
          calipso.e.post_emit('CONTENT_TYPE_CREATE',c);
          res.redirect('/content/type');
        }

        // If not already redirecting, then redirect
        next();

      });

     }
  });
Пример #9
0
        ContentType.findById(id, function(err, c) {
          if (!err && c) {

              c.contentType = form.contentType.contentType;
              c.description = form.contentType.description;
              c.layout = form.contentType.layout;
              c.ispublic = form.contentType.ispublic === "Yes" ? true : false;
              c.updated = new Date();
              c.fields = form.contentType.fields;
              
              calipso.e.pre_emit('CONTENT_TYPE_UPDATE',c);
             
              c.save(function(err) {
                if(err) {
                  req.flash('error',req.t('Could not update content type because {msg}.',{msg:err.message}));
                  if(res.statusCode != 302) {  // Don't redirect if we already are, multiple errors
                    res.redirect('/content/type/edit/' + req.moduleParams.id);
                  }
                } else {
                  calipso.e.post_emit('CONTENT_TYPE_UPDATE',c);
                  res.redirect('/content/type/show/' + req.moduleParams.id);
                }
                next();
              });

          } else {
            req.flash('error',req.t('Could not locate that content type.'));
            res.redirect('/content/type');
            next();
          }
        });
Пример #10
0
/**
 * Save version
 */
function saveVersion(event,content,next) {   
    
    var ContentVersion = calipso.lib.mongoose.model('ContentVersion');
    
    // Create version and map fiels
    var version = new ContentVersion();    
    calipso.form.mapFields(content.doc,version);
    version.contentId = content._id;
    
    if(version.get("version")) {
      calipso.e.pre_emit('CONTENT_VERSION',version);
    }
    
    version.save(function(err) {
      
      if(err) {
        calipso.error(err);
      }
      if(version.get("version")) {
        // TODO - enable notification / event?
        calipso.e.post_emit('CONTENT_VERSION',version);
      }
            
      return next();
      
    });
    
}
Пример #11
0
              ContentType.findOne({contentType:form.content.contentType}, function(err, contentType) {

                  if(err || !contentType) {
                    req.flash('error',req.t('Could not save content as I was unable to locate content type {type}.',{type:form.content.contentType}));
                    res.redirect('/content');
                    next();
                  } else {

                    // Copy over content type data
                    c.contentType = contentType.contentType;
                    c.layout = contentType.layout;
                    c.ispublic = contentType.ispublic;

                    // Emit pre event
                    // This does not allow you to change the content
                    calipso.e.pre_emit('CONTENT_CREATE',c,function(c) {                    
                        
                      c.save(function(err) {
                        if(err) {
                          
                          var errorMsg = '';
                          if(err.errors) {
                            for(var error in err.errors) {
                              errorMessage = error + " " + err.errors[error] + '\r\n';
                            }
                          } else {
                            errorMessage = err.message;
                          }
                          req.flash('error',req.t('Could not update content because {msg}',{msg:errorMessage}));
                          if(res.statusCode != 302) {  // Don't redirect if we already are, multiple errors
                            res.redirect('back');
                          }
                          next();
                          
                        } else {
                           
                          req.flash('info',req.t('Content saved.'));
                          
                          // Raise CONTENT_CREATE event
                          calipso.e.post_emit('CONTENT_UPDATE',c,function(c) {
                            if(returnTo) {
                              res.redirect(returnTo);
                            } else {
                              // use the reference to the originally id deifned by req.moduleParams.id
                              res.redirect('/content/show/' + id);
                            }
                            next();
                          });
                          
                        }                        
  
                      });
                      
                    });               
                    
                  }

              });
Пример #12
0
 u.save(function(err) {        
     if(err) {
       req.flash('error',req.t('There was an error unlocking that user account.'));
     } else {
       calipso.e.post_emit('USER_UNLOCK',u);
       req.flash('info',req.t('Account unlocked.'));
     }
     res.redirect('/user/profile/' + username);    
 });
Пример #13
0
   req.session.save(function(err) {
     // Check for error
      calipso.e.post_emit('USER_LOGOUT',u);
      if(res.statusCode != 302) {
         res.redirect('back');
      }
      next();
 
   });
Пример #14
0
/**
 * Initialisation function, this is executed by calipso as the application boots up
 */
function init(module, app, next) {

  // Register for events  
  calipso.e.post('CONTENT_CREATE',module.name,indexContent);
  calipso.e.post('CONTENT_UPDATE',module.name,indexContent);  
  calipso.e.post('CONTENT_DELETE',module.name,removeContent);
  
  // Register events
  calipso.e.addEvent('CONTENT_INDEX');
  calipso.e.addEvent('CONTENT_REMOVE_INDEX');  
  
  // Do the init
  calipso.lib.step(

    function defineRoutes() {
  
      // Route on every page for search
      module.router.addRoute(/.*/, showForm, {block: 'search.form'}, this.parallel());
  
      // Page
      module.router.addRoute('POST /search',search,{block: 'content.search.results',template:'results'},this.parallel());
      module.router.addRoute('GET /search',search,{block: 'content.search.results',template:'results'},this.parallel());

      // ADMIN
      module.router.addRoute('GET /admin/search/reindex',reindex,{admin:true},this.parallel());      
      module.router.addRoute('GET /admin/search/clearindex',clearIndex,{admin:true},this.parallel());      

  
    }, function done() {
      
    // COnfigure - need to add to config
    serverOptions = {
        host: 'localhost',
        port: 9200,
    };
    connectAndMonitor(serverOptions);
    
    next();

  });

};
Пример #15
0
 User.remove({_id:u._id}, function(err) {
   if(err) {
     req.flash('info',req.t('Unable to delete the user because {msg}',{msg:err.message}));
     res.redirect("/user/list");
   } else {
     calipso.e.post_emit('USER_DELETE',u); 
     req.flash('info',req.t('The user has now been deleted.'));
     res.redirect("/user/list");
   }
   next();
 });
Пример #16
0
          ContentType.findOne({contentType:form.content.contentType}, function(err, contentType) {


              if(err || !contentType) {

                calipso.debug(err);
                req.flash('error',req.t('Could not create content as I was unable to locate content type {type}.',{type:form.content.contentType}));
                res.redirect('/content');
                next();

              } else {

                // Copy over content type data - in meta as this is
                // not mastered here
                c.contentType = contentType.contentType;
                c.layout = contentType.layout;
                c.ispublic = contentType.ispublic;

                // Emit event pre-save, this DOES NOT Allow you to change
                // The content item (yet).
                calipso.e.pre_emit('CONTENT_CREATE',c,function(c) {
                
                  c.save(function(err) {
                    if(err) {
                      calipso.debug(err);
                      // TODO : err.errors is an object that contains actual fields, can pass back actual errors
                      // To the form
                      req.flash('error',req.t('Could not save content because {msg}.',{msg:err.message}));
                      if(res.statusCode != 302) {
                          res.redirect('/content/new?type='+form.content.contentType);
                      }
                      next();
                    } else {
                      req.flash('info',req.t('Content saved.'));
                      
                      // Raise CONTENT_CREATE event
                      calipso.e.post_emit('CONTENT_CREATE',c,function(c) {
                          
                        if(returnTo) {
                          res.redirect(returnTo);
                        } else {
                          res.redirect('/content/show/' + c._id);
                        }
                        next();
                      });
                      
                    }
                    
                  });
                  
                });
              }

          });
Пример #17
0
 ContentType.remove({_id:id}, function(err) {
   if(err) {
     req.flash('info',req.t('Unable to delete the content type because {msg}.',{msg:err.message}));
     res.redirect("/content/type");
   } else {
     calipso.e.post_emit('CONTENT_TYPE_DELETE',c);
     req.flash('info',req.t('The content type has now been deleted.'));
     res.redirect("/content/type");
   }
   next();
 });
Пример #18
0
/**
 *Init
 */
function init(module,app,next) {

  // Version events
  calipso.e.addEvent('CONTENT_VERSION');
  
  calipso.lib.step(
      function defineRoutes() {

        // Menus
        module.router.addRoute('GET /content/show/:id',showContent,{admin:true},this.parallel());
        module.router.addRoute('GET /content/show/:id',showContent,{admin:true},this.parallel());
        
        // Crud operations        
        module.router.addRoute('GET /content/show/:id/versions',listVersions,{admin:true,template:'list',block:'content.version'},this.parallel());
        module.router.addRoute('GET /content/show/:id/versions/diff/:a',diffVersion,{admin:true,template:'diff',block:'content.diff'},this.parallel());
        module.router.addRoute('GET /content/show/:id/versions/diff/:a/:b',diffVersion,{admin:true,template:'diff',block:'content.diff'},this.parallel());
        module.router.addRoute('GET /content/show/:id/version/:version',showVersion,{admin:true,template:'show',block:'content.version'},this.parallel());
        module.router.addRoute('GET /content/show/:id/version/:version/revert',revertVersion,{admin:true},this.parallel());

      },
      function done() {
        
        // Schema
        var ContentVersion = new calipso.lib.mongoose.Schema({
          contentId:{type: String}
          // All other properties are dynamically mapped, hence use of .set / .get
        });

        calipso.lib.mongoose.model('ContentVersion', ContentVersion);
        
        // Version event listeners        
        calipso.e.post('CONTENT_CREATE',module.name,saveVersion);
        calipso.e.post('CONTENT_UPDATE',module.name,saveVersion);

        // Form alteration
        if(calipso.modules.content.fn.originalContentForm) {
          // We have already altered the form, so lets set it back before altering it
          calipso.modules.content.fn.contentForm = calipso.modules.content.fn.originalContentForm;  
        }
        
        // Now, lets alter the form
        calipso.modules.content.fn.originalContentForm = calipso.modules.content.fn.contentForm;
        calipso.modules.content.fn.contentForm = function() {            
          var form = calipso.modules.content.fn.originalContentForm();
          form.sections.push(contentVersionFormSection);          
          return form;
        }
       
        next();

      }
  );
}
Пример #19
0
 c.save(function(err) {
   if(err) {
     req.flash('error',req.t('Could not update content type because {msg}.',{msg:err.message}));
     if(res.statusCode != 302) {  // Don't redirect if we already are, multiple errors
       res.redirect('/content/type/edit/' + req.moduleParams.id);
     }
   } else {
     calipso.e.post_emit('CONTENT_TYPE_UPDATE',c);
     res.redirect('/content/type/show/' + req.moduleParams.id);
   }
   next();
 });
Пример #20
0
 version.save(function(err) {
   
   if(err) {
     calipso.error(err);
   }
   if(version.get("version")) {
     // TODO - enable notification / event?
     calipso.e.post_emit('CONTENT_VERSION',version);
   }
         
   return next();
   
 });
Пример #21
0
/**
 * Remove a single object
 */
function removeContent(content) {      
  
  // Remove
  calipso.e.pre_emit('CONTENT_REMOVE_INDEX',content);
  elasticSearchClient.deleteDocument('calipso', 'content', content._id)
    .on('data',
    function(data) {
        var result = JSON.parse(data);
        if(!result.ok) {
          calipso.e.post_emit('CONTENT_REMOVE_INDEX',content);
          calipso.error("Error elastic search removing: " + result); 
        }
    }).exec();
      
}
Пример #22
0
      c.save(function(err) {

        if(err) {
          req.flash('error',req.t('Could not save content type because {msg}.',{msg:err.message}));
          if(res.statusCode != 302) {
            res.redirect('/content/type/new');
          }
        } else {
          calipso.e.post_emit('CONTENT_TYPE_CREATE',c);
          res.redirect('/content/type');
        }

        // If not already redirecting, then redirect
        next();

      });
Пример #23
0
  Content.findById(id, function(err, c) {
      
    // Raise CONTENT_CREATE event
    calipso.e.pre_emit('CONTENT_DELETE',c);
    
    Content.remove({_id:id}, function(err) {
      if(err) {
        req.flash('info',req.t('Unable to delete the content because {msg}',{msg:err.message}));
        res.redirect("/");
      } else {
        calipso.e.post_emit('CONTENT_DELETE',c); 
        req.flash('info',req.t('The content has now been deleted.'));
        res.redirect("/");
      }
      next();
    });

  });
Пример #24
0
      u.save(function(err) {

        if(err) {
          req.flash('error',req.t('Could not save user because {msg}.',{msg:err.message}));
          if(res.statusCode != 302 && !res.noRedirect) {
            res.redirect('back');
          }
        } else {
          calipso.e.post_emit('USER_CREATE',u);
          if(!res.noRedirect) {
            req.flash('info',req.t('Profile created, you can now login using this account.'));
            res.redirect('/user/profile/' + u.username);
          }
        }

        // If not already redirecting, then redirect
        next(err);

      });
Пример #25
0
/**
 * Initialisation function, this is executed by calipso as the application boots up
 */
function init(module, app, next) {

  calipso.e.addEvent('TEMPLATE_EVENT');  
  
  // Version event listeners
  calipso.e.custom('TEMPLATE_EVENT','PING',module.name,templatePing);
  calipso.e.pre('CONTENT_CREATE',module.name,templateEvent);
  calipso.e.post('CONTENT_CREATE',module.name,templateEvent);  
  calipso.e.pre('CONTENT_UPDATE',module.name,templateEvent);
  calipso.e.post('CONTENT_UPDATE',module.name,templateEvent);  
  calipso.e.pre('CONTENT_CREATE_FORM',module.name,formAlter);
  calipso.e.pre('CONTENT_UPDATE_FORM',module.name,formAlter);
  
  calipso.lib.step(

  function defineRoutes() {

    // Add a route to every page, notice the 'end:false' to ensure block further routing
    module.router.addRoute(/.*/, allPages, {
      end: false,
      template: 'templateAll',
      block: 'right'
    }, this.parallel());

    // Page
    module.router.addRoute('GET /template', templatePage, {
      template: 'templateShow',
      block: 'content'
    }, this.parallel());

  }, function done() {

    // Any schema configuration goes here
    next();

  });


};
Пример #26
0
      getForm(req,"/content/" + id,req.t("Edit Content ..."),c.contentType,function(form) {

        // Default values
        var values = {content: c};

        // Fix for content type being held in meta field
        // TODO this has a bad smell
        values.contentType = values.content.contentType;
        values.returnTo = returnTo;

        res.layout = 'admin';

        // Test!
        calipso.e.pre_emit('CONTENT_UPDATE_FORM',form,function(form) {
          calipso.form.render(form,values,req,function(form) {
            calipso.theme.renderItem(req,res,form,block,{},next);
          });
        });

      });
Пример #27
0
 User.findOne({username:username}, function(err, u) {
     
     if(err || !u) {        
       req.flash('error',req.t('There was an error unlocking that user account.'));
       res.redirect('/user/list');
     }
     
     u.locked = false;
     calipso.e.pre_emit('USER_UNLOCK',u);
     u.save(function(err) {        
         if(err) {
           req.flash('error',req.t('There was an error unlocking that user account.'));
         } else {
           calipso.e.post_emit('USER_UNLOCK',u);
           req.flash('info',req.t('Account unlocked.'));
         }
         res.redirect('/user/profile/' + username);    
     });
     
 });
Пример #28
0
  getForm(req,"/content",req.t("Create Content ..."),type,function(form) {

    // Default values
    var values = {
        content: {
          title:alias,  // Default the title to the alias
          alias:alias,
          teaser:teaser,
          contentType:type,
          taxonomy:taxonomy,
          returnTo: returnTo
        }
    }

    res.layout = 'admin';
    
    calipso.e.pre_emit('CONTENT_CREATE_FORM',form,function(form) {
      calipso.form.render(form,values,req,function(form) {        
          calipso.theme.renderItem(req,res,form,block,{},next);          
      });
    });
  });
Пример #29
0
/**
 * Index an object
 */
function indexContent(event,content,next) {
    
  var toIndex = content.toObject();

  // Elastic expects all documents to have an id
  toIndex.id = toIndex._id;
  delete toIndex._id;
  
  calipso.e.pre_emit('CONTENT_INDEX',content);
  
  // Index, content, based on content type
  elasticSearchClient.index('calipso','content', toIndex)
    .on('data', function(data) {
        var result = JSON.parse(data);
        if(!result.ok) {
          calipso.e.post_emit('CONTENT_INDEX',content);
          calipso.error("Error elastic search indexing: " + result); 
        }
    })
    .exec()

  return next();
    
}
Пример #30
0
      User.findOne({username:username},function (err, user) {

        // Check if the user hash is ok, or if there is no hash (supports transition from password to hash)
        // TO BE REMOVED In later version
        if(user && calipso.lib.crypto.check(form.user.password,user.hash) || (user && user.hash === '')) {
          if(!user.locked) {            
            found = true;            
            calipso.e.post_emit('USER_LOGIN',user);
            createUserSession(req, user, function(err) {
                if(err) calipso.error("Error saving session: " + err);
            });             
          }
        }

        if(!found) {
          req.flash('error',req.t('You may have entered an incorrect username or password, please try again.  If you still cant login after a number of tries your account may be locked, please contact the site administrator.'));
        }
        
        if(res.statusCode != 302) {
          res.redirect('back');
        }
        next();
        return;
      });