| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Administrator 4 * @subpackage com_contact 5 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE.txt 7 */ 8 9 // No direct access 10 defined('_JEXEC') or die; 11 12 jimport('joomla.application.component.modeladmin'); 13 14 /** 15 * Item Model for a Contact. 16 * 17 * @package Joomla.Administrator 18 * @subpackage com_contact 19 * @since 1.6 20 */ 21 class ContactModelContact extends JModelAdmin 22 { 23 /** 24 * Method to perform batch operations on an item or a set of items. 25 * 26 * @param array $commands An array of commands to perform. 27 * @param array $pks An array of item ids. 28 * @param array $contexts An array of item contexts. 29 * 30 * @return boolean Returns true on success, false on failure. 31 * 32 * @since 2.5 33 */ 34 public function batch($commands, $pks, $contexts) 35 { 36 // Sanitize user ids. 37 $pks = array_unique($pks); 38 JArrayHelper::toInteger($pks); 39 40 // Remove any values of zero. 41 if (array_search(0, $pks, true)) 42 { 43 unset($pks[array_search(0, $pks, true)]); 44 } 45 46 if (empty($pks)) 47 { 48 $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED')); 49 return false; 50 } 51 52 $done = false; 53 54 if (!empty($commands['category_id'])) 55 { 56 $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c'); 57 58 if ($cmd == 'c') 59 { 60 $result = $this->batchCopy($commands['category_id'], $pks, $contexts); 61 if (is_array($result)) 62 { 63 $pks = $result; 64 } 65 else 66 { 67 return false; 68 } 69 } 70 elseif ($cmd == 'm' && !$this->batchMove($commands['category_id'], $pks, $contexts)) 71 { 72 return false; 73 } 74 $done = true; 75 } 76 77 if (!empty($commands['assetgroup_id'])) 78 { 79 if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts)) 80 { 81 return false; 82 } 83 84 $done = true; 85 } 86 87 if (!empty($commands['language_id'])) 88 { 89 if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) 90 { 91 return false; 92 } 93 94 $done = true; 95 } 96 97 if (strlen($commands['user_id']) > 0) 98 { 99 if (!$this->batchUser($commands['user_id'], $pks, $contexts)) 100 { 101 return false; 102 } 103 104 $done = true; 105 } 106 107 if (!$done) 108 { 109 $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION')); 110 return false; 111 } 112 113 // Clear the cache 114 $this->cleanCache(); 115 116 return true; 117 } 118 119 /** 120 * Batch copy items to a new category or current. 121 * 122 * @param integer $value The new category. 123 * @param array $pks An array of row IDs. 124 * @param array $contexts An array of item contexts. 125 * 126 * @return mixed An array of new IDs on success, boolean false on failure. 127 * 128 * @since 11.1 129 */ 130 protected function batchCopy($value, $pks, $contexts) 131 { 132 $categoryId = (int) $value; 133 134 $table = $this->getTable(); 135 $i = 0; 136 137 // Check that the category exists 138 if ($categoryId) 139 { 140 $categoryTable = JTable::getInstance('Category'); 141 if (!$categoryTable->load($categoryId)) 142 { 143 if ($error = $categoryTable->getError()) 144 { 145 // Fatal error 146 $this->setError($error); 147 return false; 148 } 149 else 150 { 151 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); 152 return false; 153 } 154 } 155 } 156 157 if (empty($categoryId)) 158 { 159 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); 160 return false; 161 } 162 163 // Check that the user has create permission for the component 164 $user = JFactory::getUser(); 165 if (!$user->authorise('core.create', 'com_contact.category.' . $categoryId)) 166 { 167 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); 168 return false; 169 } 170 171 // Parent exists so we let's proceed 172 while (!empty($pks)) 173 { 174 // Pop the first ID off the stack 175 $pk = array_shift($pks); 176 177 $table->reset(); 178 179 // Check that the row actually exists 180 if (!$table->load($pk)) 181 { 182 if ($error = $table->getError()) 183 { 184 // Fatal error 185 $this->setError($error); 186 return false; 187 } 188 else 189 { 190 // Not fatal error 191 $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); 192 continue; 193 } 194 } 195 196 // Alter the title & alias 197 $data = $this->generateNewTitle($categoryId, $table->alias, $table->name); 198 $table->name = $data['0']; 199 $table->alias = $data['1']; 200 201 // Reset the ID because we are making a copy 202 $table->id = 0; 203 204 // New category ID 205 $table->catid = $categoryId; 206 207 // TODO: Deal with ordering? 208 //$table->ordering = 1; 209 210 // Check the row. 211 if (!$table->check()) 212 { 213 $this->setError($table->getError()); 214 return false; 215 } 216 217 // Store the row. 218 if (!$table->store()) 219 { 220 $this->setError($table->getError()); 221 return false; 222 } 223 224 // Get the new item ID 225 $newId = $table->get('id'); 226 227 // Add the new ID to the array 228 $newIds[$i] = $newId; 229 $i++; 230 } 231 232 // Clean the cache 233 $this->cleanCache(); 234 235 return $newIds; 236 } 237 238 /** 239 * Batch change a linked user. 240 * 241 * @param integer $value The new value matching a User ID. 242 * @param array $pks An array of row IDs. 243 * @param array $contexts An array of item contexts. 244 * 245 * @return boolean True if successful, false otherwise and internal error is set. 246 * 247 * @since 2.5 248 */ 249 protected function batchUser($value, $pks, $contexts) 250 { 251 // Set the variables 252 $user = JFactory::getUser(); 253 $table = $this->getTable(); 254 255 foreach ($pks as $pk) 256 { 257 if ($user->authorise('core.edit', $contexts[$pk])) 258 { 259 $table->reset(); 260 $table->load($pk); 261 $table->user_id = (int) $value; 262 263 if (!$table->store()) 264 { 265 $this->setError($table->getError()); 266 return false; 267 } 268 } 269 else 270 { 271 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT')); 272 return false; 273 } 274 } 275 276 // Clean the cache 277 $this->cleanCache(); 278 279 return true; 280 } 281 282 /** 283 * Method to test whether a record can be deleted. 284 * 285 * @param object $record A record object. 286 * 287 * @return boolean True if allowed to delete the record. Defaults to the permission set in the component. 288 * @since 1.6 289 */ 290 protected function canDelete($record) 291 { 292 if (!empty($record->id)) { 293 if ($record->published != -2) { 294 return ; 295 } 296 $user = JFactory::getUser(); 297 return $user->authorise('core.delete', 'com_contact.category.'.(int) $record->catid); 298 } 299 } 300 301 /** 302 * Method to test whether a record can have its state edited. 303 * 304 * @param object $record A record object. 305 * 306 * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component. 307 * @since 1.6 308 */ 309 protected function canEditState($record) 310 { 311 $user = JFactory::getUser(); 312 313 // Check against the category. 314 if (!empty($record->catid)) { 315 return $user->authorise('core.edit.state', 'com_contact.category.'.(int) $record->catid); 316 } 317 // Default to component settings if category not known. 318 else { 319 return parent::canEditState($record); 320 } 321 } 322 323 /** 324 * Returns a Table object, always creating it 325 * 326 * @param type $type The table type to instantiate 327 * @param string $prefix A prefix for the table class name. Optional. 328 * @param array $config Configuration array for model. Optional. 329 * 330 * @return JTable A database object 331 * @since 1.6 332 */ 333 public function getTable($type = 'Contact', $prefix = 'ContactTable', $config = array()) 334 { 335 return JTable::getInstance($type, $prefix, $config); 336 } 337 338 /** 339 * Method to get the row form. 340 * 341 * @param array $data Data for the form. 342 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 343 * 344 * @return mixed A JForm object on success, false on failure 345 * @since 1.6 346 */ 347 public function getForm($data = array(), $loadData = true) 348 { 349 JForm::addFieldPath('JPATH_ADMINISTRATOR/components/com_users/models/fields'); 350 351 // Get the form. 352 $form = $this->loadForm('com_contact.contact', 'contact', array('control' => 'jform', 'load_data' => $loadData)); 353 if (empty($form)) { 354 return false; 355 } 356 357 // Modify the form based on access controls. 358 if (!$this->canEditState((object) $data)) { 359 // Disable fields for display. 360 $form->setFieldAttribute('featured', 'disabled', 'true'); 361 $form->setFieldAttribute('ordering', 'disabled', 'true'); 362 $form->setFieldAttribute('published', 'disabled', 'true'); 363 364 // Disable fields while saving. 365 // The controller has already verified this is a record you can edit. 366 $form->setFieldAttribute('featured', 'filter', 'unset'); 367 $form->setFieldAttribute('ordering', 'filter', 'unset'); 368 $form->setFieldAttribute('published', 'filter', 'unset'); 369 } 370 371 return $form; 372 } 373 374 /** 375 * Method to get a single record. 376 * 377 * @param integer $pk The id of the primary key. 378 * 379 * @return mixed Object on success, false on failure. 380 * @since 1.6 381 */ 382 public function getItem($pk = null) 383 { 384 if ($item = parent::getItem($pk)) { 385 // Convert the params field to an array. 386 $registry = new JRegistry; 387 $registry->loadString($item->metadata); 388 $item->metadata = $registry->toArray(); 389 } 390 391 return $item; 392 } 393 394 /** 395 * Method to get the data that should be injected in the form. 396 * 397 * @return mixed The data for the form. 398 * @since 1.6 399 */ 400 protected function loadFormData() 401 { 402 // Check the session for previously entered form data. 403 $data = JFactory::getApplication()->getUserState('com_contact.edit.contact.data', array()); 404 405 if (empty($data)) { 406 $data = $this->getItem(); 407 408 // Prime some default values. 409 if ($this->getState('contact.id') == 0) { 410 $app = JFactory::getApplication(); 411 $data->set('catid', JRequest::getInt('catid', $app->getUserState('com_contact.contacts.filter.category_id'))); 412 } 413 } 414 415 return $data; 416 } 417 418 /** 419 * Prepare and sanitise the table prior to saving. 420 * 421 * @param JTable $table 422 * 423 * @return void 424 * @since 1.6 425 */ 426 protected function prepareTable(&$table) 427 { 428 $date = JFactory::getDate(); 429 $user = JFactory::getUser(); 430 431 $table->name = htmlspecialchars_decode($table->name, ENT_QUOTES); 432 $table->alias = JApplication::stringURLSafe($table->alias); 433 434 if (empty($table->alias)) { 435 $table->alias = JApplication::stringURLSafe($table->name); 436 } 437 438 if (empty($table->id)) { 439 // Set the values 440 //$table->created = $date->toSql(); 441 442 // Set ordering to the last item if not set 443 if (empty($table->ordering)) { 444 $db = JFactory::getDbo(); 445 $db->setQuery('SELECT MAX(ordering) FROM #__contact_details'); 446 $max = $db->loadResult(); 447 448 $table->ordering = $max+1; 449 } 450 } 451 else { 452 // Set the values 453 //$table->modified = $date->toSql(); 454 //$table->modified_by = $user->get('id'); 455 } 456 } 457 458 /** 459 * A protected method to get a set of ordering conditions. 460 * 461 * @param JTable $table A record object. 462 * 463 * @return array An array of conditions to add to add to ordering queries. 464 * @since 1.6 465 */ 466 protected function getReorderConditions($table) 467 { 468 $condition = array(); 469 $condition[] = 'catid = '.(int) $table->catid; 470 471 return $condition; 472 } 473 474 /** 475 * Method to toggle the featured setting of contacts. 476 * 477 * @param array $pks The ids of the items to toggle. 478 * @param int $value The value to toggle to. 479 * 480 * @return boolean True on success. 481 * @since 1.6 482 */ 483 public function featured($pks, $value = 0) 484 { 485 // Sanitize the ids. 486 $pks = (array) $pks; 487 JArrayHelper::toInteger($pks); 488 489 if (empty($pks)) { 490 $this->setError(JText::_('COM_CONTACT_NO_ITEM_SELECTED')); 491 return false; 492 } 493 494 $table = $this->getTable(); 495 496 try 497 { 498 $db = $this->getDbo(); 499 500 $db->setQuery( 501 'UPDATE #__contact_details' . 502 ' SET featured = '.(int) $value. 503 ' WHERE id IN ('.implode(',', $pks).')' 504 ); 505 if (!$db->query()) { 506 throw new Exception($db->getErrorMsg()); 507 } 508 509 } 510 catch (Exception $e) 511 { 512 $this->setError($e->getMessage()); 513 return false; 514 } 515 516 $table->reorder(); 517 518 // Clean component's cache 519 $this->cleanCache(); 520 521 return true; 522 } 523 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |