From 585644d0c9051949cb4eede036626f2fe96d2c79 Mon Sep 17 00:00:00 2001
From: aboire <thomas.craipeau@gmail.com>
Date: Sun, 2 Apr 2017 09:01:04 +0400
Subject: [PATCH] update photonews

---
 imports/api/news.js                |  9 +++
 imports/api/server/method.js       | 96 +++++++++++++++++++++++++++++-
 imports/api/server/publish.js      | 24 ++++++++
 imports/ui/news/detail/detail.html |  7 +++
 imports/ui/news/news.html          |  6 +-
 5 files changed, 139 insertions(+), 3 deletions(-)

diff --git a/imports/api/news.js b/imports/api/news.js
index accb9f9..8ee3bc8 100644
--- a/imports/api/news.js
+++ b/imports/api/news.js
@@ -135,6 +135,7 @@ News.deny({
   //collection
   if(Meteor.isClient){
     import { Photosimg } from './client/photosimg.js'
+    import { Documents } from './documents.js';
     import { Citoyens } from './citoyens.js'
     News.helpers({
       authorNews () {
@@ -145,6 +146,14 @@ News.deny({
         return Photosimg.find({_id:this.media.content.imageId});
       }
       },
+      photoNewsAlbums () {
+        if(this.media && this.media.images){
+          let arrayId = this.media.images.map((_id) => {
+            return new Mongo.ObjectID(_id)
+          })
+        return Documents.find({_id: { $in: arrayId }}).fetch();
+      }
+      },
       likesCount () {
         if (this.voteUp && this.voteUpCount) {
           return this.voteUpCount;
diff --git a/imports/api/server/method.js b/imports/api/server/method.js
index 9bccb97..70ce382 100644
--- a/imports/api/server/method.js
+++ b/imports/api/server/method.js
@@ -278,6 +278,98 @@ Meteor.methods({
     return retour;
   },
   photoNews (photo,str,type,idType) {
+    check(str, String);
+    check(type, String);
+    check(idType, String);
+    if (!this.userId) {
+      throw new Meteor.Error("not-authorized");
+    }
+    //savoir qui peut poster une photo sur l'event
+    /*if (!Events.findOne({_id:new Mongo.ObjectID(idType)}).isAdmin()) {
+      throw new Meteor.Error("not-authorized");
+    }*/
+    let retourUpload = apiCommunecter.postUploadPixel('events',idType,'newsImage',photo,str);
+    if(retourUpload){
+      let insertDoc = {};
+      insertDoc.id = idType;
+      insertDoc.type = "events";
+      insertDoc.folder = `events/${idType}/album`;
+      insertDoc.moduleId = "communecter";
+      insertDoc.doctype = "image";
+      insertDoc.name = retourUpload.name;
+      insertDoc.size = retourUpload.size;
+      //insertDoc.date = "";
+      insertDoc.contentKey = "slider";
+      insertDoc.formOrigin = "news";
+      let  doc = apiCommunecter.postPixel("document","save",insertDoc);
+      if(doc){
+        //{"result":true,"msg":"Document bien enregistr\u00e9","id":{"$id":"58df810add04528643014012"},"name":"moon.png"}
+        let insertNew = {};
+        insertNew.parentId=idType;
+        insertNew.parentType=type;
+        insertNew.text="photo";
+        insertNew["media"]={};
+        insertNew["media"]["type"]="gallery_images";
+        insertNew["media"]["countImages"]="1";
+        insertNew["media"]["images"]=[doc.data.id["$id"]];
+        newsId = Meteor.call('insertNew',insertNew);
+        if(newsId){
+          return {photoret:doc.data.id["$id"],newsId:newsId.data.id["$id"]};
+        }else{
+          throw new Meteor.Error("photoNews error");
+        }
+      }else{
+        throw new Meteor.Error("insertDocument error");
+      }
+    }else{
+      throw new Meteor.Error("postUploadPixel error");
+    }
+  },
+  photoNewsUpdate (newsId,photoId) {
+    check(newsId, String);
+    check(photoId, String);
+    if (!this.userId) {
+      throw new Meteor.Error("not-authorized");
+    }
+    let parentId = News.findOne({_id:new Mongo.ObjectID(newsId)}).target.id;
+    let parentType = News.findOne({_id:new Mongo.ObjectID(newsId)}).target.type;
+    let media={};
+    media["type"]="gallery_images";
+    media["countImages"]="1";
+    media["images"]=[photoId];
+    News.update({_id:new Mongo.ObjectID(newsId)},{$set:{'media':media}});
+    return photoId;
+  },
+  cfsbase64tos3up (photo,str,type,idType) {
+    check(photo, Match.Any);
+    check(str, Match.Any);
+    if (!this.userId) {
+      throw new Meteor.Error("not-authorized");
+    }
+    let retourUpload = apiCommunecter.postUploadPixel('events',idType,'newsImage',photo,str);
+    if(retourUpload){
+      let insertDoc = {};
+      insertDoc.id = idType;
+      insertDoc.type = "events";
+      insertDoc.folder = `events/${idType}/album`;
+      insertDoc.moduleId = "communecter";
+      insertDoc.doctype = "image";
+      insertDoc.name = retourUpload.name;
+      insertDoc.size = retourUpload.size;
+      //insertDoc.date = "";
+      insertDoc.contentKey = "slider";
+      insertDoc.formOrigin = "news";
+      let  doc = apiCommunecter.postPixel("document","save",insertDoc);
+      if(doc){
+        return doc.data.id["$id"]
+      }else{
+        throw new Meteor.Error("insertDocument error");
+      }
+    }else{
+      throw new Meteor.Error("postUploadPixel error");
+    }
+  },
+  photoNewsOld (photo,str,type,idType) {
     check(str, String);
     check(type, String);
     check(idType, String);
@@ -328,7 +420,7 @@ Meteor.methods({
       throw new Meteor.Error("photoNews error");
     }
   },
-  photoNewsUpdate (newsId,photoId) {
+  photoNewsUpdateOld (newsId,photoId) {
     check(newsId, String);
     check(photoId, String);
     if (!this.userId) {
@@ -365,7 +457,7 @@ Meteor.methods({
 
     return image._id;
   },
-  cfsbase64tos3up (photo,str,type,idType) {
+  cfsbase64tos3upOld (photo,str,type,idType) {
     check(photo, Match.Any);
     check(str, Match.Any);
     if (!this.userId) {
diff --git a/imports/api/server/publish.js b/imports/api/server/publish.js
index 151ad40..f4d6639 100644
--- a/imports/api/server/publish.js
+++ b/imports/api/server/publish.js
@@ -313,6 +313,18 @@ Meteor.publishComposite('citoyenEvents', function(latlng,radius) {
 									});
 								}
 							}
+						},
+						{
+							find: function(news) {
+								if(news.media && news.media.images){
+									let arrayId = news.media.images.map((_id) => {
+										return new Mongo.ObjectID(_id)
+									})
+									return Documents.find({
+										_id : { $in: arrayId }
+									});
+								}
+							}
 						}
 					]
 				}
@@ -348,6 +360,18 @@ Meteor.publishComposite('citoyenEvents', function(latlng,radius) {
 									});
 								}
 							}
+						},
+						{
+							find: function(news) {
+								if(news.media && news.media.images){
+									let arrayId = news.media.images.map((_id) => {
+										return new Mongo.ObjectID(_id)
+									})
+									return Documents.find({
+										_id : { $in: arrayId }
+									});
+								}
+							}
 						}
 					]
 				}
diff --git a/imports/ui/news/detail/detail.html b/imports/ui/news/detail/detail.html
index 5542cd2..636606a 100644
--- a/imports/ui/news/detail/detail.html
+++ b/imports/ui/news/detail/detail.html
@@ -26,6 +26,13 @@
       {{/each}}
     </div>
     {{/if}}
+    {{#if photoNewsAlbums}}
+    <div class="item item-body">
+      {{#each photoNewsAlbums}}
+    <img src="{{urlImageCommunecter}}/upload/{{moduleId}}/{{folder}}/{{name}}" class="full-image photo-viewer" />
+      {{/each}}
+    </div>
+    {{/if}}
     {{#if text}}
     <div class="item item-body">
       {{text}}
diff --git a/imports/ui/news/news.html b/imports/ui/news/news.html
index 5357654..b500090 100644
--- a/imports/ui/news/news.html
+++ b/imports/ui/news/news.html
@@ -23,11 +23,14 @@
     {{#if Template.subscriptionsReady}}
     {{#transition in="zoomIn" out="bounceOut"}}
     {{#each scope.news}}
-    <a href="{{pathFor 'newsDetail' _id=scope._id._str newsId=_id._str scope=target.type}}" class="item animated in {{#if photoNews}}item-thumbnail-left{{else}}{{#if imageDoc authorNews._id._str}}item-avatar{{/if}}{{/if}}">
+    <a href="{{pathFor 'newsDetail' _id=scope._id._str newsId=_id._str scope=target.type}}" class="item animated in {{#if photoNews}}item-thumbnail-left{{else}}{{#if photoNewsAlbums.[0].name}}item-thumbnail-left{{else}}{{#if imageDoc authorNews._id._str}}item-avatar{{/if}}{{/if}}{{/if}}">
 
       {{#each photoNews}}
       <img src="{{urlImageDesktop}}{{this.url store='photosLarge' uploading='/default.png' storing='/default.png'}}"  />
       {{else}}
+      {{#if photoNewsAlbums.[0].name}}
+      {{> imgDoc imageDoc=photoNewsAlbums.[0] resized="80x80"}}
+      {{else}}
       {{#if imageDoc}}
       {{> imgDoc imageDoc=imageDoc resized="80x80"}}
       {{else}}
@@ -35,6 +38,7 @@
       {{> imgDoc imageDoc=(imageDoc authorNews._id._str) resized="80X80"}}
       {{/if}}
       {{/if}}
+      {{/if}}
       {{/each}}
 
       <h2>{{#if name}}{{cutString name 50}}{{/if}}</h2>
-- 
GitLab