/** * Finds a given path on the server, adding in any ordering elements missing * * @param string $path Path to resolve * @return string */ public static function resolve($path) { $content_root = Config::getContentRoot(); $content_type = Config::getContentType(); if (strpos($path, "/") === 0) { $parts = explode("/", substr($path, 1)); } else { $parts = explode("/", $path); } $fixedpath = "/"; foreach ($parts as $part) { if (!File::exists(Path::assemble($content_root, $path . '.' . $content_type)) && !is_dir(Path::assemble($content_root, $part))) { // check folders $list = Statamic::get_content_tree($fixedpath, 1, 1, FALSE, TRUE, FALSE); foreach ($list as $item) { $t = basename($item['slug']); if (Slug::isNumeric($t)) { $nl = strlen(Slug::getOrderNumber($t)) + 1; if (strlen($part) >= strlen($item['slug']) - $nl && Pattern::endsWith($item['slug'], $part)) { $part = $item['slug']; break; } } else { if (Pattern::endsWith($item['slug'], $part)) { if (strlen($part) >= strlen($t)) { $part = $item['slug']; break; } } } } // check files $list = Statamic::get_file_list($fixedpath); foreach ($list as $key => $item) { if (Pattern::endsWith($key, $part)) { $t = basename($item); $offset = 0; if (Pattern::startsWith($key, '__')) { $offset = 2; } elseif (Pattern::startsWith($key, '_')) { $offset = 1; } if (Config::getEntryTimestamps() && Slug::isDateTime($t)) { if (strlen($part) >= strlen($key) - 16 - $offset) { $part = $key; break; } } elseif (Slug::isDate($t)) { if (strlen($part) >= strlen($key) - 12 - $offset) { $part = $key; break; } } elseif (Slug::isNumeric($t)) { $nl = strlen(Slug::getOrderNumber($key)) + 1; if (strlen($part) >= strlen($key) - $nl - $offset) { $part = $key; break; } } else { $t = basename($item); if (strlen($part) >= strlen($t) - $offset) { $part = $key; break; } } } } } if ($fixedpath != '/') { $fixedpath .= '/'; } $fixedpath .= $part; } return $fixedpath; }
$data['slug'] = substr($slug, 11); $data['original_datestamp'] = substr($slug, 0, 10); $data['original_timestamp'] = ""; if (!$new) { $data['datestamp'] = strtotime(substr($slug, 0, 10)); $data['full_slug'] = $folder . "/" . $data['original_slug']; $data['timestamp'] = "0000"; } } } elseif ($data['type'] == 'number') { if ($new) { $data['original_numeric'] = Statamic::get_next_numeric($folder); $data['numeric'] = Statamic::get_next_numeric($folder); $data['full_slug'] = $folder; } else { $numeric = Slug::getOrderNumber($slug); $data['slug'] = substr($slug, strlen($numeric) + 1); $data['original_slug'] = substr($slug, strlen($numeric) + 1); $data['numeric'] = $numeric; $data['original_numeric'] = $numeric; $data['full_slug'] = $folder . "/" . $data['original_slug']; } } } else { if ($new) { if ($fieldset) { $fs = Statamic_Fieldset::load($fieldset); $fields_data = $fs->get_data(); $data['fields'] = isset($fields_data['fields']) ? $fields_data['fields'] : array(); $data['type'] = 'none'; $data['fieldset'] = $fieldset;
public static function get_content_tree($directory = '/', $depth = 1, $max_depth = 5, $folders_only = false, $include_entries = false, $hide_hidden = true, $include_content = false, $site_root = false) { // $folders_only = true only page.md // folders_only = false includes any numbered or non-numbered page (excluding anything with a fields.yaml file) // if include_entries is true then any numbered files are included $content_root = Config::getContentRoot(); $content_type = Config::getContentType(); $site_root = $site_root ? $site_root : Config::getSiteRoot(); $current_url = Path::tidy($site_root . '/' . Request::getResourceURI()); $taxonomy_url = false; if (Taxonomy::isTaxonomyURL($current_url)) { list($taxonomy_type, $taxonomy_name) = Taxonomy::getCriteria($current_url); $taxonomy_url = self::remove_taxonomy_from_path($current_url, $taxonomy_type, $taxonomy_name); } $directory = '/' . $directory . '/'; #ensure proper slashing if ($directory != '/') { $base = Path::tidy("{$content_root}/{$directory}"); } elseif ($directory == '/') { $base = "{$content_root}"; } else { $base = "{$content_root}"; } $files = glob("{$base}/*"); $data = array(); if ($files) { foreach ($files as $path) { $current_name = basename($path); if (!Pattern::endsWith($current_name, '.yaml')) { // Hidden page that should be removed if ($hide_hidden && Pattern::startsWith($current_name, '_')) { continue; } $node = array(); $file = substr($path, strlen($base) + 1, strlen($path) - strlen($base) - strlen($content_type) - 2); if (is_dir($path)) { $folder = substr($path, strlen($base) + 1); $node['type'] = 'folder'; $node['slug'] = basename($folder); $node['title'] = ucwords(basename($folder)); $node['numeric'] = Slug::getOrderNumber($folder); $node['file_path'] = Path::tidy($site_root . '/' . $directory . '/' . $folder . '/page'); if (Slug::isNumeric($folder)) { $pos = strpos($folder, "."); if ($pos !== false) { $node['raw_url'] = Path::tidy(Path::clean($site_root . '/' . $directory . '/' . $folder)); $node['url'] = Path::clean($node['raw_url']); $node['title'] = ucwords(basename(substr($folder, $pos + 1))); } else { $node['title'] = ucwords(basename($folder)); $node['raw_url'] = Path::tidy($site_root . '/' . $directory . '/' . $folder); $node['url'] = Path::clean($node['raw_url']); } } else { $node['title'] = ucwords(basename($folder)); $node['raw_url'] = Path::tidy($site_root . '/' . $directory . '/' . $folder); $node['url'] = Path::clean($node['raw_url']); } $node['depth'] = $depth; $node['children'] = $depth < $max_depth ? self::get_content_tree($directory . $folder . '/', $depth + 1, $max_depth, $folders_only, $include_entries, $hide_hidden, $include_content, $site_root) : null; $node['is_current'] = $node['raw_url'] == $current_url || $node['url'] == $current_url ? true : false; $node['is_parent'] = false; if ($node['url'] == URL::popLastSegment($current_url) || $taxonomy_url && $node['url'] == $taxonomy_url) { $node['is_parent'] = true; } $node['has_children'] = $node['children'] ? true : false; // has entries? if (File::exists(Path::tidy($path . "/fields.yaml"))) { $node['has_entries'] = true; } else { $node['has_entries'] = false; } $meta = self::get_content_meta("page", Path::tidy($directory . "/" . $folder), false, true); //$meta = self::get_content_meta("page", Statamic_Helper::reduce_double_slashes($directory."/".$folder)); if (isset($meta['title'])) { $node['title'] = $meta['title']; } if (isset($meta['last_modified'])) { $node['last_modified'] = $meta['last_modified']; } if ($hide_hidden === true && (isset($meta['status']) && ($meta['status'] == 'hidden' || $meta['status'] == 'draft'))) { // placeholder condition } else { $data[] = $include_content ? array_merge($meta, $node) : $node; // print_r($data); } } else { if (Pattern::endsWith($path, $content_type)) { if ($folders_only == false) { if ($file == 'page' || $file == 'feed' || $file == '404') { // $node['url'] = $directory; // $node['title'] = basename($directory); // $meta = self::get_content_meta('page', substr($directory, 1)); // $node['depth'] = $depth; } else { $include = true; // date based is never included if (Config::getEntryTimestamps() && Slug::isDateTime(basename($path))) { $include = false; } elseif (Slug::isDate(basename($path))) { $include = false; } elseif (Slug::isNumeric(basename($path))) { if ($include_entries == false) { if (File::exists(Path::tidy(dirname($path) . "/fields.yaml"))) { $include = false; } } } if ($include) { $node['type'] = 'file'; $node['raw_url'] = Path::tidy($directory) . basename($path); $pretty_url = Path::clean($node['raw_url']); $node['url'] = substr($pretty_url, 0, -1 * (strlen($content_type) + 1)); $node['is_current'] = $node['url'] == $current_url || $node['url'] == $current_url ? true : false; $node['slug'] = substr(basename($path), 0, -1 * (strlen($content_type) + 1)); $meta = self::get_content_meta(substr(basename($path), 0, -1 * (strlen($content_type) + 1)), substr($directory, 1), false, true); //$node['meta'] = $meta; if (isset($meta['title'])) { $node['title'] = $meta['title']; } $node['depth'] = $depth; if ($hide_hidden === true && (isset($meta['status']) && ($meta['status'] == 'hidden' || $meta['status'] == 'draft'))) { } else { $data[] = $include_content ? array_merge($meta, $node) : $node; } } } } } } } } } return $data; }
/** * get_numeric * Gets the numeric value of the $numeric_slug * * @deprecated Use Slug::getOrderNumber() instead * * @param string $numeric_slug Numeric slug to inspect * @return integer */ public static function get_numeric($numeric_slug) { Log::warn("Use of Statamic_Helper::get_datetimestamp() is deprecated. Use Slug::getOrderNumber() instead.", "core", "Statamic_Helper"); return Slug::getOrderNumber($numeric_slug); }