Source for file contact.php

Documentation is available at contact.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_contact
  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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. JLoader::register('ContactHelper'JPATH_ADMINISTRATOR '/components/com_contact/helpers/contact.php');
  13.  
  14. /**
  15.  * Item Model for a Contact.
  16.  *
  17.  * @package     Joomla.Administrator
  18.  * @subpackage  com_contact
  19.  * @since       1.6
  20.  */
  21. {
  22.     /**
  23.      * The type alias for this content type.
  24.      *
  25.      * @var      string 
  26.      * @since    3.2
  27.      */
  28.     public $typeAlias = 'com_contact.contact';
  29.  
  30.     /**
  31.      * Method to perform batch operations on an item or a set of items.
  32.      *
  33.      * @param   array  $commands  An array of commands to perform.
  34.      * @param   array  $pks       An array of item ids.
  35.      * @param   array  $contexts  An array of item contexts.
  36.      *
  37.      * @return  boolean  Returns true on success, false on failure.
  38.      *
  39.      * @since   2.5
  40.      */
  41.     public function batch($commands$pks$contexts)
  42.     {
  43.         // Sanitize user ids.
  44.         $pks array_unique($pks);
  45.         JArrayHelper::toInteger($pks);
  46.  
  47.         // Remove any values of zero.
  48.         if (array_search(0$pkstrue))
  49.         {
  50.             unset($pks[array_search(0$pkstrue)]);
  51.         }
  52.  
  53.         if (empty($pks))
  54.         {
  55.             $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
  56.             return false;
  57.         }
  58.  
  59.         $done false;
  60.  
  61.         // Set some needed variables.
  62.         $this->user JFactory::getUser();
  63.         $this->table $this->getTable();
  64.         $this->tableClassName get_class($this->table);
  65.         $this->contentType new JUcmType;
  66.         $this->type $this->contentType->getTypeByTable($this->tableClassName);
  67.         $this->batchSet true;
  68.  
  69.         if ($this->type === false)
  70.         {
  71.             $type new JUcmType;
  72.             $this->type $type->getTypeByAlias($this->typeAlias);
  73.             $typeAlias $this->type->type_alias;
  74.         }
  75.         else
  76.         {
  77.             $typeAlias $this->type->type_alias;
  78.         }
  79.  
  80.         $this->tagsObserver $this->table->getObserverOfClass('JTableObserverTags');
  81.  
  82.         if (!empty($commands['category_id']))
  83.         {
  84.             $cmd JArrayHelper::getValue($commands'move_copy''c');
  85.  
  86.             if ($cmd == 'c')
  87.             {
  88.                 $result $this->batchCopy($commands['category_id']$pks$contexts);
  89.  
  90.                 if (is_array($result))
  91.                 {
  92.                     $pks $result;
  93.                 }
  94.                 else
  95.                 {
  96.                     return false;
  97.                 }
  98.             }
  99.             elseif ($cmd == 'm' && !$this->batchMove($commands['category_id']$pks$contexts))
  100.             {
  101.                 return false;
  102.             }
  103.  
  104.             $done true;
  105.         }
  106.  
  107.         if (!empty($commands['assetgroup_id']))
  108.         {
  109.             if (!$this->batchAccess($commands['assetgroup_id']$pks$contexts))
  110.             {
  111.                 return false;
  112.             }
  113.  
  114.             $done true;
  115.         }
  116.  
  117.         if (!empty($commands['language_id']))
  118.         {
  119.             if (!$this->batchLanguage($commands['language_id']$pks$contexts))
  120.             {
  121.                 return false;
  122.             }
  123.  
  124.             $done true;
  125.         }
  126.  
  127.         if (!empty($commands['tag']))
  128.         {
  129.             if (!$this->batchTag($commands['tag']$pks$contexts))
  130.             {
  131.                 return false;
  132.             }
  133.  
  134.             $done true;
  135.         }
  136.  
  137.         if (strlen($commands['user_id']0)
  138.         {
  139.             if (!$this->batchUser($commands['user_id']$pks$contexts))
  140.             {
  141.                 return false;
  142.             }
  143.  
  144.             $done true;
  145.         }
  146.  
  147.         if (!$done)
  148.         {
  149.             $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
  150.  
  151.             return false;
  152.         }
  153.  
  154.         // Clear the cache
  155.         $this->cleanCache();
  156.  
  157.         return true;
  158.     }
  159.  
  160.     /**
  161.      * Batch copy items to a new category or current.
  162.      *
  163.      * @param   integer  $value     The new category.
  164.      * @param   array    $pks       An array of row IDs.
  165.      * @param   array    $contexts  An array of item contexts.
  166.      *
  167.      * @return  mixed  An array of new IDs on success, boolean false on failure.
  168.      *
  169.      * @since   11.1
  170.      */
  171.     protected function batchCopy($value$pks$contexts)
  172.     {
  173.         $categoryId = (int) $value;
  174.  
  175.         $table $this->getTable();
  176.         $i 0;
  177.  
  178.         if (!parent::checkCategoryId($categoryId))
  179.         {
  180.             return false;
  181.         }
  182.  
  183.         // Parent exists so we proceed
  184.         while (!empty($pks))
  185.         {
  186.             // Pop the first ID off the stack
  187.             $pk array_shift($pks);
  188.  
  189.             $this->table->reset();
  190.  
  191.             // Check that the row actually exists
  192.             if (!$this->table->load($pk))
  193.             {
  194.                 if ($error $this->table->getError())
  195.                 {
  196.                     // Fatal error
  197.                     $this->setError($error);
  198.                     return false;
  199.                 }
  200.                 else
  201.                 {
  202.                     // Not fatal error
  203.                     $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND'$pk));
  204.                     continue;
  205.                 }
  206.             }
  207.  
  208.             // Alter the title & alias
  209.             $data $this->generateNewTitle($categoryId$this->table->alias$this->table->name);
  210.             $this->table->name $data['0'];
  211.             $this->table->alias $data['1'];
  212.  
  213.             // Reset the ID because we are making a copy
  214.             $this->table->id 0;
  215.  
  216.             // New category ID
  217.             $this->table->catid $categoryId;
  218.  
  219.             // TODO: Deal with ordering?
  220.             //$this->table->ordering    = 1;
  221.  
  222.             // Check the row.
  223.             if (!$this->table->check())
  224.             {
  225.                 $this->setError($this->table->getError());
  226.                 return false;
  227.             }
  228.  
  229.             parent::createTagsHelper($this->tagsObserver$this->type$pk$this->typeAlias$this->table);
  230.  
  231.             // Store the row.
  232.             if (!$this->table->store())
  233.             {
  234.                 $this->setError($this->table->getError());
  235.                 return false;
  236.             }
  237.  
  238.             // Get the new item ID
  239.             $newId $this->table->get('id');
  240.  
  241.             // Add the new ID to the array
  242.             $newIds[$i$newId;
  243.             $i++;
  244.         }
  245.  
  246.         // Clean the cache
  247.         $this->cleanCache();
  248.  
  249.         return $newIds;
  250.     }
  251.  
  252.     /**
  253.      * Batch change a linked user.
  254.      *
  255.      * @param   integer  $value     The new value matching a User ID.
  256.      * @param   array    $pks       An array of row IDs.
  257.      * @param   array    $contexts  An array of item contexts.
  258.      *
  259.      * @return  boolean  True if successful, false otherwise and internal error is set.
  260.      *
  261.      * @since   2.5
  262.      */
  263.     protected function batchUser($value$pks$contexts)
  264.     {
  265.  
  266.         foreach ($pks as $pk)
  267.         {
  268.             if ($this->user->authorise('core.edit'$contexts[$pk]))
  269.             {
  270.                 $this->table->reset();
  271.                 $this->table->load($pk);
  272.                 $this->table->user_id = (int) $value;
  273.  
  274.                 static::createTagsHelper($this->tagsObserver$this->type$pk$this->typeAlias$this->table);
  275.  
  276.                 if (!$this->table->store())
  277.                 {
  278.                     $this->this->setError($table->getError());
  279.  
  280.                     return false;
  281.                 }
  282.             }
  283.             else
  284.             {
  285.                 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
  286.                 return false;
  287.             }
  288.         }
  289.  
  290.         // Clean the cache
  291.         $this->cleanCache();
  292.  
  293.         return true;
  294.     }
  295.  
  296.     /**
  297.      * Method to test whether a record can be deleted.
  298.      *
  299.      * @param   object  $record  A record object.
  300.      *
  301.      * @return  boolean  True if allowed to delete the record. Defaults to the permission set in the component.
  302.      * @since   1.6
  303.      */
  304.     protected function canDelete($record)
  305.     {
  306.         if (!empty($record->id))
  307.         {
  308.             if ($record->published != -2)
  309.             {
  310.                 return;
  311.             }
  312.             $user JFactory::getUser();
  313.             return $user->authorise('core.delete''com_contact.category.' . (int) $record->catid);
  314.         }
  315.     }
  316.  
  317.     /**
  318.      * Method to test whether a record can have its state edited.
  319.      *
  320.      * @param   object    $record    A record object.
  321.      *
  322.      * @return  boolean  True if allowed to change the state of the record. Defaults to the permission set in the component.
  323.      * @since   1.6
  324.      */
  325.     protected function canEditState($record)
  326.     {
  327.         $user JFactory::getUser();
  328.  
  329.         // Check against the category.
  330.         if (!empty($record->catid))
  331.         {
  332.             return $user->authorise('core.edit.state''com_contact.category.' . (int) $record->catid);
  333.         }
  334.         // Default to component settings if category not known.
  335.         else
  336.         {
  337.             return parent::canEditState($record);
  338.         }
  339.     }
  340.  
  341.     /**
  342.      * Returns a Table object, always creating it
  343.      *
  344.      * @param   type      $type      The table type to instantiate
  345.      * @param   string    $prefix    A prefix for the table class name. Optional.
  346.      * @param   array     $config    Configuration array for model. Optional.
  347.      *
  348.      * @return  JTable    A database object
  349.      * @since   1.6
  350.      */
  351.     public function getTable($type 'Contact'$prefix 'ContactTable'$config array())
  352.     {
  353.         return JTable::getInstance($type$prefix$config);
  354.     }
  355.  
  356.     /**
  357.      * Method to get the row form.
  358.      *
  359.      * @param   array      $data        Data for the form.
  360.      * @param   boolean    $loadData    True if the form is to load its own data (default case), false if not.
  361.      *
  362.      * @return  mixed  A JForm object on success, false on failure
  363.      * @since   1.6
  364.      */
  365.     public function getForm($data array()$loadData true)
  366.     {
  367.         JForm::addFieldPath('JPATH_ADMINISTRATOR/components/com_users/models/fields');
  368.  
  369.         // Get the form.
  370.         $form $this->loadForm('com_contact.contact''contact'array('control' => 'jform''load_data' => $loadData));
  371.         if (empty($form))
  372.         {
  373.             return false;
  374.         }
  375.  
  376.         // Modify the form based on access controls.
  377.         if (!$this->canEditState((object) $data))
  378.         {
  379.             // Disable fields for display.
  380.             $form->setFieldAttribute('featured''disabled''true');
  381.             $form->setFieldAttribute('ordering''disabled''true');
  382.             $form->setFieldAttribute('published''disabled''true');
  383.  
  384.             // Disable fields while saving.
  385.             // The controller has already verified this is a record you can edit.
  386.             $form->setFieldAttribute('featured''filter''unset');
  387.             $form->setFieldAttribute('ordering''filter''unset');
  388.             $form->setFieldAttribute('published''filter''unset');
  389.         }
  390.  
  391.         return $form;
  392.     }
  393.  
  394.     /**
  395.      * Method to get a single record.
  396.      *
  397.      * @param   integer    $pk    The id of the primary key.
  398.      *
  399.      * @return  mixed  Object on success, false on failure.
  400.      * @since   1.6
  401.      */
  402.     public function getItem($pk null)
  403.     {
  404.         if ($item parent::getItem($pk))
  405.         {
  406.             // Convert the metadata field to an array.
  407.             $registry new JRegistry;
  408.             $registry->loadString($item->metadata);
  409.             $item->metadata $registry->toArray();
  410.         }
  411.  
  412.         // Load associated contact items
  413.         $app JFactory::getApplication();
  414.         $assoc JLanguageAssociations::isEnabled();
  415.  
  416.         if ($assoc)
  417.         {
  418.             $item->associations array();
  419.  
  420.             if ($item->id != null)
  421.             {
  422.                 $associations JLanguageAssociations::getAssociations('com_contact''#__contact_details''com_contact.item'$item->id);
  423.  
  424.                 foreach ($associations as $tag => $association)
  425.                 {
  426.                     $item->associations[$tag$association->id;
  427.                 }
  428.             }
  429.         }
  430.  
  431.         // Load item tags
  432.         if (!empty($item->id))
  433.         {
  434.             $item->tags new JHelperTags;
  435.             $item->tags->getTagIds($item->id'com_contact.contact');
  436.         }
  437.  
  438.         return $item;
  439.     }
  440.  
  441.     /**
  442.      * Method to get the data that should be injected in the form.
  443.      *
  444.      * @return  mixed  The data for the form.
  445.      * @since   1.6
  446.      */
  447.     protected function loadFormData()
  448.     {
  449.         // Check the session for previously entered form data.
  450.         $data JFactory::getApplication()->getUserState('com_contact.edit.contact.data'array());
  451.  
  452.         if (empty($data))
  453.         {
  454.             $data $this->getItem();
  455.  
  456.             // Prime some default values.
  457.             if ($this->getState('contact.id'== 0)
  458.             {
  459.                 $app JFactory::getApplication();
  460.                 $data->set('catid'$app->input->get('catid'$app->getUserState('com_contact.contacts.filter.category_id')'int'));
  461.             }
  462.         }
  463.  
  464.         $this->preprocessData('com_contact.contact'$data);
  465.  
  466.         return $data;
  467.     }
  468.  
  469.     /**
  470.      * Method to save the form data.
  471.      *
  472.      * @param   array  The form data.
  473.      *
  474.      * @return  boolean  True on success.
  475.      * @since    3.0
  476.      */
  477.     public function save($data)
  478.     {
  479.         $app JFactory::getApplication();
  480.  
  481.         // Alter the title for save as copy
  482.         if ($app->input->get('task'== 'save2copy')
  483.         {
  484.             list($name$alias$this->generateNewTitle($data['catid']$data['alias']$data['name']);
  485.             $data['name'$name;
  486.             $data['alias'$alias;
  487.             $data['published'0;
  488.         }
  489.  
  490.         $links array('linka''linkb''linkc''linkd''linke');
  491.  
  492.         foreach ($links as $link)
  493.         {
  494.             if ($data['params'][$link])
  495.             {
  496.                 $data['params'][$linkJStringPunycode::urlToPunycode($data['params'][$link]);
  497.             }
  498.         }
  499.  
  500.         if (parent::save($data))
  501.         {
  502.  
  503.             $assoc JLanguageAssociations::isEnabled();
  504.             if ($assoc)
  505.             {
  506.                 $id = (int) $this->getState($this->getName('.id');
  507.                 $item $this->getItem($id);
  508.  
  509.                 // Adding self to the association
  510.                 $associations $data['associations'];
  511.  
  512.                 foreach ($associations as $tag => $id)
  513.                 {
  514.                     if (empty($id))
  515.                     {
  516.                         unset($associations[$tag]);
  517.                     }
  518.                 }
  519.  
  520.                 // Detecting all item menus
  521.                 $all_language $item->language == '*';
  522.  
  523.                 if ($all_language && !empty($associations))
  524.                 {
  525.                     JError::raiseNotice(403JText::_('COM_CONTACT_ERROR_ALL_LANGUAGE_ASSOCIATED'));
  526.                 }
  527.  
  528.                 $associations[$item->language$item->id;
  529.  
  530.                 // Deleting old association for these items
  531.                 $db JFactory::getDbo();
  532.                 $query $db->getQuery(true)
  533.                     ->delete('#__associations')
  534.                     ->where('context=' $db->quote('com_contact.item'))
  535.                     ->where('id IN (' implode(','$associations')');
  536.                 $db->setQuery($query);
  537.                 $db->execute();
  538.  
  539.                 if ($error $db->getErrorMsg())
  540.                 {
  541.                     $this->setError($error);
  542.                     return false;
  543.                 }
  544.  
  545.                 if (!$all_language && count($associations))
  546.                 {
  547.                     // Adding new association for these items
  548.                     $key md5(json_encode($associations));
  549.                     $query->clear()
  550.                         ->insert('#__associations');
  551.  
  552.                     foreach ($associations as $id)
  553.                     {
  554.                         $query->values($id ',' $db->quote('com_contact.item'',' $db->quote($key));
  555.                     }
  556.  
  557.                     $db->setQuery($query);
  558.                     $db->execute();
  559.  
  560.                     if ($error $db->getErrorMsg())
  561.                     {
  562.                         $this->setError($error);
  563.                         return false;
  564.                     }
  565.                 }
  566.             }
  567.  
  568.             return true;
  569.         }
  570.  
  571.         return false;
  572.     }
  573.  
  574.     /**
  575.      * Prepare and sanitise the table prior to saving.
  576.      *
  577.      * @param   JTable    $table 
  578.      *
  579.      * @return  void 
  580.      * @since   1.6
  581.      */
  582.     protected function prepareTable($table)
  583.     {
  584.         $date JFactory::getDate();
  585.         $user JFactory::getUser();
  586.  
  587.         $table->name htmlspecialchars_decode($table->nameENT_QUOTES);
  588.         $table->alias JApplication::stringURLSafe($table->alias);
  589.  
  590.         if (empty($table->alias))
  591.         {
  592.             $table->alias JApplication::stringURLSafe($table->name);
  593.         }
  594.  
  595.         if (empty($table->id))
  596.         {
  597.             // Set the values
  598.             $table->created $date->toSql();
  599.  
  600.             // Set ordering to the last item if not set
  601.             if (empty($table->ordering))
  602.             {
  603.                 $db JFactory::getDbo();
  604.                 $query $db->getQuery(true);
  605.                 $query->select('MAX(ordering)');
  606.                 $query->from('#__contact_details');
  607.                 $db->setQuery($query);
  608.                 $max $db->loadResult();
  609.  
  610.                 $table->ordering $max 1;
  611.             }
  612.         }
  613.         else
  614.         {
  615.             // Set the values
  616.             $table->modified $date->toSql();
  617.             $table->modified_by $user->get('id');
  618.         }
  619.         // Increment the content version number.
  620.         $table->version++;
  621.     }
  622.  
  623.     /**
  624.      * A protected method to get a set of ordering conditions.
  625.      *
  626.      * @param   JTable    $table    A record object.
  627.      *
  628.      * @return  array  An array of conditions to add to add to ordering queries.
  629.      * @since   1.6
  630.      */
  631.     protected function getReorderConditions($table)
  632.     {
  633.         $condition array();
  634.         $condition['catid = ' . (int) $table->catid;
  635.  
  636.         return $condition;
  637.     }
  638.  
  639.     protected function preprocessForm(JForm $form$data$group 'content')
  640.     {
  641.         // Association content items
  642.         $app JFactory::getApplication();
  643.         $assoc JLanguageAssociations::isEnabled();
  644.         if ($assoc)
  645.         {
  646.             $languages JLanguageHelper::getLanguages('lang_code');
  647.  
  648.             // force to array (perhaps move to $this->loadFormData())
  649.             $data = (array) $data;
  650.  
  651.             $addform new SimpleXMLElement('<form />');
  652.             $fields $addform->addChild('fields');
  653.             $fields->addAttribute('name''associations');
  654.             $fieldset $fields->addChild('fieldset');
  655.             $fieldset->addAttribute('name''item_associations');
  656.             $fieldset->addAttribute('description''COM_CONTACT_ITEM_ASSOCIATIONS_FIELDSET_DESC');
  657.             $add false;
  658.             foreach ($languages as $tag => $language)
  659.             {
  660.                 if (empty($data['language']|| $tag != $data['language'])
  661.                 {
  662.                     $add true;
  663.                     $field $fieldset->addChild('field');
  664.                     $field->addAttribute('name'$tag);
  665.                     $field->addAttribute('type''modal_contact');
  666.                     $field->addAttribute('language'$tag);
  667.                     $field->addAttribute('label'$language->title);
  668.                     $field->addAttribute('translate_label''false');
  669.                     $field->addAttribute('edit''true');
  670.                     $field->addAttribute('clear''true');
  671.                 }
  672.             }
  673.             if ($add)
  674.             {
  675.                 $form->load($addformfalse);
  676.             }
  677.         }
  678.  
  679.         parent::preprocessForm($form$data$group);
  680.     }
  681.  
  682.     /**
  683.      * Method to toggle the featured setting of contacts.
  684.      *
  685.      * @param   array    $pks      The ids of the items to toggle.
  686.      * @param   integer  $value    The value to toggle to.
  687.      *
  688.      * @return  boolean  True on success.
  689.      * @since   1.6
  690.      */
  691.     public function featured($pks$value 0)
  692.     {
  693.         // Sanitize the ids.
  694.         $pks = (array) $pks;
  695.         JArrayHelper::toInteger($pks);
  696.  
  697.         if (empty($pks))
  698.         {
  699.             $this->setError(JText::_('COM_CONTACT_NO_ITEM_SELECTED'));
  700.             return false;
  701.         }
  702.  
  703.         $table $this->getTable();
  704.  
  705.         try
  706.         {
  707.             $db $this->getDbo();
  708.  
  709.             $query $db->getQuery(true);
  710.             $query->update('#__contact_details');
  711.             $query->set('featured = ' . (int) $value);
  712.             $query->where('id IN (' implode(','$pks')');
  713.             $db->setQuery($query);
  714.  
  715.             $db->execute();
  716.         }
  717.         catch (Exception $e)
  718.         {
  719.             $this->setError($e->getMessage());
  720.             return false;
  721.         }
  722.  
  723.         $table->reorder();
  724.  
  725.         // Clean component's cache
  726.         $this->cleanCache();
  727.  
  728.         return true;
  729.     }
  730.  
  731.     /**
  732.      * Method to change the title & alias.
  733.      *
  734.      * @param   integer  $parent_id  The id of the parent.
  735.      * @param   string   $alias      The alias.
  736.      * @param   string   $title      The title.
  737.      *
  738.      * @return  array  Contains the modified title and alias.
  739.      *
  740.      * @since   3.1
  741.      */
  742.     protected function generateNewTitle($category_id$alias$name)
  743.     {
  744.         // Alter the title & alias
  745.         $table $this->getTable();
  746.         while ($table->load(array('alias' => $alias'catid' => $category_id)))
  747.         {
  748.             if ($name == $table->name)
  749.             {
  750.                 $name JString::increment($name);
  751.             }
  752.             $alias JString::increment($alias'dash');
  753.         }
  754.  
  755.         return array($name$alias);
  756.     }
  757. }

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