public function index($value, $parameters = array()) { if (!isset($parameters[0])) { return 'Unknown'; } if (!preg_match(Pattern::COORDINATES, $value, $point_1_matches)) { return 'Unknown'; } if (!preg_match(Pattern::COORDINATES, $parameters[0], $point_2_matches)) { return 'Unknown'; } $point_1 = array($point_1_matches[1], $point_1_matches[2]); $point_2 = array($point_2_matches[1], $point_2_matches[2]); $distance = Math::getDistanceInKilometers($point_1, $point_2); return Math::convertKilometersToMiles($distance); }
/** * 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); }
/** * Takes a dot-notated key and finds the value for it in the given * array or object. * * @param string $key Dot-notated key to find * @param array|object $data Array or object to search * @param mixed $default Default value to use if not found * @return mixed */ protected function getVariable($key, $data, $default = null) { $modifiers = null; if (strpos($key, "|") === false) { } else { $parts = explode("|", $key); $key = $parts[0]; $modifiers = array_splice($parts, 1); } if (strpos($key, $this->scopeGlue) === false) { $parts = explode('.', $key); } else { $parts = explode($this->scopeGlue, $key); } foreach ($parts as $key_part) { if (is_array($data)) { if (!array_key_exists($key_part, $data)) { return $default; } $data = $data[$key_part]; } elseif (is_object($data)) { if (!isset($data->{$key_part})) { return $default; } $data = $data->{$key_part}; } else { return $default; } } if ($modifiers) { foreach ($modifiers as $mod) { if (strpos($mod, ":") === false) { $modifier_name = $mod; $modifier_params = array(); } else { $parts = explode(":", $mod); $modifier_name = $parts[0]; $modifier_params = array_splice($parts, 1); } if ($modifier_name == 'trim') { $data = trim($data); } elseif ($modifier_name == 'img') { $data = '<img src="' . \Path::toAsset($data) . '" />'; } elseif ($modifier_name == 'link') { if (filter_var($data, FILTER_VALIDATE_EMAIL)) { // email address $data = '<a href="mailto:' . $data . '" />' . $data . '</a>'; } else { $data = '<a href="' . $data . '" />' . $data . '</a>'; } } elseif ($modifier_name == 'upper') { $data = strtoupper($data); } else { if ($modifier_name == 'lower') { $data = strtolower($data); } else { if ($modifier_name == 'slugify') { $data = \Slug::make($data); } else { if ($modifier_name == 'deslugify') { $data = trim(preg_replace('~[-_]~', ' ', $data), " "); } else { if ($modifier_name == 'title') { $data = ucwords($data); } else { if ($modifier_name == 'format') { $data = date($modifier_params[0], $data); } else { if ($modifier_name == 'format_number') { $decimals = isset($modifier_params[0]) ? $modifier_params[0] : 0; $data = number_format($data, $decimals); } else { if ($modifier_name == 'in_future') { $data = \Date::resolve($data) > time() ? "true" : ""; } else { if ($modifier_name == 'in_past') { $data = \Date::resolve($data) < time() ? "true" : ""; } else { if ($modifier_name == 'markdown') { $data = Markdown($data); } else { if ($modifier_name == 'textile') { $textile = new \Textile(); $data = $textile->TextileThis($data); } else { if ($modifier_name == 'length') { if (is_array($data)) { $data = count($data); } else { $data = strlen($data); } } else { if ($modifier_name == 'scramble') { $data = str_shuffle($data); } else { if ($modifier_name == 'word_count') { $data = str_word_count($data); } else { if ($modifier_name == 'obfuscate') { $data = \HTML::obfuscateEmail($data); } else { if ($modifier_name == 'rot13') { $data = str_rot13($data); } else { if ($modifier_name == 'urlencode') { $data = urlencode($data); } else { if ($modifier_name == 'urldecode') { $data = urldecode($data); } else { if ($modifier_name == 'striptags') { $data = strip_tags($data); } else { if ($modifier_name == '%') { $divisor = isset($modifier_params[0]) ? $modifier_params[0] : 1; $data = $data % $divisor; } else { if ($modifier_name == 'empty') { $data = \Helper::isEmptyArray($data) ? "true" : ""; } else { if ($modifier_name == 'not_empty') { $data = !\Helper::isEmptyArray($data) ? "true" : ""; } else { if ($modifier_name == 'numeric') { $data = is_numeric($data) ? "true" : ""; } else { if ($modifier_name == 'repeat') { $multiplier = isset($modifier_params[0]) ? $modifier_params[0] : 1; $data = str_repeat($data, $multiplier); } else { if ($modifier_name == 'reverse') { $data = strrev($data); } else { if ($modifier_name == 'round') { $precision = isset($modifier_params[0]) ? (int) $modifier_params[0] : 0; $data = round((double) $data, $precision); } else { if ($modifier_name == 'floor') { $data = floor((double) $data); } else { if ($modifier_name == 'ceil') { $data = ceil((double) $data); } else { if ($modifier_name == '+') { if (isset($modifier_params[0])) { $number = $modifier_params[0]; $data = $data + $number; } } else { if ($modifier_name == '-') { if (isset($modifier_params[0])) { $number = $modifier_params[0]; $data = $data - $number; } } else { if ($modifier_name == '*') { if (isset($modifier_params[0])) { $number = $modifier_params[0]; $data = $data * $number; } } else { if ($modifier_name == '/') { if (isset($modifier_params[0])) { $number = $modifier_params[0]; $data = $data / $number; } } else { if ($modifier_name == '^') { if (isset($modifier_params[0])) { $exp = $modifier_params[0]; $data = pow($data, $exp); } } else { if ($modifier_name == 'sqrt') { $data = sqrt($data); } else { if ($modifier_name == 'abs') { $data = abs($data); } else { if ($modifier_name == 'log') { $base = isset($modifier_params[0]) ? (int) $modifier_params[0] : M_E; $data = log($data, $base); } else { if ($modifier_name == 'log10') { $data = log10($data); } else { if ($modifier_name == 'deg2rad') { $data = deg2rad($data); } else { if ($modifier_name == 'rad2deg') { $data = rad2deg($data); } else { if ($modifier_name == 'sin') { $data = sin($data); } else { if ($modifier_name == 'asin') { $data = asin($data); } else { if ($modifier_name == 'cos') { $data = cos($data); } else { if ($modifier_name == 'acos') { $data = acos($data); } else { if ($modifier_name == 'tan') { $data = tan($data); } else { if ($modifier_name == 'atan') { $data = atan($data); } else { if ($modifier_name == 'decbin') { $data = decbin($data); } else { if ($modifier_name == 'dechex') { $data = dechex($data); } else { if ($modifier_name == 'decoct') { $data = decoct($data); } else { if ($modifier_name == 'hexdec') { $data = hexdec($data); } else { if ($modifier_name == 'octdec') { $data = octdec($data); } else { if ($modifier_name == 'bindec') { $data = bindec((string) $data); } else { if ($modifier_name == 'distance_in_mi_from') { if (!isset($modifier_params[0])) { return 'Unknown'; } if (!preg_match(\Pattern::COORDINATES, $data, $point_1_matches)) { return 'Unknown'; } if (!preg_match(\Pattern::COORDINATES, $modifier_params[0], $point_2_matches)) { return 'Unknown'; } $point_1 = array($point_1_matches[1], $point_1_matches[2]); $point_2 = array($point_2_matches[1], $point_2_matches[2]); $distance = \Math::getDistanceInKilometers($point_1, $point_2); $data = \Math::convertKilometersToMiles($distance); } else { if ($modifier_name == 'distance_in_km_from') { if (!isset($modifier_params[0])) { return 'Unknown'; } if (!preg_match(\Pattern::COORDINATES, $data, $point_1_matches)) { return 'Unknown'; } if (!preg_match(\Pattern::COORDINATES, $modifier_params[0], $point_2_matches)) { return 'Unknown'; } $point_1 = array($point_1_matches[1], $point_1_matches[2]); $point_2 = array($point_2_matches[1], $point_2_matches[2]); $data = \Math::getDistanceInKilometers($point_1, $point_2); } else { if ($modifier_name == 'smartypants') { $data = SmartyPants($data, 2); } else { if ($modifier_name == 'widont') { // thanks to Shaun Inman for inspriation here // http://www.shauninman.com/archive/2008/08/25/widont_2_1_1 // if there are content tags if (preg_match("/<\\/(?:p|li|h1|h2|h3|h4|h5|h6|figcaption)>/ism", $data)) { $data = preg_replace("/(?<!<[p|li|h1|h2|h3|h4|h5|h6|div|figcaption])([^\\s])[ \t]+([^\\s]+(?:<\\/(?:p|li|h1|h2|h3|h4|h5|h6|div|figcaption)>))\$/im", "\$1 \$2", rtrim($data)); // otherwise } else { $data = preg_replace("/([^\\s])\\s+([^\\s]+)\\s*\$/im", "\$1 \$2", rtrim($data)); } } elseif ($modifier_name == 'backspace') { if (!is_array($data) && isset($modifier_params[0]) && $modifier_params[0] > 0) { $data = substr($data, 0, -$modifier_params[0]); } } else { if ($modifier_name == 'truncate') { $length = 30; $hellip = "…"; if (sizeof($modifier_params) > 0) { $length = (int) $modifier_params[0]; } else { if (isset($modifier_params[1])) { $hellip = (int) $modifier_params[1]; } } if (strlen($data) > $length) { $data = substr($data, 0, $length) . $hellip; } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } return $data; }
/** * Convert kilometers to miles * * @param float $kilometers Kilometers to convert * @return float */ public static function convert_km_to_miles($kilometers) { Log::warn("Use of Statamic_Helper::convert_km_to_miles() is deprecated. Use Math::convertKilometersToMiles() instead.", "core", "Statamic_Helper"); return Math::convertKilometersToMiles($kilometers); }