Esempio n. 1
0
 /**
  * Supplements the content in the set
  *
  * @param array  $context  Context for supplementing
  * @return void
  */
 public function supplement($context = array())
 {
     $hash = Debug::markStart('content', 'supplementing');
     if ($this->supplemented) {
         return;
     }
     $this->supplemented = true;
     $context = Helper::ensureArray($context);
     // determine context
     $given_context = $context;
     $context = array('locate_with' => isset($given_context['locate_with']) ? $given_context['locate_with'] : null, 'center_point' => isset($given_context['center_point']) ? $given_context['center_point'] : null, 'list_helpers' => isset($given_content['list_helpers']) ? $given_context['list_helpers'] : true, 'context_urls' => isset($given_context['context_urls']) ? $given_context['context_urls'] : true, 'total_found' => isset($given_context['total_found']) ? $given_context['total_found'] : null, 'group_by_date' => isset($given_context['group_by_date']) ? $given_context['group_by_date'] : null, 'inherit_folder_data' => isset($given_context['inherit_folder_data']) ? $given_context['inherit_folder_data'] : true, 'merge_with_data' => isset($given_context['merge_with_data']) ? $given_context['merge_with_data'] : true);
     // set up helper variables
     $center_point = false;
     if ($context['center_point'] && preg_match(Pattern::COORDINATES, $context['center_point'], $matches)) {
         $center_point = array($matches[1], $matches[2]);
     }
     // contextual urls are based on current page, not individual data records
     // we can figure this out once and then set it with each one
     if ($context['context_urls']) {
         $raw_url = Request::getResourceURI();
         $page_url = Path::tidy($raw_url);
     }
     // iteration memory
     $last_date = null;
     // loop through content, supplementing each record with data
     foreach ($this->content as $content_key => $data) {
         // locate
         if ($context['locate_with']) {
             $location_data = isset($data[$context['locate_with']]) ? $data[$context['locate_with']] : null;
             // check that location data is fully set
             if (is_array($location_data) && isset($location_data['latitude']) && $location_data['latitude'] && isset($location_data['longitude']) && $location_data['longitude']) {
                 $data['latitude'] = $location_data['latitude'];
                 $data['longitude'] = $location_data['longitude'];
                 $data['coordinates'] = $location_data['latitude'] . "," . $location_data['longitude'];
                 // get distance from center
                 if ($center_point) {
                     $location = array($data['latitude'], $data['longitude']);
                     $data['distance_km'] = Math::getDistanceInKilometers($center_point, $location);
                     $data['distance_mi'] = Math::convertKilometersToMiles($data['distance_km']);
                 }
             }
         }
         // contextual urls
         if ($context['context_urls']) {
             $data['raw_url'] = $raw_url;
             $data['page_url'] = $page_url;
         }
         // total entries
         if ($context['total_found']) {
             $data['total_found'] = (int) $context['total_found'];
         }
         // group by date
         if ($context['group_by_date'] && $data['datestamp']) {
             $formatted_date = Date::format($context['group_by_date'], $data['datestamp']);
             if ($formatted_date !== $last_date) {
                 $last_date = $formatted_date;
                 $data['grouped_date'] = $formatted_date;
             } else {
                 $data['grouped_date'] = '';
             }
         }
         // loop through content to add data for variables that are arrays
         foreach ($data as $key => $value) {
             // Only run on zero indexed arrays/loops
             if (is_array($value) && isset($value[0]) && !is_array($value[0])) {
                 // list helpers
                 if ($context['list_helpers']) {
                     // make automagic lists
                     $data[$key . "_list"] = join(", ", $value);
                     $data[$key . "_spaced_list"] = join(" ", $value);
                     $data[$key . "_option_list"] = join("|", $value);
                     $data[$key . "_ordered_list"] = "<ol><li>" . join("</li><li>", $value) . "</li></ol>";
                     $data[$key . "_unordered_list"] = "<ul><li>" . join("</li><li>", $value) . "</li></ul>";
                     $data[$key . "_sentence_list"] = Helper::makeSentenceList($value);
                     $data[$key . "_ampersand_sentence_list"] = Helper::makeSentenceList($value, "&", false);
                     // handle taxonomies
                     if (Taxonomy::isTaxonomy($key)) {
                         $url_list = array_map(function ($item) use($data, $key, $value) {
                             return '<a href="' . Taxonomy::getURL($data['_folder'], $key, $item) . '">' . $item . '</a>';
                         }, $value);
                         $data[$key . "_url_list"] = join(", ", $url_list);
                         $data[$key . "_spaced_url_list"] = join(" ", $url_list);
                         $data[$key . "_ordered_url_list"] = "<ol><li>" . join("</li><li>", $url_list) . "</li></ol>";
                         $data[$key . "_unordered_url_list"] = "<ul><li>" . join("</li><li>", $url_list) . "</li></ul>";
                         $data[$key . "_sentence_url_list"] = Helper::makeSentenceList($url_list);
                         $data[$key . "_ampersand_sentence_url_list"] = Helper::makeSentenceList($url_list, "&", false);
                     }
                 }
             }
         }
         // update content with supplemented data merged with global config data
         if ($context['merge_with_data'] || $context['inherit_folder_data']) {
             $folder_data = array();
             $all_config = array();
             if ($context['inherit_folder_data']) {
                 $folder_data = $this->getFolderData($data['_file']);
             }
             if ($context['merge_with_data']) {
                 $all_config = Config::getAll();
             }
             // merge them all together
             $this->content[$content_key] = $data + $folder_data + $all_config;
         } else {
             $this->content[$content_key] = $data;
         }
     }
     Debug::markEnd($hash);
 }