Source for file contacts.php

Documentation is available at contacts.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Finder.Contacts
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_BASE'or die;
  11.  
  12. require_once JPATH_ADMINISTRATOR '/components/com_finder/helpers/indexer/adapter.php';
  13.  
  14. /**
  15.  * Finder adapter for Joomla Contacts.
  16.  *
  17.  * @package     Joomla.Plugin
  18.  * @subpackage  Finder.Contacts
  19.  * @since       2.5
  20.  */
  21. {
  22.     /**
  23.      * The plugin identifier.
  24.      *
  25.      * @var    string 
  26.      * @since  2.5
  27.      */
  28.     protected $context = 'Contacts';
  29.  
  30.     /**
  31.      * The extension name.
  32.      *
  33.      * @var    string 
  34.      * @since  2.5
  35.      */
  36.     protected $extension = 'com_contact';
  37.  
  38.     /**
  39.      * The sublayout to use when rendering the results.
  40.      *
  41.      * @var    string 
  42.      * @since  2.5
  43.      */
  44.     protected $layout = 'contact';
  45.  
  46.     /**
  47.      * The type of content that the adapter indexes.
  48.      *
  49.      * @var    string 
  50.      * @since  2.5
  51.      */
  52.     protected $type_title = 'Contact';
  53.  
  54.     /**
  55.      * The table name.
  56.      *
  57.      * @var    string 
  58.      * @since  2.5
  59.      */
  60.     protected $table = '#__contact_details';
  61.  
  62.     /**
  63.      * The field the published state is stored in.
  64.      *
  65.      * @var    string 
  66.      * @since  2.5
  67.      */
  68.     protected $state_field = 'published';
  69.  
  70.     /**
  71.      * Load the language file on instantiation.
  72.      *
  73.      * @var    boolean 
  74.      * @since  3.1
  75.      */
  76.     protected $autoloadLanguage = true;
  77.  
  78.     /**
  79.      * Method to update the item link information when the item category is
  80.      * changed. This is fired when the item category is published or unpublished
  81.      * from the list view.
  82.      *
  83.      * @param   string   $extension  The extension whose category has been updated.
  84.      * @param   array    $pks        A list of primary key ids of the content that has changed state.
  85.      * @param   integer  $value      The value of the state that the content has been changed to.
  86.      *
  87.      * @return  void 
  88.      *
  89.      * @since   2.5
  90.      */
  91.     public function onFinderCategoryChangeState($extension$pks$value)
  92.     {
  93.         // Make sure we're handling com_contact categories
  94.         if ($extension == 'com_contact')
  95.         {
  96.             $this->categoryStateChange($pks$value);
  97.         }
  98.     }
  99.  
  100.     /**
  101.      * Method to remove the link information for items that have been deleted.
  102.      *
  103.      * This event will fire when contacts are deleted and when an indexed item is deleted.
  104.      *
  105.      * @param   string  $context  The context of the action being performed.
  106.      * @param   JTable  $table    A JTable object containing the record to be deleted
  107.      *
  108.      * @return  boolean  True on success.
  109.      *
  110.      * @since   2.5
  111.      * @throws  Exception on database error.
  112.      */
  113.     public function onFinderAfterDelete($context$table)
  114.     {
  115.         if ($context == 'com_contact.contact')
  116.         {
  117.             $id $table->id;
  118.         }
  119.         elseif ($context == 'com_finder.index')
  120.         {
  121.             $id $table->link_id;
  122.         }
  123.         else
  124.         {
  125.             return true;
  126.         }
  127.         // Remove the items.
  128.         return $this->remove($id);
  129.     }
  130.  
  131.     /**
  132.      * Method to determine if the access level of an item changed.
  133.      *
  134.      * @param   string   $context  The context of the content passed to the plugin.
  135.      * @param   JTable   $row      A JTable object
  136.      * @param   boolean  $isNew    If the content has just been created
  137.      *
  138.      * @return  boolean  True on success.
  139.      *
  140.      * @since   2.5
  141.      * @throws  Exception on database error.
  142.      */
  143.     public function onFinderAfterSave($context$row$isNew)
  144.     {
  145.         // We only want to handle contacts here
  146.         if ($context == 'com_contact.contact')
  147.         {
  148.             // Check if the access levels are different
  149.             if (!$isNew && $this->old_access != $row->access)
  150.             {
  151.                 // Process the change.
  152.                 $this->itemAccessChange($row);
  153.             }
  154.  
  155.             // Reindex the item
  156.             $this->reindex($row->id);
  157.         }
  158.  
  159.         // Check for access changes in the category
  160.         if ($context == 'com_categories.category')
  161.         {
  162.             // Check if the access levels are different
  163.             if (!$isNew && $this->old_cataccess != $row->access)
  164.             {
  165.                 $this->categoryAccessChange($row);
  166.             }
  167.         }
  168.  
  169.         return true;
  170.     }
  171.  
  172.     /**
  173.      * Method to reindex the link information for an item that has been saved.
  174.      * This event is fired before the data is actually saved so we are going
  175.      * to queue the item to be indexed later.
  176.      *
  177.      * @param   string   $context  The context of the content passed to the plugin.
  178.      * @param   JTable   $row      A JTable object
  179.      * @param   boolean  $isNew    If the content is just about to be created
  180.      *
  181.      * @return  boolean  True on success.
  182.      *
  183.      * @since   2.5
  184.      * @throws  Exception on database error.
  185.      */
  186.     public function onFinderBeforeSave($context$row$isNew)
  187.     {
  188.         // We only want to handle contacts here
  189.         if ($context == 'com_contact.contact')
  190.         {
  191.             // Query the database for the old access level if the item isn't new
  192.             if (!$isNew)
  193.             {
  194.                 $this->checkItemAccess($row);
  195.             }
  196.         }
  197.  
  198.         // Check for access levels from the category
  199.         if ($context == 'com_categories.category')
  200.         {
  201.             // Query the database for the old access level if the item isn't new
  202.             if (!$isNew)
  203.             {
  204.                 $this->checkCategoryAccess($row);
  205.             }
  206.         }
  207.  
  208.         return true;
  209.     }
  210.  
  211.     /**
  212.      * Method to update the link information for items that have been changed
  213.      * from outside the edit screen. This is fired when the item is published,
  214.      * unpublished, archived, or unarchived from the list view.
  215.      *
  216.      * @param   string   $context  The context for the content passed to the plugin.
  217.      * @param   array    $pks      A list of primary key ids of the content that has changed state.
  218.      * @param   integer  $value    The value of the state that the content has been changed to.
  219.      *
  220.      * @return  void 
  221.      *
  222.      * @since   2.5
  223.      */
  224.     public function onFinderChangeState($context$pks$value)
  225.     {
  226.         // We only want to handle contacts here
  227.         if ($context == 'com_contact.contact')
  228.         {
  229.             $this->itemStateChange($pks$value);
  230.         }
  231.  
  232.         // Handle when the plugin is disabled
  233.         if ($context == 'com_plugins.plugin' && $value === 0)
  234.         {
  235.             $this->pluginDisable($pks);
  236.         }
  237.     }
  238.  
  239.     /**
  240.      * Method to index an item. The item must be a FinderIndexerResult object.
  241.      *
  242.      * @param   FinderIndexerResult  $item    The item to index as an FinderIndexerResult object.
  243.      * @param   string               $format  The item format
  244.      *
  245.      * @return  void 
  246.      *
  247.      * @since   2.5
  248.      * @throws  Exception on database error.
  249.      */
  250.     protected function index(FinderIndexerResult $item$format 'html')
  251.     {
  252.         // Check if the extension is enabled
  253.         if (JComponentHelper::isEnabled($this->extension== false)
  254.         {
  255.             return;
  256.         }
  257.  
  258.         $item->setLanguage();
  259.  
  260.         // Initialize the item parameters.
  261.         $registry new JRegistry;
  262.         $registry->loadString($item->params);
  263.         $item->params $registry;
  264.  
  265.         // Build the necessary route and path information.
  266.         $item->url $this->getURL($item->id$this->extension$this->layout);
  267.         $item->route ContactHelperRoute::getContactRoute($item->slug$item->catslug);
  268.         $item->path FinderIndexerHelper::getContentPath($item->route);
  269.  
  270.         // Get the menu title if it exists.
  271.         $title $this->getItemMenuTitle($item->url);
  272.  
  273.         // Adjust the title if necessary.
  274.         if (!empty($title&& $this->params->get('use_menu_title'true))
  275.         {
  276.             $item->title $title;
  277.         }
  278.  
  279.         /*
  280.          * Add the meta-data processing instructions based on the contact
  281.          * configuration parameters.
  282.          */
  283.         // Handle the contact position.
  284.         if ($item->params->get('show_position'true))
  285.         {
  286.             $item->addInstruction(FinderIndexer::META_CONTEXT'position');
  287.         }
  288.  
  289.         // Handle the contact street address.
  290.         if ($item->params->get('show_street_address'true))
  291.         {
  292.             $item->addInstruction(FinderIndexer::META_CONTEXT'address');
  293.         }
  294.  
  295.         // Handle the contact city.
  296.         if ($item->params->get('show_suburb'true))
  297.         {
  298.             $item->addInstruction(FinderIndexer::META_CONTEXT'city');
  299.         }
  300.  
  301.         // Handle the contact region.
  302.         if ($item->params->get('show_state'true))
  303.         {
  304.             $item->addInstruction(FinderIndexer::META_CONTEXT'region');
  305.         }
  306.  
  307.         // Handle the contact country.
  308.         if ($item->params->get('show_country'true))
  309.         {
  310.             $item->addInstruction(FinderIndexer::META_CONTEXT'country');
  311.         }
  312.  
  313.         // Handle the contact zip code.
  314.         if ($item->params->get('show_postcode'true))
  315.         {
  316.             $item->addInstruction(FinderIndexer::META_CONTEXT'zip');
  317.         }
  318.  
  319.         // Handle the contact telephone number.
  320.         if ($item->params->get('show_telephone'true))
  321.         {
  322.             $item->addInstruction(FinderIndexer::META_CONTEXT'telephone');
  323.         }
  324.  
  325.         // Handle the contact fax number.
  326.         if ($item->params->get('show_fax'true))
  327.         {
  328.             $item->addInstruction(FinderIndexer::META_CONTEXT'fax');
  329.         }
  330.  
  331.         // Handle the contact e-mail address.
  332.         if ($item->params->get('show_email'true))
  333.         {
  334.             $item->addInstruction(FinderIndexer::META_CONTEXT'email');
  335.         }
  336.  
  337.         // Handle the contact mobile number.
  338.         if ($item->params->get('show_mobile'true))
  339.         {
  340.             $item->addInstruction(FinderIndexer::META_CONTEXT'mobile');
  341.         }
  342.  
  343.         // Handle the contact webpage.
  344.         if ($item->params->get('show_webpage'true))
  345.         {
  346.             $item->addInstruction(FinderIndexer::META_CONTEXT'webpage');
  347.         }
  348.  
  349.         // Handle the contact user name.
  350.         $item->addInstruction(FinderIndexer::META_CONTEXT'user');
  351.  
  352.         // Add the type taxonomy data.
  353.         $item->addTaxonomy('Type''Contact');
  354.  
  355.         // Add the category taxonomy data.
  356.         $item->addTaxonomy('Category'$item->category$item->cat_state$item->cat_access);
  357.  
  358.         // Add the language taxonomy data.
  359.         $item->addTaxonomy('Language'$item->language);
  360.  
  361.         // Add the region taxonomy data.
  362.         if (!empty($item->region&& $this->params->get('tax_add_region'true))
  363.         {
  364.             $item->addTaxonomy('Region'$item->region);
  365.         }
  366.  
  367.         // Add the country taxonomy data.
  368.         if (!empty($item->country&& $this->params->get('tax_add_country'true))
  369.         {
  370.             $item->addTaxonomy('Country'$item->country);
  371.         }
  372.  
  373.         // Get content extras.
  374.         FinderIndexerHelper::getContentExtras($item);
  375.  
  376.         // Index the item.
  377.         $this->indexer->index($item);
  378.     }
  379.  
  380.     /**
  381.      * Method to setup the indexer to be run.
  382.      *
  383.      * @return  boolean  True on success.
  384.      *
  385.      * @since   2.5
  386.      */
  387.     protected function setup()
  388.     {
  389.         // Load dependent classes.
  390.         require_once JPATH_SITE '/components/com_contact/helpers/route.php';
  391.  
  392.         // This is a hack to get around the lack of a route helper.
  393.         FinderIndexerHelper::getContentPath('index.php?option=com_contact');
  394.  
  395.         return true;
  396.     }
  397.  
  398.     /**
  399.      * Method to get the SQL query used to retrieve the list of content items.
  400.      *
  401.      * @param   mixed  $query  A JDatabaseQuery object or null.
  402.      *
  403.      * @return  JDatabaseQuery  A database object.
  404.      *
  405.      * @since   2.5
  406.      */
  407.     protected function getListQuery($query null)
  408.     {
  409.         $db JFactory::getDbo();
  410.         // Check if we can use the supplied SQL query.
  411.         $query $query instanceof JDatabaseQuery $query $db->getQuery(true)
  412.             ->select('a.id, a.name AS title, a.alias, a.con_position AS position, a.address, a.created AS start_date')
  413.             ->select('a.created_by_alias, a.modified, a.modified_by')
  414.             ->select('a.metakey, a.metadesc, a.metadata, a.language')
  415.             ->select('a.sortname1, a.sortname2, a.sortname3')
  416.             ->select('a.publish_up AS publish_start_date, a.publish_down AS publish_end_date')
  417.             ->select('a.suburb AS city, a.state AS region, a.country, a.postcode AS zip')
  418.             ->select('a.telephone, a.fax, a.misc AS summary, a.email_to AS email, a.mobile')
  419.             ->select('a.webpage, a.access, a.published AS state, a.ordering, a.params, a.catid')
  420.             ->select('c.title AS category, c.published AS cat_state, c.access AS cat_access');
  421.  
  422.         // Handle the alias CASE WHEN portion of the query
  423.         $case_when_item_alias ' CASE WHEN ';
  424.         $case_when_item_alias .= $query->charLength('a.alias''!=''0');
  425.         $case_when_item_alias .= ' THEN ';
  426.         $a_id $query->castAsChar('a.id');
  427.         $case_when_item_alias .= $query->concatenate(array($a_id'a.alias')':');
  428.         $case_when_item_alias .= ' ELSE ';
  429.         $case_when_item_alias .= $a_id.' END as slug';
  430.         $query->select($case_when_item_alias);
  431.  
  432.         $case_when_category_alias ' CASE WHEN ';
  433.         $case_when_category_alias .= $query->charLength('c.alias''!=''0');
  434.         $case_when_category_alias .= ' THEN ';
  435.         $c_id $query->castAsChar('c.id');
  436.         $case_when_category_alias .= $query->concatenate(array($c_id'c.alias')':');
  437.         $case_when_category_alias .= ' ELSE ';
  438.         $case_when_category_alias .= $c_id.' END as catslug';
  439.         $query->select($case_when_category_alias)
  440.  
  441.             ->select('u.name')
  442.             ->from('#__contact_details AS a')
  443.             ->join('LEFT''#__categories AS c ON c.id = a.catid')
  444.             ->join('LEFT''#__users AS u ON u.id = a.user_id');
  445.  
  446.         return $query;
  447.     }
  448. }

Documentation generated on Tue, 19 Nov 2013 14:56:47 +0100 by phpDocumentor 1.4.3