<?php

namespace App\Http\Controllers;

use App\Http\Controllers\UserController;
use App\Models\Article;
use App\Models\Author;
use App\Models\Issue;
use App\Models\Journal;
use App\Models\Volume;
use App\Models\Log;
use App\Models\Country;
use App\Models\JMSContent;
use App\Models\Node;
use App\Models\Keywords;
use App\Models\Meta;
use App\Rules\Captcha;
use App\User;
use Auth;
use Cookie;
use DB;
use Hash;
use File;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;

class JMSContentController extends BenthamController
{
    public function index(Request $request)
    {	
        $editPermission   = false;
        $deletePermission = false;
        $article_list  = array();

        $param['journal_id']    = $journal_id   = $request->get('journal_id');
        $param['article_type']  = $article_type = $request->get('article_type');
        $param['jms_ref_no']    = $jms_ref_no   = $request->get('jms_ref_no');
        $param['status']        = $status       = $request->get('status');

        $frm_search = $request->get('frm_search');
        $module     = $request->get('module');

        $article_list = JMSContent::GetJMSContentList($param);
        $journal_search =  DB::table('journal')->orderBy('subtitle')->get();

        if(checkPermission('JMSContent', 'edit')) {
            $editPermission = true;
        }
        if(checkPermission('JMSContent', 'delete')) {
            $deletePermission = true;
        }
        // dd($article_list);
        return View::make('admin.jms_content.jms_content_list', with(['article_list' => $article_list, 'param' => $param, 'journal_search' => $journal_search, 'editPermission' => $editPermission, 'deletePermission' => $deletePermission]));
    }

    public function create()
    {
        return View::make('admin.orders.bundle_form');
    }

    public function store(Request $request)
    { 
        $validator = Validator::make($request->all(), [
            'volume_id'     => 'required',
        ]);

        if($validator->fails()) {
            return back()->withErrors($validator)
                ->withInput();
        } 
        else 
        {
        // try transaction start here for data integrity
            $log_article  = array();
            $log_authors  = array();
            $log_keywords = array();
            $log_meta     = array();
            $article_type='';
            $user=Auth::user();
            $jms_article = JMSContent::GetJMSContentId($request->id);
            $add_content = array();

            if(!empty($jms_article->content))
            {
                try
                {
                    DB::beginTransaction();

                    $jsonData = json_decode($jms_article->content);
                   // dd($jsonData->manuscript->journal_code);

                    $journal_id     = $request->journal_id;
                    $volume_id      = $request->volume_id;
                    $ms_id          = trim($jsonData->manuscript->ms_id);
                    $jms_ref_no     = trim($jsonData->manuscript->reference_number);
                    $title          = str_replace("'", "''", htmlspecialchars($jsonData->manuscript->manuscript_title, ENT_QUOTES));
                    $abstract       = str_replace("'", "''",htmlspecialchars($jsonData->manuscript->abstract, ENT_QUOTES));
                    $is_epub        = $jsonData->manuscript->is_epub;
                    $receivedate    = isset($jsonData->manuscript->submission_date) ? strftime('%Y-%m-%d',$jsonData->manuscript->submission_date) : '';
                    $revisedate     = isset($jsonData->manuscript->revision_date) ? strftime('%Y-%m-%d',$jsonData->manuscript->revision_date) : '';
                    $acceptdate     = isset($jsonData->manuscript->acceptance_date) ? strftime('%Y-%m-%d',$jsonData->manuscript->acceptance_date) : '';
                    $publish_on     = time();
                    $epub_abstract_date     = isset($jsonData->manuscript->epub_abstract_date) ? $jsonData->manuscript->epub_abstract_date : '';
                    $epub_full_text_date    = $jsonData->manuscript->epub_full_text_date;
					
                    $art_type       = (object) DB::select("select * from article_type where LOWER(type_name) = '".trim(strtolower($jsonData->manuscript->article_type))."'")[0];
                    $art_type       = $art_type->art_type_id;
                    $keywords       = $jsonData->manuscript->keyword;

                    //--------------------Insert Node Table--------------
                    $add_node = array(
                        'type'     => 'article',
                        'title'    => $title,
                        'language' => 'en',
                        'uid'      => $user->id,
                        'status'   => 1,
                        'created'  => time(),
                        'changed'  => time(),
                    );

                    $node_id = Node::Add($add_node);
                    $issue_id = 0;
                    //---------------------Create DOI--------------------
                    $doi = Article::GetArticleDOI($journal_id, $volume_id, $issue_id, 0, $is_epub);
                    //---------------------Create eLocator---------------
                    $elocator = 'e' . date('dmy') . $node_id;
                    //---------------------Add Article Table-------------
                    $article_obj = new article;
                    $article_obj->table      = "article";
                    $article_obj->nid        = $node_id;
                    $article_obj->journal_id = $journal_id;
                    $article_obj->volume_id  = $volume_id;
                    $article_obj->issue_id   = $issue_id;
                    $article_obj->js_id      = isset($section_id) ? $section_id : 0;
                    $article_obj->jms_ref_no = $jms_ref_no;
                    $article_obj->title      = $title;
                    $article_obj->art_type   = $art_type;
                    $article_obj->doi        = $doi;
                    $article_obj->elocator   = $elocator;

                    $article_obj->is_epub = $is_epub;

                    if($is_epub == 4) {

                        $article_obj->epub_abstract_date = $epub_abstract_date;
                    }
                    if($is_epub == 1) {
                        $article_obj->epub_full_text_date = $epub_full_text_date;
                    }

                    $article_obj->epub_type         = isset($epub_type) ? $epub_type : 0;
                    $article_obj->abstract          = $abstract;
                    $article_obj->receivedate       = $receivedate;
                    $article_obj->revisedate        = $revisedate;
                    $article_obj->acceptdate        = $acceptdate;
                    $article_obj->article_status    = 'A';
                    $article_obj->publish_on        = time();
                    $article_obj->last_modified     = time();
                    $article_obj->created           = time();
                    $article_obj->created_by        = $user->id;
                    $article_obj->updated_by        = $user->id;
                    $article_obj->save();
                    $article_id = $article_obj->id;

                    //-------------------Insert into Keywords table---------------
                    $keywords_input = $keywords;
                    if($keywords_input != "") {
                        $keywordsArray = explode(",", $keywords_input);
                        if(!empty($keywordsArray)) {

                            for ($a = 0; $a < count($keywordsArray); $a++) {
                                $add_keywords[] = array(
                                    'keyword' => trim($keywordsArray[$a]),
                                    'article_id' => $article_id,
                                );
                                $log_keywords['article_keywords'][$a] = $add_keywords;
                            }
                            Keywords::AddKeywords($add_keywords);
                        }
                    }
                   //------------Add Structure Abstract-------------- 
                   if(is_array($jsonData->manuscript->structure_abstract))
                   {
                        for($a=0; $a < sizeof($jsonData->manuscript->structure_abstract); $a++)
                        {
                            $add_abstract = array();                        
                            $add_abstract['article_id']        = $article_id;
                            $add_abstract['sequence']          = trim($jsonData->manuscript->structure_abstract[$a]->sequence);
                            $add_abstract['field_name']        = trim($jsonData->manuscript->structure_abstract[$a]->field_name);
                            $add_abstract['field_description'] = str_replace("'", "''", htmlspecialchars($jsonData->manuscript->structure_abstract[$a]->field_description));
                            $abstract_id      = Article::InsertArticleAbstractDetail($add_abstract);

                            $add_log = array();
                            $add_log = array(
                                'content_id'      => $abstract_id,
                                'content_type_id' => config('constants.log_content_types.Article_Abstract'),
                                'action'          => config('constants.log_action_types.insert'),
                                'log_data'        => json_encode($add_abstract),
                                'created_dated'   => time(),
                                'created_by'      => Auth::user()->id,
                                'ip'              => ip2long($request->ip()),
                            );
                            $log = Log::AddLog($add_log);
                        }
                   }

                   //-------------------Add Article Funders--------------------
                   if(is_array($jsonData->manuscript->funders))
                   {
                        $add_funder = array();
                        $funder_awards = array();
                        
                        for($a=0; $a < sizeof($jsonData->manuscript->funders); $a++)
                        {
                            $add_funder['article_id']               = $article_id;
                            $add_funder['funder_name']              = trim($jsonData->manuscript->funders[$a]->funder_name);
                            $add_funder['funder_registry_ids']      = trim($jsonData->manuscript->funders[$a]->ms_funder_id);
                            $funder_id = Article::InsertArticleFunderDetail($add_funder);

                           if(is_array($jsonData->manuscript->funders[$a]->funder_awards))
                           {
                                for($b=0; $b < sizeof($jsonData->manuscript->funders[$a]->funder_awards); $b++)
                                {
                                    $funder_awards['funder_id']    = $funder_id;
                                    $funder_awards['article_id']   = $article_id;
                                    $funder_awards['award_number'] = trim($jsonData->manuscript->funders[$a]->funder_awards[$b]->award);
                                    
                                    Article::InsertFunderAwards($funder_awards);
                                }
                           }

                            $log = array_merge($add_funder, $funder_awards);
                            $add_log = array();
                            $add_log = array(
                                'content_id'      => $funder_id,
                                'content_type_id' => config('constants.log_content_types.Article_Funder'),
                                'action'          => config('constants.log_action_types.insert'),
                                'log_data'        => json_encode($log),
                                'created_dated'   => time(),
                                'created_by'      => Auth::user()->id,
                                'ip'              => ip2long($request->ip()),
                            );
                            $log = Log::AddLog($add_log);
                       }
                   }

                   //-------------------Add Article Author--------------------
                   if(is_array($jsonData->manuscript->authors))
                   {
                        for($a=0; $a < sizeof($jsonData->manuscript->authors); $a++)
                        {
                            $add_author = array();
                            $add_author['article_id']           = $article_id;
                            $add_author['iseditorcoeditor']     = isset($jsonData->manuscript->authors[$a]->is_principal) ? 'a' : 'ca';
                            $add_author['sequence']             = trim($jsonData->manuscript->authors[$a]->sequence);
                            $add_author['orcid']                = trim($jsonData->manuscript->authors[$a]->orcid);
                            $add_author['h_index']              = trim($jsonData->manuscript->authors[$a]->h_index);
                            $add_author['is_principle']         = trim($jsonData->manuscript->authors[$a]->is_principal);
                            $add_author['first_name']           = trim($jsonData->manuscript->authors[$a]->first_name);
                            if($jsonData->manuscript->authors[$a]->middle_name != "")
                            {
                              $add_author['last_name'] = trim($jsonData->manuscript->authors[$a]->middle_name." ".$jsonData->manuscript->authors[$a]->last_name); 
                            }
                            else
                            {
                              $add_author['last_name'] = trim($jsonData->manuscript->authors[$a]->last_name); 
                            }
                            $add_author['email']                = trim($jsonData->manuscript->authors[$a]->email);
                            $add_author['country']              = trim($jsonData->manuscript->authors[$a]->country);
                           $author_id = Author::AddAuthor($add_author);
                           $add_author['author_id'] = $author_id;

                            $log_authors['author'][$a] = $add_author;

                           if(is_array($jsonData->manuscript->authors[$a]->affiliation))
                           {    
                                for($b=0; $b < sizeof($jsonData->manuscript->authors[$a]->affiliation); $b++)
                                {
                                    $add_affiliation = array();
                                    $add_affiliation['author_id']   = $author_id;
                                    $add_affiliation['institution'] = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->institution);
                                    $add_affiliation['department']  = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->department);
                                    $add_affiliation['address']     = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->address);
                                    $add_affiliation['country']     = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->country);
                                    $add_affiliation['city']        = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->city);
                                    $add_affiliation['postal_code'] = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->postal_code);
                                    $add_affiliation['phone']       = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->phone);
                                    $add_affiliation['fax']         = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->fax);
                                    $add_affiliation['institution_id'] = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->institution_id);
                                    $add_affiliation['web_view']    = addslashes(str_replace('"', "'", $add_affiliation['institution']." ".$add_affiliation['department']." ".$add_affiliation['city']." ".$add_affiliation['country']));
                                    $add_affiliation['aff_id']      = Article::InsertAuthorAffiliation($add_affiliation);

                                    $add_log = array();
                                    $add_log = array(
                                        'content_id'      => $add_affiliation['aff_id'],
                                        'content_type_id' => config('constants.log_content_types.Author_Affiliation'),
                                        'action'          => config('constants.log_action_types.insert'),
                                        'log_data'        => json_encode($add_affiliation),
                                        'created_dated'   => time(),
                                        'created_by'      => Auth::user()->id,
                                        'ip'              => ip2long($request->ip()),
                                    );
                                    $log = Log::AddLog($add_log);
                                }
                           }
                            
                       }
                   }

                    //------------------Update import JMS Table and Insert into log table----------------
                        $jms_content['jms_ref_no'] = $jms_ref_no;
                        $jms_content['is_epub'] = $is_epub;
                        $jms_content['status'] = 1;
                    
                    JMSContent::UpdateJMSContent($jms_content);

                    $log     = array_merge($add_node, $log_article, $log_keywords, $log_authors, $log_meta);
                    $add_log = array();
                    $add_log = array(
                        'content_id'      => $article_id,
                        'content_type_id' => config('constants.log_content_types.Article'),
                        'action'          => config('constants.log_action_types.insert'),
                        'log_data'        => json_encode($log),
                        'created_dated'   => time(),
                        'created_by'      => Auth::user()->id,
                        'ip'              => ip2long($request->ip()),
                    );
                    $log = Log::AddLog($add_log);


                    DB::commit();
                } catch (Exception $e) {

                    DB::rollback();
                    Session::flash('error', 'Somthing wrong with this page. Please try again!');
                    return Redirect::to('admin/jmscontent');
                }
            }
        }

        return Redirect::to('admin/jmscontent')->with('success', 'Article Has Been Sucessfully Imported.');
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
    }

    public function update(Request $request, $article_id)
    {   
        // try transaction start here for data integrity
            $log_article  = array();
            $log_authors  = array();
            $log_keywords = array();
            $log_meta     = array();
            $add_content = array();
            $article_type='';
            $user=Auth::user();
            $jms_article = JMSContent::GetJMSContentId($request->id);
            $article = Article::GetArticleById($article_id);

            if(!empty($jms_article->content))
            {
                try
                {
                    DB::beginTransaction();
                    
                    $jsonData = json_decode($jms_article->content);
                   // dd($jsonData->manuscript->journal_code);

                    if(isset($jsonData->manuscript->is_epub) && $jsonData->manuscript->is_epub == 1)
                        {
                            $journal_id     = $request->journal_id;
                            $volume_id      = $request->volume_id;
                            $ms_id          = trim($jsonData->manuscript->ms_id);
                            $jms_ref_no     = trim($jsonData->manuscript->reference_number);
                            $title          = str_replace("'", "''", htmlspecialchars($jsonData->manuscript->manuscript_title, ENT_QUOTES));
                            $abstract       = str_replace("'", "''",htmlspecialchars($jsonData->manuscript->abstract, ENT_QUOTES));
                            $is_epub        = trim($jsonData->manuscript->is_epub);
                            $epub_full_text_date    = $jsonData->manuscript->epub_full_text_date;
                            $art_type       = (object) DB::select("select * from article_type where LOWER(type_name) = '".trim(strtolower($jsonData->manuscript->article_type))."'")[0];
                            $art_type       = $art_type->art_type_id;
                            $keywords       = $jsonData->manuscript->keyword;

                            if ($is_epub == 1) {
                                $epub_type = "epub-article";
                                } 
                                else 
                                {
                                $epub_type = "";
                                }                              

                             $issue_id = 0;
                            
                            if(isset($request->update_article))
                            {
                                //---------------------update Article Table-------------
                                $update_obj['journal_id']           = $journal_id;
                                $update_obj['volume_id']            = $volume_id;
                                $update_obj['js_id']                = isset($section_id) ? $section_id : 0;
                                $update_obj['title']                = $title;
                                $update_obj['art_type']             = $art_type;
                                $update_obj['is_epub']              = $is_epub;
                                $update_obj['epub_full_text_date']  = $epub_full_text_date;
                                $update_obj['epub_type']            = isset($epub_type) ? $epub_type : 0;
                                $update_obj['abstract']             = $abstract;
                                $update_obj['last_modified']        = time();
                                $update_obj['updated_by']           = $user->id;
                                
                                Article::where('article_id', $article_id)->update($update_obj);

                    /*------------------ Add Article in Journal Access---------------
                    4 = Commentary          6 = Editorial               14 = Preface        
                    17 = Erratum            22 = Patent selection       21 = Biography              
                    31 = Dedication         32 = Obituary               39 = Achievements highlights
                    18 = Acknowledgment     43 = Perspective            35 = Retraction Notice
                    60 = Withdrawal Notice  57 = Editors Perspective    52 = Corrigendum
                    */
                                if($is_epub == 1) 
                                {
                    $article_type_arr = array(4,6,14,18,17,22,21,43,31,32,39,35,60,57,52);

                     if(in_array($art_type, $article_type_arr))
                     {
                        $add_article['content_type']            = 'a';
                        $add_article['content_type_id']         = $article_id;
                        $add_article['content_type_access_id']  = 1;
                        $add_article['perpetual']               = 1;
                        $add_article['remarks']                 = 'Automatically Free';
                        $add_article['from_date']               = null;
                        $add_article['to_date']                 = null;
                        $add_article['system_time']             = time();
                        $add_article['system_user_name']        = $user->id;
                    
                        $add_article_data[] = $add_article;
                        empty($add_article);

                        // (2).  Insert All articles Contetnt Access
                        ContentAccess::insert($add_article_data);
                     }
                                 }
                    
                    //-------------------Insert into Keywords table---------------
                    $keywords_input = $keywords;
                    if($keywords_input != "") {
                                    Keywords::DeleteKeywords($article_id, 'article_id');
                        $keywordsArray = explode(",", $keywords_input);
                        if(!empty($keywordsArray)) {

                            for ($a = 0; $a < count($keywordsArray); $a++) {
                                $add_keywords[] = array(
                                                'keyword' => trim($keywordsArray[$a]),
                                    'article_id' => $article_id,
                                );
                                $log_keywords['article_keywords'][$a] = $add_keywords;
                            }
                            Keywords::AddKeywords($add_keywords);
                        }
                                } else {
                                    Keywords::DeleteKeywords($article_id, 'article_id');
                    }
                   //------------Add Structure Abstract-------------- 
                   if(is_array($jsonData->manuscript->structure_abstract))
                   {
                                    Article::DeleteAbstractByArticleID($article_id);

                        for($a=0; $a < sizeof($jsonData->manuscript->structure_abstract); $a++)
                        {
                                        $add_abstract = array();
                            $add_abstract['article_id']        = $article_id;
                            $add_abstract['sequence']          = trim($jsonData->manuscript->structure_abstract[$a]->sequence);
                                        $add_abstract['field_name']        = trim($jsonData->manuscript->structure_abstract[$a]->field_name);
                            $add_abstract['field_description'] = str_replace("'", "''", htmlspecialchars($jsonData->manuscript->structure_abstract[$a]->field_description));
                            $abstract_id      = Article::InsertArticleAbstractDetail($add_abstract);

                            $add_log = array();
                            $add_log = array(
                                'content_id'      => $abstract_id,
                                'content_type_id' => config('constants.log_content_types.Article_Abstract'),
                                'action'          => config('constants.log_action_types.insert'),
                                'log_data'        => json_encode($add_abstract),
                                'created_dated'   => time(),
                                'created_by'      => Auth::user()->id,
                                'ip'              => ip2long($request->ip()),
                            );
                            $log = Log::AddLog($add_log);
                        }
                   }

                   //-------------------Add Article Funders--------------------
                   if(is_array($jsonData->manuscript->funders))
                   {
                        $add_funder = array();
                        $funder_awards = array();

                                    Article::RemoveFunderByArticleID($article_id);

                        for($a=0; $a < sizeof($jsonData->manuscript->funders); $a++)
                        {
                            $add_funder['article_id']               = $article_id;
                                        $add_funder['funder_name']              = trim($jsonData->manuscript->funders[$a]->funder_name);
                                        $add_funder['funder_registry_ids']      = trim($jsonData->manuscript->funders[$a]->ms_funder_id);
                            $funder_id = Article::InsertArticleFunderDetail($add_funder);

                           if(is_array($jsonData->manuscript->funders[$a]->funder_awards))
                           {
                                for($b=0; $b < sizeof($jsonData->manuscript->funders[$a]->funder_awards); $b++)
                                {
                                    $funder_awards['funder_id']    = $funder_id;
                                    $funder_awards['article_id']   = $article_id;
                                                $funder_awards['award_number'] = trim($jsonData->manuscript->funders[$a]->funder_awards[$b]->award);
                                    
                                    Article::InsertFunderAwards($funder_awards);
                                }
                           }

                            $log = array_merge($add_funder, $funder_awards);
                            $add_log = array();
                            $add_log = array(
                                'content_id'      => $funder_id,
                                'content_type_id' => config('constants.log_content_types.Article_Funder'),
                                'action'          => config('constants.log_action_types.insert'),
                                'log_data'        => json_encode($log),
                                'created_dated'   => time(),
                                'created_by'      => Auth::user()->id,
                                'ip'              => ip2long($request->ip()),
                            );
                            $log = Log::AddLog($add_log);
                       }
                   }

                   //-------------------Add Article Author--------------------
                   if(is_array($jsonData->manuscript->authors))
                   {
                                    $author = Author::GetAuthorsByContentId($article_id,'article_id');
                                    foreach ($author as $value) {
                                       Article::RemoveAuthorAffiliation($value->author_id,'author_id');
                                    }
                                    Author::DeleteAuthors($article_id,'article_id');

                        for($a=0; $a < sizeof($jsonData->manuscript->authors); $a++)
                        {
                                        $add_author = array();
                            $add_author['article_id']           = $article_id;
                            $add_author['iseditorcoeditor']     = isset($jsonData->manuscript->authors[$a]->is_principal) ? 'a' : 'ca';
                                        $add_author['sequence']             = trim($jsonData->manuscript->authors[$a]->sequence);
                                        $add_author['orcid']                = trim($jsonData->manuscript->authors[$a]->orcid);
                                        $add_author['h_index']              = trim($jsonData->manuscript->authors[$a]->h_index);
                                        $add_author['is_principle']         = trim($jsonData->manuscript->authors[$a]->is_principal);
                                        $add_author['first_name']           = trim($jsonData->manuscript->authors[$a]->first_name);
                            if($jsonData->manuscript->authors[$a]->middle_name != "")
                            {
                                          $add_author['last_name'] = trim($jsonData->manuscript->authors[$a]->middle_name." ".$jsonData->manuscript->authors[$a]->last_name); 
                            }
                            else
                            {
                                          $add_author['last_name'] = trim($jsonData->manuscript->authors[$a]->last_name); 
                            }
                                        $add_author['email']                = trim($jsonData->manuscript->authors[$a]->email);
                                        $add_author['country']              = trim($jsonData->manuscript->authors[$a]->country);
                           $author_id = Author::AddAuthor($add_author);
                           $add_author['author_id'] = $author_id;

                            $log_authors['author'][$a] = $add_author;

                           if(is_array($jsonData->manuscript->authors[$a]->affiliation))
                           {
                                for($b=0; $b < sizeof($jsonData->manuscript->authors[$a]->affiliation); $b++)
                                {
                                                $add_affiliation = array();
                                    $add_affiliation['author_id']   = $author_id;
                                                $add_affiliation['institution'] = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->institution);
                                                $add_affiliation['department']  = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->department);
                                                $add_affiliation['address']     = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->address);
                                                $add_affiliation['country']     = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->country);
                                                $add_affiliation['city']        = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->city);
                                                $add_affiliation['postal_code'] = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->postal_code);
                                                $add_affiliation['phone']       = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->phone);
                                                $add_affiliation['fax']         = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->fax);
                                                $add_affiliation['institution_id'] = trim($jsonData->manuscript->authors[$a]->affiliation[$b]->institution_id);
                                                $add_affiliation['web_view']    = trim(addslashes(str_replace('"', "'", $add_affiliation['institution']." ".$add_affiliation['department']." ".$add_affiliation['city']." ".$add_affiliation['country'])));
                                    $add_affiliation['aff_id']      = Article::InsertAuthorAffiliation($add_affiliation);

                                    $add_log = array();
                                    $add_log = array(
                                        'content_id'      => $add_affiliation['aff_id'],
                                        'content_type_id' => config('constants.log_content_types.Author_Affiliation'),
                                        'action'          => config('constants.log_action_types.insert'),
                                        'log_data'        => json_encode($add_affiliation),
                                        'created_dated'   => time(),
                                        'created_by'      => Auth::user()->id,
                                        'ip'              => ip2long($request->ip()),
                                    );
                                    $log = Log::AddLog($add_log);
                                }
                           }
                            
                       }
                   }
                           }
                           elseif(isset($request->update_status))
                           {
                                $update_obj['is_epub']              = $is_epub;
                                $update_obj['epub_full_text_date']  = $epub_full_text_date;
                                $update_obj['epub_type']            = isset($epub_type) ? $epub_type : 0;
                                $update_obj['last_modified']        = time();
                                $update_obj['updated_by']           = $user->id;

                                Article::where('article_id', $article_id)->update($update_obj);
                           }
                    
                            //-----------Update import JMS Table and Insert into log table----------
                            $jms_content['jms_ref_no'] = $jms_ref_no;
                            $jms_content['is_epub'] = $is_epub;
                            $jms_content['status'] = 2;

                            JMSContent::UpdateJMSContent($jms_content);
                    
                            $meta        = Meta::NodeMeta($article->nid);
                            $log_meta    = (array) $meta;
                            $log     = array_merge($log_article, $log_keywords, $log_authors, $log_meta);
                    $add_log = array();
                    $add_log = array(
                        'content_id'      => $article_id,
                        'content_type_id' => config('constants.log_content_types.Article'),
                        'action'          => config('constants.log_action_types.insert'),
                        'log_data'        => json_encode($log),
                        'created_dated'   => time(),
                        'created_by'      => Auth::user()->id,
                        'ip'              => ip2long($request->ip()),
                    );
                    $log = Log::AddLog($add_log);

                        }
                        else
                        {
                            Session::flash('error', 'This is not Fulltext article');
                            return Redirect::to('admin/jmscontent');
                        }


                    DB::commit();
                } catch (Exception $e) {

                    DB::rollback();
                    Session::flash('error', 'Somthing wrong with this page. Please try again!');
                    return Redirect::to('admin/jmscontent');
                }
            }

           return Redirect::to('admin/jmscontent')->with('success', 'Article Has Been Sucessfully Updated.');
    }


    public function destroy($id, Request $request)
    {

    }


 }