Source for file newsfeed.php

Documentation is available at newsfeed.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_newsfeeds
  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('NewsfeedsHelper'JPATH_ADMINISTRATOR '/components/com_newsfeeds/helpers/newsfeeds.php');
  13.  
  14. /**
  15.  * Newsfeed model.
  16.  *
  17.  * @package     Joomla.Administrator
  18.  * @subpackage  com_newsfeeds
  19.  * @since       1.6
  20.  */
  21. {
  22.  
  23.     /**
  24.      * The type alias for this content type.
  25.      *
  26.      * @var      string 
  27.      * @since    3.2
  28.      */
  29.     public $typeAlias = 'com_newsfeeds.newsfeed';
  30.  
  31.     /**
  32.      * @var        string    The prefix to use with controller messages.
  33.      * @since   1.6
  34.      */
  35.     protected $text_prefix = 'COM_NEWSFEEDS';
  36.  
  37.     /**
  38.      * Batch copy items to a new category or current.
  39.      *
  40.      * @param   integer  $value     The new category.
  41.      * @param   array    $pks       An array of row IDs.
  42.      * @param   array    $contexts  An array of item contexts.
  43.      *
  44.      * @return  mixed  An array of new IDs on success, boolean false on failure.
  45.      *
  46.      * @since   11.1
  47.      */
  48.     protected function batchCopy($value$pks$contexts)
  49.     {
  50.         $categoryId = (int) $value;
  51.  
  52.         $i 0;
  53.  
  54.         if (!parent::checkCategoryId($categoryId))
  55.         {
  56.             return false;
  57.         }
  58.  
  59.         // Parent exists so we let's proceed
  60.         while (!empty($pks))
  61.         {
  62.             // Pop the first ID off the stack
  63.             $pk array_shift($pks);
  64.  
  65.             $this->table->reset();
  66.  
  67.             // Check that the row actually exists
  68.             if (!$this->table->load($pk))
  69.             {
  70.                 if ($error $this->table->getError())
  71.                 {
  72.                     // Fatal error
  73.                     $this->setError($error);
  74.  
  75.                     return false;
  76.                 }
  77.                 else
  78.                 {
  79.                     // Not fatal error
  80.                     $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND'$pk));
  81.                     continue;
  82.                 }
  83.             }
  84.  
  85.             // Alter the title & alias
  86.             $data $this->generateNewTitle($categoryId$this->table->alias$this->table->name);
  87.             $this->table->name $data['0'];
  88.             $this->table->alias $data['1'];
  89.  
  90.             // Reset the ID because we are making a copy
  91.             $this->table->id 0;
  92.  
  93.             // New category ID
  94.             $this->table->catid $categoryId;
  95.  
  96.             // TODO: Deal with ordering?
  97.             //$this->table->ordering    = 1;
  98.  
  99.             // Check the row.
  100.             if (!$this->table->check())
  101.             {
  102.                 $this->setError($this->table->getError());
  103.                 return false;
  104.             }
  105.  
  106.             parent::createTagsHelper($this->tagsObserver$this->type$pk$this->typeAlias$this->table);
  107.  
  108.             // Store the row.
  109.             if (!$this->table->store())
  110.             {
  111.                 $this->setError($this->table->getError());
  112.                 return false;
  113.             }
  114.  
  115.             // Get the new item ID
  116.             $newId $this->table->get('id');
  117.  
  118.             // Add the new ID to the array
  119.             $newIds[$i$newId;
  120.             $i++;
  121.         }
  122.  
  123.         // Clean the cache
  124.         $this->cleanCache();
  125.  
  126.         return $newIds;
  127.     }
  128.  
  129.     /**
  130.      * Method to test whether a record can be deleted.
  131.      *
  132.      * @param   object    record object.
  133.      * @return  boolean  True if allowed to delete the record. Defaults to the permission set in the component.
  134.      * @since   1.6
  135.      */
  136.     protected function canDelete($record)
  137.     {
  138.         if (!empty($record->id))
  139.         {
  140.             if ($record->published != -2)
  141.             {
  142.                 return;
  143.             }
  144.             $user JFactory::getUser();
  145.  
  146.             if (!empty($record->catid))
  147.             {
  148.                 return $user->authorise('core.delete''com_newsfeed.category.' . (int) $record->catid);
  149.             }
  150.             else
  151.             {
  152.                 return parent::canDelete($record);
  153.             }
  154.         }
  155.     }
  156.  
  157.     /**
  158.      * Method to test whether a record can have its state changed.
  159.      *
  160.      * @param   object    record object.
  161.      * @return  boolean  True if allowed to change the state of the record. Defaults to the permission set in the component.
  162.      * @since   1.6
  163.      */
  164.     protected function canEditState($record)
  165.     {
  166.         $user JFactory::getUser();
  167.  
  168.         if (!empty($record->catid))
  169.         {
  170.             return $user->authorise('core.edit.state''com_newsfeeds.category.' . (int) $record->catid);
  171.         }
  172.         else
  173.         {
  174.             return parent::canEditState($record);
  175.         }
  176.     }
  177.  
  178.     /**
  179.      * Returns a Table object, always creating it.
  180.      *
  181.      * @param   type      The table type to instantiate
  182.      * @param   string    A prefix for the table class name. Optional.
  183.      * @param   array     Configuration array for model. Optional.
  184.      * @return  JTable    A database object
  185.      */
  186.     public function getTable($type 'Newsfeed'$prefix 'NewsfeedsTable'$config array())
  187.     {
  188.         return JTable::getInstance($type$prefix$config);
  189.     }
  190.  
  191.     /**
  192.      * Method to get the record form.
  193.      *
  194.      * @param   array      $data        Data for the form.
  195.      * @param   boolean    $loadData    True if the form is to load its own data (default case), false if not.
  196.      * @return  JForm    A JForm object on success, false on failure
  197.      * @since   1.6
  198.      */
  199.     public function getForm($data array()$loadData true)
  200.     {
  201.         // Get the form.
  202.         $form $this->loadForm('com_newsfeeds.newsfeed''newsfeed'array('control' => 'jform''load_data' => $loadData));
  203.         if (empty($form))
  204.         {
  205.             return false;
  206.         }
  207.  
  208.         // Determine correct permissions to check.
  209.         if ($this->getState('newsfeed.id'))
  210.         {
  211.             // Existing record. Can only edit in selected categories.
  212.             $form->setFieldAttribute('catid''action''core.edit');
  213.         }
  214.         else
  215.         {
  216.             // New record. Can only create in selected categories.
  217.             $form->setFieldAttribute('catid''action''core.create');
  218.         }
  219.  
  220.         // Modify the form based on access controls.
  221.         if (!$this->canEditState((object) $data))
  222.         {
  223.             // Disable fields for display.
  224.             $form->setFieldAttribute('ordering''disabled''true');
  225.             $form->setFieldAttribute('published''disabled''true');
  226.             $form->setFieldAttribute('publish_up''disabled''true');
  227.             $form->setFieldAttribute('publish_down''disabled''true');
  228.  
  229.             // Disable fields while saving.
  230.             // The controller has already verified this is a record you can edit.
  231.             $form->setFieldAttribute('ordering''filter''unset');
  232.             $form->setFieldAttribute('published''filter''unset');
  233.             $form->setFieldAttribute('publish_up''filter''unset');
  234.             $form->setFieldAttribute('publish_down''filter''unset');
  235.         }
  236.  
  237.         return $form;
  238.     }
  239.  
  240.     /**
  241.      * Method to get the data that should be injected in the form.
  242.      *
  243.      * @return  mixed  The data for the form.
  244.      * @since   1.6
  245.      */
  246.     protected function loadFormData()
  247.     {
  248.         // Check the session for previously entered form data.
  249.         $data JFactory::getApplication()->getUserState('com_newsfeeds.edit.newsfeed.data'array());
  250.  
  251.         if (empty($data))
  252.         {
  253.             $data $this->getItem();
  254.  
  255.             // Prime some default values.
  256.             if ($this->getState('newsfeed.id'== 0)
  257.             {
  258.                 $app JFactory::getApplication();
  259.                 $data->set('catid'$app->input->get('catid'$app->getUserState('com_newsfeeds.newsfeeds.filter.category_id')'int'));
  260.             }
  261.         }
  262.  
  263.         $this->preprocessData('com_newsfeeds.newsfeed'$data);
  264.  
  265.         return $data;
  266.     }
  267.  
  268.     /**
  269.      * Method to save the form data.
  270.      *
  271.      * @param   array  The form data.
  272.      *
  273.      * @return  boolean  True on success.
  274.      * @since    3.0
  275.      */
  276.     public function save($data)
  277.     {
  278.         $app JFactory::getApplication();
  279.  
  280.         // Alter the title for save as copy
  281.         if ($app->input->get('task'== 'save2copy')
  282.         {
  283.             list($name$alias$this->generateNewTitle($data['catid']$data['alias']$data['name']);
  284.             $data['name'$name;
  285.             $data['alias'$alias;
  286.             $data['published'0;
  287.         }
  288.  
  289.         if (parent::save($data))
  290.         {
  291.  
  292.             $assoc JLanguageAssociations::isEnabled();
  293.             if ($assoc)
  294.             {
  295.                 $id = (int) $this->getState($this->getName('.id');
  296.                 $item $this->getItem($id);
  297.  
  298.                 // Adding self to the association
  299.                 $associations $data['associations'];
  300.  
  301.                 foreach ($associations as $tag => $id)
  302.                 {
  303.                     if (empty($id))
  304.                     {
  305.                         unset($associations[$tag]);
  306.                     }
  307.                 }
  308.  
  309.                 // Detecting all item menus
  310.                 $all_language $item->language == '*';
  311.  
  312.                 if ($all_language && !empty($associations))
  313.                 {
  314.                     JError::raiseNotice(403JText::_('COM_NEWSFEEDS_ERROR_ALL_LANGUAGE_ASSOCIATED'));
  315.                 }
  316.  
  317.                 $associations[$item->language$item->id;
  318.  
  319.                 // Deleting old association for these items
  320.                 $db JFactory::getDbo();
  321.                 $query $db->getQuery(true)
  322.                     ->delete('#__associations')
  323.                     ->where($db->quoteName('context'' = ' $db->quote('com_newsfeeds.item'))
  324.                     ->where($db->quoteName('id'' IN (' implode(','$associations')');
  325.                 $db->setQuery($query);
  326.                 $db->execute();
  327.  
  328.                 if ($error $db->getErrorMsg())
  329.                 {
  330.                     $this->setError($error);
  331.                     return false;
  332.                 }
  333.  
  334.                 if (!$all_language && count($associations))
  335.                 {
  336.                     // Adding new association for these items
  337.                     $key md5(json_encode($associations));
  338.                     $query->clear()
  339.                         ->insert('#__associations');
  340.  
  341.                     foreach ($associations as $id)
  342.                     {
  343.                         $query->values($id ',' $db->quote('com_newsfeeds.item'',' $db->quote($key));
  344.                     }
  345.  
  346.                     $db->setQuery($query);
  347.                     $db->execute();
  348.  
  349.                     if ($error $db->getErrorMsg())
  350.                     {
  351.                         $this->setError($error);
  352.                         return false;
  353.                     }
  354.                 }
  355.             }
  356.  
  357.             return true;
  358.         }
  359.  
  360.         return false;
  361.     }
  362.  
  363.     /**
  364.      * Method to get a single record.
  365.      *
  366.      * @param   integer    The id of the primary key.
  367.      *
  368.      * @return  mixed  Object on success, false on failure.
  369.      * @since   1.6
  370.      */
  371.     public function getItem($pk null)
  372.     {
  373.         if ($item parent::getItem($pk))
  374.         {
  375.             // Convert the params field to an array.
  376.             $registry new JRegistry;
  377.             $registry->loadString($item->metadata);
  378.             $item->metadata $registry->toArray();
  379.  
  380.             // Convert the images field to an array.
  381.             $registry new JRegistry;
  382.             $registry->loadString($item->images);
  383.             $item->images $registry->toArray();
  384.         }
  385.  
  386.         // Load associated newsfeeds items
  387.         $app JFactory::getApplication();
  388.         $assoc JLanguageAssociations::isEnabled();
  389.  
  390.         if ($assoc)
  391.         {
  392.             $item->associations array();
  393.  
  394.             if ($item->id != null)
  395.             {
  396.                 $associations JLanguageAssociations::getAssociations('com_newsfeeds''#__newsfeeds''com_newsfeeds.item'$item->id);
  397.  
  398.                 foreach ($associations as $tag => $association)
  399.                 {
  400.                     $item->associations[$tag$association->id;
  401.                 }
  402.             }
  403.         }
  404.  
  405.         if (!empty($item->id))
  406.         {
  407.             $item->tags new JHelperTags;
  408.             $item->tags->getTagIds($item->id'com_newsfeeds.newsfeed');
  409.             $item->metadata['tags'$item->tags;
  410.         }
  411.  
  412.         return $item;
  413.     }
  414.  
  415.     /**
  416.      * Prepare and sanitise the table prior to saving.
  417.      */
  418.     protected function prepareTable($table)
  419.     {
  420.         $date JFactory::getDate();
  421.         $user JFactory::getUser();
  422.  
  423.         $table->name htmlspecialchars_decode($table->nameENT_QUOTES);
  424.         $table->alias JApplication::stringURLSafe($table->alias);
  425.  
  426.         if (empty($table->alias))
  427.         {
  428.             $table->alias JApplication::stringURLSafe($table->name);
  429.         }
  430.  
  431.         if (empty($table->id))
  432.         {
  433.             // Set the values
  434.             $table->created $date->toSql();
  435.  
  436.             // Set ordering to the last item if not set
  437.             if (empty($table->ordering))
  438.             {
  439.                 $db JFactory::getDbo();
  440.                 $db->setQuery('SELECT MAX(ordering) FROM #__newsfeeds');
  441.                 $max $db->loadResult();
  442.  
  443.                 $table->ordering $max 1;
  444.             }
  445.         }
  446.         else
  447.         {
  448.             // Set the values
  449.             $table->modified $date->toSql();
  450.             $table->modified_by $user->get('id');
  451.         }
  452.  
  453.         // Increment the content version number.
  454.         $table->version++;
  455.     }
  456.  
  457.     /**
  458.      * Method to change the published state of one or more records.
  459.      *
  460.      * @param   array    $pks      A list of the primary keys to change.
  461.      * @param   integer  $value    The value of the published state.
  462.      *
  463.      * @return  boolean  True on success.
  464.      * @since   1.6
  465.      */
  466.     public function publish(&$pks$value 1)
  467.     {
  468.         $result parent::publish($pks$value);
  469.  
  470.         // Clean extra cache for newsfeeds
  471.         $this->cleanCache('feed_parser');
  472.  
  473.         return $result;
  474.     }
  475.  
  476.     /**
  477.      * A protected method to get a set of ordering conditions.
  478.      *
  479.      * @param   object    record object.
  480.      * @return  array  An array of conditions to add to add to ordering queries.
  481.      * @since   1.6
  482.      */
  483.     protected function getReorderConditions($table)
  484.     {
  485.         $condition array();
  486.         $condition['catid = ' . (int) $table->catid;
  487.         return $condition;
  488.     }
  489.  
  490.     protected function preprocessForm(JForm $form$data$group 'content')
  491.     {
  492.         // Association newsfeeds items
  493.         $app JFactory::getApplication();
  494.         $assoc JLanguageAssociations::isEnabled();
  495.         if ($assoc)
  496.         {
  497.             $languages JLanguageHelper::getLanguages('lang_code');
  498.  
  499.             // force to array (perhaps move to $this->loadFormData())
  500.             $data = (array) $data;
  501.  
  502.             $addform new SimpleXMLElement('<form />');
  503.             $fields $addform->addChild('fields');
  504.             $fields->addAttribute('name''associations');
  505.             $fieldset $fields->addChild('fieldset');
  506.             $fieldset->addAttribute('name''item_associations');
  507.             $fieldset->addAttribute('description''COM_NEWSFEEDS_ITEM_ASSOCIATIONS_FIELDSET_DESC');
  508.             $add false;
  509.             foreach ($languages as $tag => $language)
  510.             {
  511.                 if (empty($data['language']|| $tag != $data['language'])
  512.                 {
  513.                     $add true;
  514.                     $field $fieldset->addChild('field');
  515.                     $field->addAttribute('name'$tag);
  516.                     $field->addAttribute('type''modal_newsfeed');
  517.                     $field->addAttribute('language'$tag);
  518.                     $field->addAttribute('label'$language->title);
  519.                     $field->addAttribute('translate_label''false');
  520.                     $field->addAttribute('edit''true');
  521.                     $field->addAttribute('clear''true');
  522.                 }
  523.             }
  524.             if ($add)
  525.             {
  526.                 $form->load($addformfalse);
  527.             }
  528.         }
  529.  
  530.         parent::preprocessForm($form$data$group);
  531.     }
  532.  
  533.     /**
  534.      * Method to change the title & alias.
  535.      *
  536.      * @param   integer  $parent_id  The id of the parent.
  537.      * @param   string   $alias      The alias.
  538.      * @param   string   $title      The title.
  539.      *
  540.      * @return  array  Contains the modified title and alias.
  541.      *
  542.      * @since   3.1
  543.      */
  544.     protected function generateNewTitle($category_id$alias$name)
  545.     {
  546.         // Alter the title & alias
  547.         $table $this->getTable();
  548.         while ($table->load(array('alias' => $alias'catid' => $category_id)))
  549.         {
  550.             if ($name == $table->name)
  551.             {
  552.                 $name JString::increment($name);
  553.             }
  554.             $alias JString::increment($alias'dash');
  555.         }
  556.  
  557.         return array($name$alias);
  558.     }
  559. }

Documentation generated on Tue, 19 Nov 2013 15:09:26 +0100 by phpDocumentor 1.4.3