<?php
   define('USER', 'jesse.turner@gatewaybaltic.com');
   define('TOKEN', 'reNaj8P1B4r3y9=fD8RaM0a4Lf30sdBda1d210we'); 
   require 'Solve360Service.php';
   
   $proj=$_GET["proj"];
   $company=$_GET["comp"];
   $indx=$_GET["indx"];
   $compId=$_GET["id"];
   $projectName=$_GET["projName"];
   $solve360Service = new Solve360Service(USER, TOKEN);
   
   $comp = $solve360Service->getCompany($compId);
   
   $categoryTags=array();
   // check category tags to see if any client tags
   $isClient=false;
   foreach ($comp->categories->children() as $cat) {
      if ($cat->id=='17487432' || $cat->id=='17487426' || $cat->id=='17487420') $isClient=true;
      else {
         $catName=$cat->name;
         $categoryTags[]="$catName";
      }
   }
   
   $name=$comp->item->name;
   if ($isClient==true) $name='CLIENTDETAILS-NOTCOMPANY';
   
   // company page activities
   $acts=array();
   foreach ($comp->activities->children() as $activity) {
      if ($activity->typeid==3 || $activity->typeid==73) {
         $actId=$activity->id;
         $actName=$activity->name;
         $actCreated=$activity->created;
         $actType=$activity->typeid;
         if ($actType==73) $actDate=$activity->fields->dateoccured;
         else $actDate='';
         $actCreatorId=$activity->creatorid;
         $actCreatorName=$activity->creatorname;
         $acts[]=array("$actId","$actName","$actType","COMPANYROOTPAGE","$actCreated","$actDate","$actCreatorId","$actCreatorName");
      }
   }
   
   // related to (blogs) & activities from related contacts
   $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='';
            //
            // contact project relations
            $isRelated=false;
            $contactRelated=array();
            foreach ($con->relateditems->children() as $conrel) {
               if ($conrel->typeid==2) {
                  $store=$conrel->name;
                  $contactRelated[]="$store";
                  if ($store==$projectName) $isRelated=true;
               }
            }
            // contact activities
            foreach ($con->activities->children() as $activity) {
               if ($activity->typeid==3 || $activity->typeid==73) {
                  $actId=$activity->id;
                  $actName=$activity->name;
                  $actCreated=$activity->created;
                  $actType=$activity->typeid;
                  if ($actType==73) $actDate=$activity->fields->dateoccured;
                  else $actDate='';
                  $actCreatorId=$activity->creatorid;
                  $actCreatorName=$activity->creatorname;
                  //
                  //$conName=$con->item->name;
                  $acts[]=array("$actId","$actName","$actType","$contactId","$actCreated","$actDate","$actCreatorId","$actCreatorName");
               }
            }
            $contacts[]=array("$contactId", "$contactName","$contactTitle","$contactPhone","$contactMobile","$contactEmail", "$contactWebsite","$contactCountry", "$contactAssigned", $contactRelated,);
            // don't think we need isRelated, as will share contacts between projects
         } 
      }
   }
   
   // 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;
   $assignedto=$comp->item->fields->assignedto;
   //$project=custom5722951; NB: was at end of array
   
   echo json_encode(array("$proj","$company","$indx","$name",$acts,$relations,$categoryTags,"$website","$phone","$address","$turnover","$employees","$industry","$background","$country","$created",$contacts,"$compId","$assignedto"));
?>