List every contact including the ones in subcategories

by drilippi   Last Updated October 14, 2019 01:10 AM

I need a phone book with all people of the organization, but also need a separated phone book for each department. We have a "Main" category, with a few subcategories. I cannot duplicate the personal for each category, because there are many contacts. I know this solution is already built-in for com_content.

I made it through override but and still I cannot make the pagination work.

This is my /html/com_contact/category/newlayout.php . I highlighted where I added the new code and explained what I was trying to do.

    <?php
    /**
     * @package     Joomla.Site
     * @subpackage  com_contact
     *
     * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
     * @license     GNU General Public License version 2 or later; see LICENSE.txt
     */

    defined('_JEXEC') or die;


    ?>
    <div class="contact-category<?php echo $this->pageclass_sfx;?>">
    <?php if ($this->params->get('show_page_heading')) : ?>
    <h1>
        <?php echo $this->escape($this->params->get('page_heading')); ?>
    </h1>
    <?php endif; ?>
    <?php if ($this->params->get('show_category_title', 1)) : ?>
    <h2>
        <?php echo JHtml::_('content.prepare', $this->category->title, '', 'com_contact.category.title'); ?>
    </h2>
    <?php endif; ?>
    <?php if ($this->params->def('show_description', 1) || $this->params->def('show_description_image', 1)) : ?>
        <div class="category-desc">
        <?php if ($this->params->get('show_description_image') && $this->category->getParams()->get('image')) : ?>
            <img src="<?php echo $this->category->getParams()->get('image'); ?>"/>
        <?php endif; ?>
        <?php if ($this->category->description && $this->params->get('show_description')) : ?>
            <?php echo JHtml::_('content.prepare', $this->category->description, '', 'com_contact.category.description'); ?>
        <?php endif; ?>
        <div class="clr"></div>
        </div>
    <?php endif; ?>

    <?php
    // -------------added code - start -----------
    //categoria selecionada no com_menu
    $catidselect = $this->category->id;
    //pegar subcat ids
    $catID = JRequest::getVar('id');
    $categories = JCategories::getInstance('Contact');
    $cat = $categories->get($catID);
    $children = $cat->getChildren();

    //list all the Category n Subcategoris IDs
    $numeros_id = array();
    $numeros_id []= $catidselect;
    foreach ($children as $numero){
        $numeros_id[] = $numero->id;
    }

    // Get a db connection.
    $db = JFactory::getDbo();

    // Create a new query object.
    $query = $db->getQuery(true);

    // Select all records from the user profile table where key begins with "custom.".
    // Order it by the ordering field.
    $query->select('*');
    $query->from($db->quoteName('#__contact_details'));
    $j=1;
    $len = count($numeros_id);
    $query->where($db->quoteName('published') . ' =  1',  'AND' );
    //build the 'where' statement
    $catid_stings= ' ( ';
    foreach ($numeros_id as $num ){
        if($len === 1) {
            $catid_stings=$catid_stings.$db->quoteName('catid').' = '.$db->quote($num) ;
        } else if($len > 1 && $j < $len){
            $catid_stings=$catid_stings.' '.$db->quoteName('catid').' = '.$db->quote($num).' OR';
        } else if($len > 1 && $j === $len){
            $catid_stings=$catid_stings.' '.$db->quoteName('catid').' = '.$db->quote($num) ;
        }
        $j++;
    }
    $catid_stings = $catid_stings." )";
    $query->where($catid_stings);
    $query->order('name ASC');

    $db = $db->setQuery($query);
    //Trying to fix pagination
        //count all items in query
        $num_total = $db->loadObjectList();
        $this->pagination->total = count($num_total);
        //get limit from $this
        $llimit = $this->pagination->limit;
        //create number of total pages
        $this->pagination->pagesTotal = ceil(count($num_total)/$llimit);
        $this->pagination->pagesStop = ceil(count($num_total)/$llimit);
        //
        $pagCur = $this->pagination->pagesCurrent;
        //recalculate new limitstart
        $newLimitstart = ($llimit != 0 ? (($pagCur-1) * $llimit) : 0);
        $this->pagination->limitstart = $newLimitstart;

    //Query items for this page
    $db->setQuery($query, $newLimitstart, $llimit);


    // Load the results as a list of stdClass objects (see later for more options on retrieving data).
    $results = $db->loadObjectList();
    $this->items = $results;


    // -------------added code - end -----------
    ?>


    <?php echo $this->loadTemplate('items'); ?>



    </div>

And this is my /html/com_contact/category/newlayout_items.php . The only trouble here whas the slug, because I didn't know how to create, but I think I got around.

    <?php
    /**
     * @package     Joomla.Site
     * @subpackage  com_contact
     *
     * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
     * @license     GNU General Public License version 2 or later; see LICENSE.txt
     */

    defined('_JEXEC') or die;

    JHtml::_('behavior.framework');

    $listOrder = $this->escape($this->state->get('list.ordering'));
    $listDirn  = $this->escape($this->state->get('list.direction'));


    ?>
    <?php if (empty($this->items)) : ?>
        <p> <?php echo JText::_('COM_CONTACT_NO_CONTACTS'); ?> </p>
    <?php else : ?>

    <form action="<?php echo htmlspecialchars(JUri::getInstance()->toString()); ?>" method="post" name="adminForm" id="adminForm">

        <div class="table-responsive-lg">
            <table class="category table">
                <?php if ($this->params->get('show_headings')) : ?>
                <thead><tr>

                    <?php if ($this->params->get('show_image_heading')) : ?>
                    <th class="item-image">
                    </th>
                    <?php endif; ?>
                    <th class="item-title">
                        <?php echo JHtml::_('grid.sort', 'COM_CONTACT_CONTACT_EMAIL_NAME_LABEL', 'a.name', $listDirn, $listOrder); ?>
                    </th>
                    <th class="item-linha-de-pesquisa">
                            <?php echo JHtml::_('grid.sort', 'COM_CONTACT_ADDRESS', 'a.address', $listDirn, $listOrder); ?>
                    </th>
                    <?php if ($this->params->get('show_position_headings')) : ?>
                    <th class="item-position">
                        <?php echo JHtml::_('grid.sort', 'COM_CONTACT_POSITION', 'a.con_position', $listDirn, $listOrder); ?>
                    </th>
                    <?php endif; ?>
                    <?php if ($this->params->get('show_email_headings')) : ?>
                    <th class="item-email">
                        <?php echo JText::_('JGLOBAL_EMAIL'); ?>
                    </th>
                    <?php endif; ?>
                    <?php if ($this->params->get('show_telephone_headings')) : ?>
                    <th class="item-phone">
                        <?php echo JText::_('COM_CONTACT_TELEPHONE'); ?>
                    </th>
                    <?php endif; ?>

                    <?php if ($this->params->get('show_mobile_headings')) : ?>
                    <th class="item-phone">
                        <?php echo JText::_('COM_CONTACT_MOBILE'); ?>
                    </th>
                    <?php endif; ?>

                    <?php if ($this->params->get('show_fax_headings')) : ?>
                    <th class="item-phone">
                        <?php echo JText::_('COM_CONTACT_FAX'); ?>
                    </th>
                    <?php endif; ?>

                    <?php if ($this->params->get('show_suburb_headings')) : ?>
                    <th class="item-suburb">
                        <?php echo JHtml::_('grid.sort', 'COM_CONTACT_SUBURB', 'a.suburb', $listDirn, $listOrder); ?>
                    </th>
                    <?php endif; ?>

                    <?php if ($this->params->get('show_state_headings')) : ?>
                    <th class="item-state">
                        <?php echo JHtml::_('grid.sort', 'COM_CONTACT_STATE', 'a.state', $listDirn, $listOrder); ?>
                    </th>
                    <?php endif; ?>

                    <?php if ($this->params->get('show_country_headings')) : ?>
                    <th class="item-state">
                        <?php echo JHtml::_('grid.sort', 'COM_CONTACT_COUNTRY', 'a.country', $listDirn, $listOrder); ?>
                    </th>
                    <?php endif; ?>

                    </tr>
                </thead>
                <?php endif; ?>

                <tbody>
                    <?php foreach ($this->items as $i => $item) : ?>
                        <?php  //Make slug - dont know other way :(
                                $item->slug = $item->id.'-'.$item->alias ;?>
                        <?php if ($this->items[$i]->published == 0) : ?>
                            <tr class="system-unpublished cat-list-row<?php echo $i % 2; ?>">
                        <?php else: ?>
                            <tr class="cat-list-row<?php echo $i % 2; ?>" >
                        <?php endif; ?>

                            <?php if ($this->params->get('show_image_heading')) : ?>
                                <td class="item-image" >
                                    <?php if ($this->items[$i]->image) : ?>
                                        <?php echo JHtml::_('image', $this->items[$i]->image, JText::_('COM_CONTACT_IMAGE_DETAILS'), array('class' => 'contact-thumbnail img-thumbnail')); ?>
                                    <?php endif; ?>
                                </td>
                            <?php endif; ?>

                            <td class="item-title" data-label="<?php echo JText::_('COM_CONTACT_CONTACT_EMAIL_NAME_LABEL'); ?>" >
                                <a href="<?php echo JRoute::_(ContactHelperRoute::getContactRoute($item->slug, $item->catid)); ?>">
                                    <?php echo $item->name; ?></a>
                            </td>

                            <td class="item-linha-de-pesquisa" data-label="Linha de Pesquisa">
                                    <?php echo $item->address; ?>
                            </td>

                            <?php if ($this->params->get('show_position_headings')) : ?>
                                <td class="item-position" data-label="<?php echo JText::_('COM_CONTACT_POSITION'); ?>">
                                    <?php echo $item->con_position; ?>
                                </td>
                            <?php endif; ?>

                            <?php if ($this->params->get('show_email_headings')) : ?>
                                <td class="item-email" data-label="<?php echo JText::_('JGLOBAL_EMAIL'); ?>">
                                    <?php echo $item->email_to; ?>
                                </td>
                            <?php endif; ?>

                            <?php if ($this->params->get('show_telephone_headings')) : ?>
                                <td class="item-phone" data-label="<?php echo JText::_('COM_CONTACT_TELEPHONE'); ?>">
                                    <?php echo $item->telephone; ?>
                                </td>
                            <?php endif; ?>

                            <?php if ($this->params->get('show_mobile_headings')) : ?>
                                <td class="item-phone" data-label="<?php echo JText::_('COM_CONTACT_MOBILE'); ?>">
                                    <?php echo $item->mobile; ?>
                                </td>
                            <?php endif; ?>

                            <?php if ($this->params->get('show_fax_headings')) : ?>
                            <td class="item-phone" data-label="<?php echo JText::_('COM_CONTACT_FAX'); ?>">
                                <?php echo $item->fax; ?>
                            </td>
                            <?php endif; ?>

                            <?php if ($this->params->get('show_suburb_headings')) : ?>
                            <td class="item-suburb" data-label="<?php echo JText::_('COM_CONTACT_SUBURB'); ?>">
                                <?php echo $item->suburb; ?>
                            </td>
                            <?php endif; ?>

                            <?php if ($this->params->get('show_state_headings')) : ?>
                            <td class="item-state" data-label="<?php echo JText::_('COM_CONTACT_STATE'); ?>">
                                <?php echo $item->state; ?>
                            </td>
                            <?php endif; ?>

                            <?php if ($this->params->get('show_country_headings')) : ?>
                            <td class="item-state" data-label="<?php echo JText::_('COM_CONTACT_COUNTRY'); ?>">
                                <?php echo $item->country; ?>
                            </td>
                            <?php endif; ?>

                        </tr>
                    <?php endforeach; ?>

                </tbody>
            </table>
        </div>

    <?php //pagination doesn't work
    if ($this->params->get('show_pagination')) : ?>
        <div class="pagination">
            <?php if ($this->params->def('show_pagination_results', 1)) : ?>
            <p class="counter">
                <?php echo $this->pagination->getPagesCounter(); ?>
            </p>
            <?php endif; ?>
            <?php echo $this->pagination->getPagesLinks(); ?>
        </div>
        <?php endif; ?>
        <div>
            <input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
            <input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
        </div>
    </form>
    <?php endif; ?>

I don't know if there's a better way, it doesn't look like an elegant solution. And the pagination is making me so mad. I can't get $this->pagination->limitstart the "right" value.

Is there a better way to get all contacts (on the same list, not separated by category) or to fix this pagination?



Related Questions


Updated January 26, 2017 14:04 PM

Updated January 26, 2017 14:04 PM

Updated August 04, 2017 16:10 PM

Updated June 16, 2018 20:10 PM

Updated August 03, 2016 08:04 AM