Exemple #1
0
/** @since version 0.7.1.0 */
function _connections_list($atts, $content = null)
{
    global $wpdb, $connections, $current_user;
    $form = new cnFormObjects();
    $convert = new cnFormatting();
    $format =& $convert;
    $template = new stdClass();
    $atts = shortcode_atts(array('id' => NULL, 'category' => NULL, 'exclude_category' => NULL, 'category_name' => NULL, 'wp_current_category' => 'false', 'allow_public_override' => 'false', 'private_override' => 'false', 'show_alphaindex' => 'false', 'repeat_alphaindex' => 'false', 'show_alphahead' => 'false', 'list_type' => NULL, 'limit' => NULL, 'offset' => NULL, 'order_by' => NULL, 'group_name' => NULL, 'last_name' => NULL, 'title' => NULL, 'organization' => NULL, 'department' => NULL, 'city' => NULL, 'state' => NULL, 'zip_code' => NULL, 'country' => NULL, 'template' => NULL, 'template_name' => NULL), $atts);
    /*
     * Convert some of the $atts values in the array to boolean.
     */
    $convert->toBoolean(&$atts['allow_public_override']);
    $convert->toBoolean(&$atts['private_override']);
    $convert->toBoolean(&$atts['show_alphaindex']);
    $convert->toBoolean(&$atts['repeat_alphaindex']);
    $convert->toBoolean(&$atts['show_alphahead']);
    $convert->toBoolean(&$atts['wp_current_category']);
    $permittedListTypes = array('individual', 'organization', 'family', 'connection_group');
    // Convert the supplied entry types $atts['list_type'] to an array.
    if (!empty($atts['list_type'])) {
        // Trim the space characters if present.
        $atts['list_type'] = str_replace(' ', '', $atts['list_type']);
        // Convert to array.
        $atts['list_type'] = explode(',', $atts['list_type']);
    }
    // Set the template type to the first in the entry type from the supplied if multiple list types are provided.
    if (!empty($atts['list_type']) && (bool) array_intersect((array) $atts['list_type'], $permittedListTypes)) {
        $templateType = $atts['list_type'][0];
    }
    /*
     * As of version 0.7.0.5 the $atts['template_name'] is deprecated.
     */
    if (isset($atts['template_name'])) {
        // First check to see if the template is in the custom template folder.
        if (is_dir(CN_CUSTOM_TEMPLATE_PATH) && is_readable(CN_CUSTOM_TEMPLATE_PATH)) {
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH . '/' . $atts['template_name'] . '.php')) {
                $template->file = CN_CUSTOM_TEMPLATE_PATH . '/' . $atts['template_name'] . '.php';
            }
        }
        // If the template is not in the custom template folder, check for it in the default template folder.
        if (!isset($template->file)) {
            if (file_exists(CN_BASE_PATH . '/templates/' . $atts['template_name'] . '.php')) {
                $template->file = CN_BASE_PATH . '/templates/' . $atts['template_name'] . '.php';
            }
        }
    } else {
        $template = new cnTemplate();
        // Change the list type to family from connection_group to maintain compatibility with versions .0.7.0.4 and earlier.
        if ($atts['list_type'] === 'connection_group') {
            $atts['list_type'] = 'family';
        }
        /*
         * $atts['template'] can be either a string or an object. It is a string when set
         * with the shortcode attribute. If it is a string, the template will be loaded
         * via the cnTemplate class.
         * 
         * If the attribute is not set, it will be the object returned from the
         * cnOptions::getActiveTemplate() method which stores the default template
         * per list style.
         */
        if (isset($atts['template']) && !is_object($atts['template'])) {
            $template->load($atts['template']);
            $template->includeFunctions();
        } else {
            if (empty($templateType)) {
                $templateType = 'all';
            }
            // If no list type was specified, set the default ALL template.
            $template->init($connections->options->getActiveTemplate($templateType));
            $template->includeFunctions();
        }
    }
    // If no template is found, return an error message.
    if (!isset($template->file)) {
        return '<p style="color:red; font-weight:bold; text-align:center;">ERROR: Template "' . $atts['template_name'] . $atts['template'] . '" not found.</p>';
    }
    $atts = apply_filters('cn_list_atts', $atts);
    $results = $connections->retrieve->entries($atts);
    //print_r($connections->lastQuery);
    if (!empty($results)) {
        $results = apply_filters('cn_list_results', $results);
    }
    // Prints the template's CSS file.
    if (method_exists($template, 'printCSS')) {
        $out .= $template->printCSS();
    }
    // Prints the javascript tag in the footer if $template->js path is set
    if (method_exists($template, 'printJS')) {
        $template->printJS();
    }
    $out = apply_filters('cn_list_before', $out, $results);
    // If there are no results no need to proceed and output message.
    if (empty($results)) {
        $noResultMessage = 'No results';
        $noResultMessage = apply_filters('cn_no_result_message', $noResultMessage);
        return $out . '<p class="cn-no-results">' . $noResultMessage . '</p>';
    }
    $out .= '<a name="connections-list-head" style="float: left;"></a>' . "\n";
    /*
     * The alpha index is only displayed if set set to true and not set to repeat using the shortcode attributes.
     * If a alpha index is set to repeat, that is handled down separately.
     */
    if ($atts['show_alphaindex'] && !$atts['repeat_alphaindex']) {
        $index = "<div class='cn-alphaindex' style='text-align:right;font-size:larger;font-weight:bold'>" . $form->buildAlphaIndex() . "</div>";
        $out .= apply_filters('cn_list_index', $index, $results);
    }
    $out .= '<div class="connections-list">' . "\n";
    foreach ((array) $results as $row) {
        //$entry = new cnOutput($row);
        $entry = new cnvCard($row);
        //$vCard = new cnvCard($row);
        $vCard =& $entry;
        if (isset($continue)) {
            unset($continue);
        }
        if (isset($cities)) {
            unset($cities);
        }
        if (isset($states)) {
            unset($states);
        }
        if (isset($zipcodes)) {
            unset($zipcodes);
        }
        if (isset($countries)) {
            unset($countries);
        }
        if (isset($setAnchor)) {
            unset($setAnchor);
        }
        /*
         * Check to make sure there is data stored in the address array.
         * Cycle thru each address, building separate arrays for city, state, zip and country.
         */
        if ($entry->getAddresses()) {
            foreach ($entry->getAddresses() as $address) {
                if ($address->city != NULL) {
                    $cities[] = $address->city;
                }
                if ($address->state != NULL) {
                    $states[] = $address->state;
                }
                if ($address->zipcode != NULL) {
                    $zipcodes[] = $address->zipcode;
                }
                if ($address->country != NULL) {
                    $countries[] = $address->country;
                }
            }
        }
        /*
         * Filter out the entries that are wanted based on the
         * filter attributes that may have been used in the shortcode.
         * 
         * NOTE: The '@' operator is used to suppress PHP generated errors. This is done
         * because not every entry will have addresses to populate the arrays created above.
         * 
         * NOTE: Since the entry class returns all fields escaped, the shortcode filter
         * attribute needs to be escaped as well so the comparason between the two functions
         * as expected.
         */
        $atts['group_name'] = esc_attr($atts['group_name']);
        $atts['last_name'] = esc_attr($atts['last_name']);
        $atts['title'] = esc_attr($atts['title']);
        $atts['organization'] = esc_attr($atts['organization']);
        $atts['department'] = esc_attr($atts['department']);
        if ($entry->getFamilyName() != $atts['group_name'] && $atts['group_name'] != null) {
            $continue = true;
        }
        if ($entry->getLastName() != $atts['last_name'] && $atts['last_name'] != null) {
            $continue = true;
        }
        if ($entry->getTitle() != $atts['title'] && $atts['title'] != null) {
            $continue = true;
        }
        if ($entry->getOrganization() != $atts['organization'] && $atts['organization'] != null) {
            $continue = true;
        }
        if ($entry->getDepartment() != $atts['department'] && $atts['department'] != null) {
            $continue = true;
        }
        if (@(!in_array($atts['city'], $cities)) && $atts['city'] != null) {
            $continue = true;
        }
        if (@(!in_array($atts['state'], $states)) && $atts['state'] != null) {
            $continue = true;
        }
        if (@(!in_array($atts['zip_code'], $zipcodes)) && $atts['zip_code'] != null) {
            $continue = true;
        }
        if (@(!in_array($atts['country'], $countries)) && $atts['country'] != null) {
            $continue = true;
        }
        /*
         * If any of the above filters returned true, the script will continue to the next entry.
         */
        if ($continue == true) {
            continue;
        }
        /*
         * Checks the first letter of the last name to see if it is the next
         * letter in the alpha array and sets the anchor.
         * 
         * If the alpha index is set to repeat it will append to the anchor.
         * 
         * If the alpha head set to true it will append the alpha head to the anchor.
         */
        $currentLetter = strtoupper(mb_substr($entry->getSortColumn(), 0, 1));
        if ($currentLetter != $previousLetter && $atts['id'] == null) {
            if ($atts['show_alphaindex']) {
                $setAnchor = '<a class="cn-index-head" name="' . $currentLetter . '"></a>';
            }
            if ($atts['show_alphaindex'] && $atts['repeat_alphaindex']) {
                $setAnchor .= "<div class='cn-alphaindex' style='text-align:right;font-size:larger;font-weight:bold'>" . $form->buildAlphaIndex() . "</div>";
            }
            if ($atts['show_alphahead']) {
                $setAnchor .= '<h4 class="cn-alphahead">' . $currentLetter . '</h4>';
            }
            $previousLetter = $currentLetter;
        } else {
            $setAnchor = null;
        }
        /*
         * The anchor and/or the alpha head is displayed if set to true using the shortcode attributes.
         */
        if ($atts['show_alphaindex'] || $atts['show_alphahead']) {
            $out .= $setAnchor;
        }
        $alternate == '' ? $alternate = '-alternate' : ($alternate = '');
        $out .= '<div class="cn-list-row' . $alternate . ' vcard ' . $template->slug . ' ' . $entry->getCategoryClass(TRUE) . '">' . "\n";
        $out = apply_filters('cn_entry_before', $out, $entry);
        ob_start();
        include $template->file;
        $out .= ob_get_contents();
        ob_end_clean();
        $out = apply_filters('cn_entry_after', $out, $entry);
        $out .= '</div>' . "\n";
    }
    $out .= '<div class="clear"></div>' . "\n";
    $out .= '</div>' . "\n";
    $out = apply_filters('cn_list_after', $out, $results);
    return $out;
}