예제 #1
0
    /**
     * Generate default loop output settings (former layout settings) for a View, based on chosen loop output style
     *
     * @param string $style Loop output style name, which must be one of the following values:
     *     - table
     *     - bootstrap-grid
     *     - table_of_fields
     *     - ordered_list
     *     - un_ordered_list
     *     - unformatted
     *     - empty (since 1.10): Ignores fields and renders just an empty <wpv-loop></wpv-loop>
     *
     * @param array $fields (
     *         Array of definitions of fields that will be present in the loop output. If an element is not present, empty
     *         string is used instead.
     *
     *         @type string $prefix Prefix, text before shortcode.
     *         @type string $shortcode The shortcode ('[shortcode]').
     *         @type string $suffix Text after shortcode.
     *         @type string $field_name Field name.
     *         @type string $header_name Header name.
     *         @type string $row_title Row title <TH>.
     *     )
     *
     * @param array $args(
     *         Additional arguments.
     *
     *         @type bool $include_field_names If the loop output style is table_of_fields, determines whether the rendered
     *             loop output will contain table header with field names. Optional. Default is true.
     *
     *         @type int $tab_column_count Number of columns for the bootstrap-grid style. Optional. Default is 1.
     *         @type int $bootstrap_column_count Number of columns for the table style. Optional. Default is 1.
     *         @type int $bootstrap_version Version of Bootstrap. Mandatory for bootstrap-grid style, irrelephant
     *             otherwise. Must be 2 or 3.
     *         @type bool $add_container Argument for bootstrap-grid style. If true, enclose rendered html in a
     *             container div. Optional. Default is false.
     *         @type bool $add_row_class Argument for bootstrap-grid style. If true, a "row" class will be added to
     *             elements representing rows. For Bootstrap 3 it is added anyway. Optional. Default is false.
     *         @type bool $render_individual_columns Argument for bootstrap-grid style. If true, a wpv-item shortcode
     *             will be rendered for each singular column. Optional. Default is false.
     *
     *         @type bool $render_only_wpv_loop If true, only the code that should be within "<!-- wpv-loop start -->" and
     *             "<!-- wpv-loop end -->" tags is rendered. Optional. Default is false.
     *
     *         @type bool $use_loop_template Determines whether a Content Template will be used for field shortcodes.
     *             If true, the content of the CT will be returned in the 'ct_content' element and the loop output will
     *             contain shortcodes referencing it. In such case the argument loop_template_title is mandatory. Optional.
     *             Default is false.
     *
     *         @type string $loop_template_title Title of the Content Template that should contain field shortcodes. Only
     *             relevant if use_loop_template is true, and in such case it is mandatory.
     *     )
     *
     * @return  null|array Null on error. Otherwise an array containing following elements:
     *     array(
     *         @type array loop_output_settings Loop Output settings for a View, as they should be stored in the database:
     *             array(
     *                 @type string $style
     *                 @type string $layout_meta_html
     *                 @type int $table_cols
     *                 @type int $bootstrap_grid_cols
     *                 @type string $bootstrap_grid_container '1' or ''
     *                 @type string $bootstrap_grid_row_class '1' or ''
     *                 @type string $bootstrap_grid_individual '1' or ''
     *                 @type string $include_field_names '1' or ''
     *                 @type array $fields
     *                 @type array $real_fields
     *             )
     *         @type string ct_content Content of the Content Template (see use_loop_template argument for more info) or
     *             an empty string.
     *     )
     *
     * @since 1.10
     */
    static function generate_loop_output( $style = 'empty', $fields = array(), $args = array() ) {

        // Default values for arguments
        $args = array_merge(
            array(
                'include_field_names' => true,
                'tab_column_count' => 1,
                'bootstrap_column_count' => 1,
                'bootstrap_version' => 'undefined',
                'add_container' => false,
                'add_row_class' => false,
                'render_individual_columns' => false,
                'use_loop_template' => false,
                'loop_template_title' => '',
                'render_only_wpv_loop' => false ),
            $args );

        // Avoid extract() and validate.
        $include_field_names = ( true == $args['include_field_names'] ) ? true : false;
        $tab_column_count = (int) $args['tab_column_count'];
        $bootstrap_column_count = (int) $args['bootstrap_column_count'];
        $add_container = ( true == $args['add_container'] ) ? true : false;
        $add_row_class = ( true == $args['add_row_class'] ) ? true : false;
        $render_individual_columns = ( true == $args['render_individual_columns'] ) ? true : false;
        $use_loop_template = ( true == $args['use_loop_template'] ) ? true : false;
        $loop_template_title = $args['loop_template_title']; // can be anything
        $render_only_wpv_loop = ( true == $args['render_only_wpv_loop'] ) ? true : false;

        // Disallow empty title if we're creating new CT
        if( ( true == $use_loop_template ) && empty( $loop_template_title ) ) {
            return null;
        }

        // Results
        $loop_output_settings = array(
            'style' => $style,  // this will be valid value, or we'll return null later
            'additional_js'	=> '' );

        // Ensure all field keys are present for all fields.
        $fields_normalized = array();
        $field_defaults = array(
            'prefix' => '',
            'shortcode' => '',
            'suffix' => '',
            'field_name' => '',
            'header_name' => '',
            'row_title' => '' );
        foreach( $fields as $field ) {
            $fields_normalized[] = wp_parse_args( $field, $field_defaults );
        }
        $fields = $fields_normalized;

        // Render layout HTML
        switch( $style ) {
            case 'table':
                $loop_output = WPV_View_Base::generate_table_layout( $fields, $args );
                break;
            case 'bootstrap-grid':
                $loop_output = WPV_View_Base::generate_bootstrap_grid_layout( $fields, $args );
                break;
            case 'table_of_fields':
                $loop_output = WPV_View_Base::generate_table_of_fields_layout( $fields, $args );
                break;
            case 'ordered_list':
                $loop_output = WPV_View_Base::generate_list_layout( $fields, $args, 'ol' );
                break;
            case 'un_ordered_list':
                $loop_output = WPV_View_Base::generate_list_layout( $fields, $args, 'ul' );
                break;
            case 'unformatted':
                $loop_output = WPV_View_Base::generate_unformatted_layout( $fields, $args );
                break;
            case 'empty':
                $loop_output = array(
                    'loop_template' => "\t\t<wpv-loop>\n\t\t</wpv-loop>\n",
                    'ct_content' => ''
                );
                break;
            default:
                // Invalid loop output style
                return null;
        }
        // If rendering has failed, we fail too.
        if( null == $loop_output ) {
            return null;
        }

        $layout_meta_html = $loop_output['loop_template'];

        if( ! $render_only_wpv_loop ) {
            // Render the whole layout_meta_html
            $layout_meta_html = sprintf(
                "[wpv-layout-start]\n"
                . "\t[wpv-items-found]\n"
                . "\t<!-- wpv-loop-start -->\n"
                . "%s"
                . "\t<!-- wpv-loop-end -->\n"
                . "\t[/wpv-items-found]\n"
                . "\t[wpv-no-items-found]\n"
                . "\t\t<strong>[wpml-string context=\"wpv-views\"]No items found[/wpml-string]</strong>\n"
                . "\t[/wpv-no-items-found]\n"
                . "[wpv-layout-end]\n",
                $layout_meta_html );
        }

        $loop_output_settings['layout_meta_html'] = $layout_meta_html;

        // Pass other layout settings in the same way as it was in wpv_update_layout_extra_callback().

        // Only one value makes sense, but both are always stored...
        $loop_output_settings['table_cols'] = $tab_column_count;
        $loop_output_settings['bootstrap_grid_cols']  = $bootstrap_column_count;

        // These are '1' for true or '' for false (not sure if e.g. 0 can be passed instead, better leave it as it was).
        $loop_output_settings['bootstrap_grid_container'] = $add_container ? '1' : '';
        $loop_output_settings['bootstrap_grid_row_class'] = $add_row_class ? '1' : '';
        $loop_output_settings['bootstrap_grid_individual'] = $render_individual_columns ? '1' : '';
        $loop_output_settings['include_field_names'] = $include_field_names ? '1' : '';

        /* The 'fields' element is originally constructed in wpv_layout_wizard_convert_settings() with a comment
         * saying just "Compatibility".
         *
         * TODO it would be nice to explain why is this needed (compatibility with what?). */
        $fields_compatible = array();
        $field_index = 0;
        foreach ( $fields as $field ) {
            $fields_compatible[ 'prefix_' . $field_index ] = '';

            $shortcode = stripslashes( $field['shortcode'] );

            if ( preg_match( '/\[types.*?field=\"(.*?)\"/', $shortcode, $matched ) ) {
                $fields_compatible[ 'name_' . $field_index ] = 'types-field';
                $fields_compatible[ 'types_field_name_' . $field_index ] = $matched[1];
                $fields_compatible[ 'types_field_data_' . $field_index ] = $shortcode;
            } else {
                $fields_compatible[ 'name_' . $field_index ] = trim( $shortcode, '[]');
                $fields_compatible[ 'types_field_name_' . $field_index ] = '';
                $fields_compatible[ 'types_field_data_' . $field_index ] = '';
            }

            $fields_compatible[ 'row_title_' . $field_index ] = $field['field_name'];
            $fields_compatible[ 'suffix_' . $field_index ] = '';

            ++$field_index;
        }
        $loop_output_settings['fields'] = $fields_compatible;

        // 'real_fields' will be an array of field shortcodes
        $field_shortcodes = array();
        foreach( $fields as $field ) {
            $field_shortcodes[] = stripslashes( $field['shortcode'] );
        }
        $loop_output_settings['real_fields'] = $field_shortcodes;

        // we'll be returning layout settings and content of a CT (optionally)
        $result = array(
            'loop_output_settings' => $loop_output_settings,
            'ct_content' => $loop_output['ct_content'] );

        return $result;
    }