<?php
   define('USER', 'inese.andersone@gatewaybaltic.com');
         define('TOKEN', 'F274va12MeWcTdUfPfB9r7X3=3b0naF9I7=003r0'); 
   require 'Solve360Service.php';
   
   $projNum=$_GET["p"];
   $compId=$_GET["c"];
   $projectName=$_GET["n"];
   $solve360Service = new Solve360Service(USER, TOKEN);
   
   $comp = $solve360Service->getCompany($compId);
   $isClient=false;
   foreach ($comp->categories->children() as $cat) {
      if ($cat->id=='17487432' || $cat->id=='17487426' || $cat->id=='17487420') $isClient=true;
   }
   
   $name=$comp->item->name;
   if ($isClient==true) $name='CLIENTDETAILS-NOTCOMPANY';
   
   // activities
   $acts=array();
   foreach ($comp->activities->children() as $activity) {
      if ($activity->typeid==3 || $activity->typeid==73) {
         $actName=$activity->name;
         $actDate=$activity->created;
         if ($activity->typeid==73) {
            $actType=0;
            //$actDate=$activity->fields->dateoccured;
         } else $actType=1;
         $acts[]=array("$actDate","$actName",$actType,"COMPANYROOTPAGE");
      }
   }
   
   // related to (blogs) & additions to activities from relevant related contacts' activities
   $relations=array();
   $contacts=array();
   foreach ($comp->relateditems->children() as $rel) {
      if ($rel->typeid==2) {
         $blogName=$rel->name;
         $relations[]="$blogName";
      } else {
         if ($rel->typeid==1) {
            $contactName=$rel->name;
            $contactId=$rel->id;
            $con = $solve360Service->getContact($contactId);
            //
            $contactTitle=$con->item->fields->jobtitle;
            $contactPhone=$con->item->fields->businessphonedirect;
            $contactMobile=$con->item->fields->cellularphone;
            $contactWebsite=$con->item->fields->website;
            $contactEmail=$con->item->fields->businessemail;
            $contactCountry=$con->item->fields->custom4698752;
            $contactAssigned=$con->item->fields->assignedto;
            $contactRelated='';
            //
            $isRelated=false;
            foreach ($con->relateditems->children() as $conrel) {
               if ($conrel->typeid==2) {
                  $store=$conrel->name;
                  if ($contactRelated!='') $contactRelated.=', ';
                  $contactRelated.="$store";
                  if ($conrel->name==$projectName) $isRelated=true;
               }
            }
            if ($isRelated==true) {
               foreach ($con->activities->children() as $activity) {
                  if ($activity->typeid==3 || $activity->typeid==73) {
                     $actName=$activity->name;
                     $actDate=$activity->created;
                     if ($activity->typeid==73) {
                        $actType=0;
                        $actDate=$activity->fields->dateoccured;
                     } else $actType=1;
                     $conName=$con->item->name;
                     $acts[]=array("$actDate","$actName",$actType,"$conName");
                  }
               }
            }
            $contacts[]=array("$contactName","$contactTitle","$contactPhone","$contactMobile","$contactEmail", "$contactWebsite","$contactCountry", "$contactAssigned", "$contactRelated", $isRelated, array(-1), "$contactId");
         } 
      }
   }
   
   // other fields
   $website=$comp->item->fields->website;
   $phone=$comp->item->fields->phone;
   $address=$comp->item->fields->mainaddress;
   $turnover=$comp->item->fields->custom4656526;
   $employees=$comp->item->fields->custom10891353;
   $profile=$comp->item->fields->custom4593433;
   $industry=$comp->item->fields->custom4593432;
   $background=$comp->item->fields->background;
   $created=$comp->item->created;
   $country=$comp->item->fields->custom4593431;
   
   echo json_encode(array("$projNum","$compId","$name",$acts,$relations,"$website","$phone","$address","$turnover","$employees","$profile","$industry","$background","$country","$created",0,array(""),array(""),array(""),array(""),array(0),array(0),array(""),array(""),array(""),array(""),array(0),$contacts));
?>