PageRenderTime 15ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_weblinks/models/weblink.php

https://bitbucket.org/eternaware/joomus
PHP | 236 lines | 119 code | 26 blank | 91 comment | 19 complexity | 057a2f8fd0fc4dcb61424726ef45c698 MD5 | raw file
Possible License(s): LGPL-2.1
  1<?php
  2/**
  3 * @package     Joomla.Administrator
  4 * @subpackage  com_weblinks
  5 *
  6 * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8 */
  9
 10defined('_JEXEC') or die;
 11
 12/**
 13 * Weblinks model.
 14 *
 15 * @package     Joomla.Administrator
 16 * @subpackage  com_weblinks
 17 * @since       1.5
 18 */
 19class WeblinksModelWeblink extends JModelAdmin
 20{
 21	/**
 22	 * @var		string	The prefix to use with controller messages.
 23	 * @since	1.6
 24	 */
 25	protected $text_prefix = 'COM_WEBLINKS';
 26
 27	/**
 28	 * Method to test whether a record can be deleted.
 29	 *
 30	 * @param	object	A record object.
 31	 * @return	boolean	True if allowed to delete the record. Defaults to the permission set in the component.
 32	 * @since	1.6
 33	 */
 34	protected function canDelete($record)
 35	{
 36		if (!empty($record->id)) {
 37			if ($record->state != -2) {
 38				return;
 39			}
 40			$user = JFactory::getUser();
 41
 42			if ($record->catid) {
 43				return $user->authorise('core.delete', 'com_weblinks.category.'.(int) $record->catid);
 44			}
 45			else {
 46				return parent::canDelete($record);
 47			}
 48		}
 49	}
 50
 51	/**
 52	 * Method to test whether a record can have its state changed.
 53	 *
 54	 * @param	object	A record object.
 55	 * @return	boolean	True if allowed to change the state of the record. Defaults to the permission set in the component.
 56	 * @since	1.6
 57	 */
 58	protected function canEditState($record)
 59	{
 60		$user = JFactory::getUser();
 61
 62		if (!empty($record->catid)) {
 63			return $user->authorise('core.edit.state', 'com_weblinks.category.'.(int) $record->catid);
 64		}
 65		else {
 66			return parent::canEditState($record);
 67		}
 68	}
 69	/**
 70	 * Returns a reference to the a Table object, always creating it.
 71	 *
 72	 * @param	type	The table type to instantiate
 73	 * @param	string	A prefix for the table class name. Optional.
 74	 * @param	array	Configuration array for model. Optional.
 75	 * @return	JTable	A database object
 76	 * @since	1.6
 77	 */
 78	public function getTable($type = 'Weblink', $prefix = 'WeblinksTable', $config = array())
 79	{
 80		return JTable::getInstance($type, $prefix, $config);
 81	}
 82
 83	/**
 84	 * Method to get the record form.
 85	 *
 86	 * @param	array	$data		An optional array of data for the form to interogate.
 87	 * @param	boolean	$loadData	True if the form is to load its own data (default case), false if not.
 88	 * @return	JForm	A JForm object on success, false on failure
 89	 * @since	1.6
 90	 */
 91	public function getForm($data = array(), $loadData = true)
 92	{
 93		$app = JFactory::getApplication();
 94
 95		// Get the form.
 96		$form = $this->loadForm('com_weblinks.weblink', 'weblink', array('control' => 'jform', 'load_data' => $loadData));
 97		if (empty($form)) {
 98			return false;
 99		}
100
101		// Determine correct permissions to check.
102		if ($this->getState('weblink.id')) {
103			// Existing record. Can only edit in selected categories.
104			$form->setFieldAttribute('catid', 'action', 'core.edit');
105		} else {
106			// New record. Can only create in selected categories.
107			$form->setFieldAttribute('catid', 'action', 'core.create');
108		}
109
110		// Modify the form based on access controls.
111		if (!$this->canEditState((object) $data)) {
112			// Disable fields for display.
113			$form->setFieldAttribute('ordering', 'disabled', 'true');
114			$form->setFieldAttribute('state', 'disabled', 'true');
115			$form->setFieldAttribute('publish_up', 'disabled', 'true');
116			$form->setFieldAttribute('publish_down', 'disabled', 'true');
117
118			// Disable fields while saving.
119			// The controller has already verified this is a record you can edit.
120			$form->setFieldAttribute('ordering', 'filter', 'unset');
121			$form->setFieldAttribute('state', 'filter', 'unset');
122			$form->setFieldAttribute('publish_up', 'filter', 'unset');
123			$form->setFieldAttribute('publish_down', 'filter', 'unset');
124		}
125
126		return $form;
127	}
128
129	/**
130	 * Method to get the data that should be injected in the form.
131	 *
132	 * @return	mixed	The data for the form.
133	 * @since	1.6
134	 */
135	protected function loadFormData()
136	{
137		// Check the session for previously entered form data.
138		$data = JFactory::getApplication()->getUserState('com_weblinks.edit.weblink.data', array());
139
140		if (empty($data)) {
141			$data = $this->getItem();
142
143			// Prime some default values.
144			if ($this->getState('weblink.id') == 0)
145			{
146				$app = JFactory::getApplication();
147				$data->set('catid', $app->input->get('catid', $app->getUserState('com_weblinks.weblinks.filter.category_id'), 'int'));
148			}
149		}
150
151		return $data;
152	}
153
154	/**
155	 * Method to get a single record.
156	 *
157	 * @param	integer	The id of the primary key.
158	 *
159	 * @return	mixed	Object on success, false on failure.
160	 * @since	1.6
161	 */
162	public function getItem($pk = null)
163	{
164		if ($item = parent::getItem($pk))
165		{
166			// Convert the params field to an array.
167			$registry = new JRegistry;
168			$registry->loadString($item->metadata);
169			$item->metadata = $registry->toArray();
170		}
171
172		if ($item = parent::getItem($pk))
173		{
174			// Convert the images field to an array.
175			$registry = new JRegistry;
176			$registry->loadString($item->images);
177			$item->images = $registry->toArray();
178		}
179
180		return $item;
181	}
182
183	/**
184	 * Prepare and sanitise the table prior to saving.
185	 *
186	 * @since	1.6
187	 */
188	protected function prepareTable($table)
189	{
190		$date = JFactory::getDate();
191		$user = JFactory::getUser();
192
193		$table->title		= htmlspecialchars_decode($table->title, ENT_QUOTES);
194		$table->alias		= JApplication::stringURLSafe($table->alias);
195
196		if (empty($table->alias)) {
197			$table->alias = JApplication::stringURLSafe($table->title);
198		}
199
200		if (empty($table->id))
201		{
202			// Set the values
203
204			// Set ordering to the last item if not set
205			if (empty($table->ordering)) {
206				$db = JFactory::getDbo();
207				$db->setQuery('SELECT MAX(ordering) FROM #__weblinks');
208				$max = $db->loadResult();
209
210				$table->ordering = $max + 1;
211			}
212			else
213			{
214				// Set the values
215				$table->modified	= $date->toSql();
216				$table->modified_by	= $user->get('id');
217			}
218
219			// Increment the content version number.
220			$table->version++;
221		}
222	}
223	/**
224	 * A protected method to get a set of ordering conditions.
225	 *
226	 * @param	object	A record object.
227	 * @return	array	An array of conditions to add to add to ordering queries.
228	 * @since	1.6
229	 */
230	protected function getReorderConditions($table)
231	{
232		$condition = array();
233		$condition[] = 'catid = '.(int) $table->catid;
234		return $condition;
235	}
236}