Exemple #1
0
 /**
  * Populates custom field values for updating the lead. Also retrieves social media data
  *
  * @param Lead       $lead
  * @param array      $data
  * @param bool|false $overwriteWithBlank
  * @param bool|true  $fetchSocialProfiles
  *
  * @return array
  */
 public function setFieldValues(Lead &$lead, array $data, $overwriteWithBlank = false, $fetchSocialProfiles = true)
 {
     if ($fetchSocialProfiles) {
         //@todo - add a catch to NOT do social gleaning if a lead is created via a form, etc as we do not want the user to experience the wait
         //generate the social cache
         list($socialCache, $socialFeatureSettings) = $this->integrationHelper->getUserProfiles($lead, $data, true, null, false, true);
         //set the social cache while we have it
         if (!empty($socialCache)) {
             $lead->setSocialCache($socialCache);
         }
     }
     //save the field values
     $fieldValues = $lead->getFields();
     if (empty($fieldValues)) {
         // Lead is new or they haven't been populated so let's build the fields now
         static $fields;
         if (empty($fields)) {
             $fields = $this->leadFieldModel->getEntities(['filter' => ['isPublished' => true], 'hydration_mode' => 'HYDRATE_ARRAY']);
             $fields = $this->organizeFieldsByGroup($fields);
         }
         $fieldValues = $fields;
     }
     //update existing values
     foreach ($fieldValues as $group => &$groupFields) {
         foreach ($groupFields as $alias => &$field) {
             if (!isset($field['value'])) {
                 $field['value'] = null;
             }
             // Only update fields that are part of the passed $data array
             if (array_key_exists($alias, $data)) {
                 $curValue = $field['value'];
                 $newValue = $data[$alias];
                 if ($curValue !== $newValue && (strlen($newValue) > 0 || strlen($newValue) === 0 && $overwriteWithBlank)) {
                     $field['value'] = $newValue;
                     $lead->addUpdatedField($alias, $newValue, $curValue);
                 }
                 //if empty, check for social media data to plug the hole
                 if (empty($newValue) && !empty($socialCache)) {
                     foreach ($socialCache as $service => $details) {
                         //check to see if a field has been assigned
                         if (!empty($socialFeatureSettings[$service]['leadFields']) && in_array($field['alias'], $socialFeatureSettings[$service]['leadFields'])) {
                             //check to see if the data is available
                             $key = array_search($field['alias'], $socialFeatureSettings[$service]['leadFields']);
                             if (isset($details['profile'][$key])) {
                                 //Found!!
                                 $field['value'] = $details['profile'][$key];
                                 $lead->addUpdatedField($alias, $details['profile'][$key]);
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     }
     $lead->setFields($fieldValues);
 }