SubsectionManager::__layoutSubsection PHP 메소드

__layoutSubsection() 개인적인 메소드

private __layoutSubsection ( $entries, $fields, $caption_template, $droptext_template, $mode, $flags = self::GETEVERYTHING )
    private function __layoutSubsection($entries, $fields, $caption_template, $droptext_template, $mode, $flags = self::GETEVERYTHING)
    {
        // Return early if there is nothing to do
        if (empty($entries) || !is_array($entries)) {
            return array('options' => array(), 'html' => '', 'preview' => '');
        }
        $token_fields = array();
        $token_names = array();
        $token_values = array();
        if (!empty($fields)) {
            // Get all field names used in templates
            if (preg_match_all('@{\\$([^}:]+)(:([^}]*))?}@U', $caption_template . $droptext_template, $m, PREG_PATTERN_ORDER) && is_array($m[0])) {
                // $m[0] is "context", i.e., {$field_name:default_value} and {$field_name:other_value}
                // $m[1] is "field name"
                // $m[2] is ":default value"
                // $m[3] is "default value"
                foreach ($m[0] as $index => $context) {
                    $token_fields[$m[1][$index]][$context] = true;
                    // Keep'em sorted, so we can use only a single str_replace call.
                    $token_names[$context] = $context;
                    $token_values[$context] = $m[3][$index];
                }
            }
            $preview = $flags & self::GETPREVIEW || $mode == 'preview';
            foreach ($fields as $index => $field) {
                $field_name = $field->get('element_name');
                $field_id = $field->get('id');
                $keep = false;
                if (isset($token_fields[$field_name])) {
                    $fields[$index]->ssm_tokens = $token_fields[$field_name];
                    $keep = true;
                }
                if ($preview && strpos($field->get('type'), 'upload') !== false) {
                    $fields[$index]->ssm_preview = $field_id;
                    $keep = true;
                }
                if (!$keep) {
                    unset($fields[$index]);
                }
            }
        }
        // Defaults
        $html = array();
        $options = array();
        $preview = $caption = '';
        $templates = array($caption_template, $droptext_template);
        foreach ($entries as $index => $entry) {
            // Generate layout
            $type = $preview = $href = $template = '';
            $values = $token_values;
            foreach ($fields as $field) {
                $field_id = $field->get('id');
                if (isset($field->ssm_tokens)) {
                    // Get value
                    if (is_callable(array($field, 'preparePlainTextValue'))) {
                        $field_value = $field->preparePlainTextValue($entry['data'][$field_id], $entry['id']);
                    } else {
                        $field_value = strip_tags($field->prepareTableValue($entry['data'][$field_id]));
                    }
                    // Caption & Drop text
                    if (!empty($field_value) && $field_value != __('None')) {
                        foreach ($field->ssm_tokens as $name => $dummy) {
                            $values[$name] = $field_value;
                        }
                    }
                }
                // Find upload fields
                if (isset($field->ssm_preview) && !empty($entry['data'][$field_id]['file'])) {
                    // Image
                    if (strpos($entry['data'][$field_id]['mimetype'], 'image') !== false) {
                        $type = 'image';
                        $preview = substr($field->get('destination'), 10) . '/' . $entry['data'][$field_id]['file'];
                        $href = URL . '/workspace' . $preview;
                    } else {
                        $type = 'file';
                        $preview = pathinfo($entry['data'][$field_id]['file'], PATHINFO_EXTENSION);
                        $href = $entry['data'][$field_id]['file'];
                    }
                }
            }
            list($caption, $droptext) = str_replace($token_names, $values, $templates);
            // Populate select options
            if ($flags & self::GETOPTIONS) {
                $options[$index] = array($entry['id'], false, strip_tags($caption));
            }
            // Generate HTML only when needed
            if (!($flags & self::GETHTML)) {
                continue;
            }
            // Create template
            if ($type == 'image') {
                $template = str_replace('{$url}', URL, self::$templates[$mode]['image']);
                $template = str_replace('{$preview}', $preview, $template);
                $template = str_replace('{$href}', $href, $template);
                $template = str_replace('{$value}', $entry['id'], $template);
                $template = str_replace('{$droptext}', htmlspecialchars($droptext), $template);
                $tmp = str_replace('{$caption}', $caption, $template);
            } elseif ($type == 'file') {
                $template = str_replace('{$type}', $preview, self::$templates[$mode]['file']);
                $template = str_replace('{$href}', $href, $template);
                $template = str_replace('{$value}', $entry['id'], $template);
                $template = str_replace('{$droptext}', htmlspecialchars($droptext), $template);
                $tmp = str_replace('{$caption}', $caption, $template);
            } else {
                $template = str_replace('{$preview}', $entry['id'], self::$templates[$mode]['text']);
                $template = str_replace('{$value}', $entry['id'], $template);
                $template = str_replace('{$droptext}', htmlspecialchars($droptext), $template);
                $tmp = str_replace('{$caption}', $caption, $template);
            }
            // Remove empty drop texts
            // Use "z" to make sure that empty captions/texts are sorted last.
            // Use ID to make sure that they are not removed from the list.
            // Use str_pad to make sure that same caption/text is sorted by ID in correct order (numerically).
            $html[strip_tags($tmp) . 'z' . str_pad($entry['id'], 6, '0', STR_PAD_LEFT)] = str_replace(' data-drop=""', '', $tmp);
        }
        // Set default result data
        $result = array('options' => $options, 'html' => '', 'preview' => '');
        // Generate preview
        if ($flags & self::GETPREVIEW) {
            // Create publish index preview for last item
            // This has to be checked right after loop above, so nothing will invalidate $type, $preview and $caption variables
            // that were last set inside the loop.
            $template = str_replace('{$caption}', $caption, self::$templates['index'][$type == 'image' ? 'image' : 'text']);
            if ($type == 'image') {
                $template = str_replace('{$preview}', $preview, $template);
                $template = str_replace('{$url}', URL, $template);
            }
            $result['preview'] = $template;
        }
        // Get HTML
        if ($flags & self::GETHTML) {
            // Sort items
            if ($flags & self::GETALLITEMS) {
                ksort($html);
            }
            $result['html'] = implode('', $html);
        }
        // Return result
        return $result;
    }