<?php
   define('USER', 'jesse.turner@gatewaybaltic.com');
   define('TOKEN', 'reNaj8P1B4r3y9=fD8RaM0a4Lf30sdBda1d210we'); 
   require 'Solve360Service.php';
   
   $projNum=$_GET["p"];
   $compId=$_GET["c"];
   $projectName=$_GET["n"];
   $solve360Service = new Solve360Service(USER, TOKEN);
   
   $comp = $solve360Service->getCompany($compId);
   $name=$comp->item->name;
   
   // 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;
         else $actType=1;
         $acts[]=array("$actDate","$actName",$actType);
      }
   }
   
   // related to (blogs) & additions to activities from relevant related contacts' activities
   $relations=array();
   foreach ($comp->relateditems->children() as $rel) {
      if ($rel->typeid==2) {
         $blogName=$rel->name;
         $relations[]="$blogName";
      } else {
         if ($rel->typeid==1) {
            $contactId=$rel->id;
            $con = $solve360Service->getContact($contactId);
            $isRelated=false;
            foreach ($con->relateditems->children() as $conrel) {
               if ($conrel->typeid==2 && $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;
                     else $actType=1;
                     $acts[]=array("$actDate","$actName",$actType);
                  }
               }
            }
         } 
      }
   }
   
   // 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->custom5722950;
   $profile=$comp->item->fields->custom4593433;
   $industry=$comp->item->fields->custom4593432;
   $background=$comp->item->fields->background;
   $created=$comp->item->created;
   
   echo json_encode(array("$projNum","$compId","$name",$acts,$relations,"$website","$phone","$address","$turnover","$employees","$profile","$industry","$background","$created"));
?>