[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_modules/models/ -> module.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Administrator
   4   * @subpackage  com_modules
   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  
  10  // No direct access.
  11  defined('_JEXEC') or die;
  12  
  13  jimport('joomla.application.component.modeladmin');
  14  
  15  /**
  16   * Module model.
  17   *
  18   * @package     Joomla.Administrator
  19   * @subpackage  com_modules
  20   * @since       1.6
  21   */
  22  class ModulesModelModule extends JModelAdmin
  23  {
  24      /**
  25       * @var    string  The prefix to use with controller messages.
  26       * @since  1.6
  27       */
  28      protected $text_prefix = 'COM_MODULES';
  29  
  30      /**
  31       * @var    string  The help screen key for the module.
  32       * @since  1.6
  33       */
  34      protected $helpKey = 'JHELP_EXTENSIONS_MODULE_MANAGER_EDIT';
  35  
  36      /**
  37       * @var    string  The help screen base URL for the module.
  38       * @since  1.6
  39       */
  40      protected $helpURL;
  41  
  42      /**
  43       * Method to auto-populate the model state.
  44       *
  45       * Note. Calling getState in this method will result in recursion.
  46       *
  47       * @return  void
  48       *
  49       * @since   1.6
  50       */
  51  	protected function populateState()
  52      {
  53          $app = JFactory::getApplication('administrator');
  54  
  55          // Load the User state.
  56          if (!($pk = (int) JRequest::getInt('id')))
  57          {
  58              if ($extensionId = (int) $app->getUserState('com_modules.add.module.extension_id'))
  59              {
  60                  $this->setState('extension.id', $extensionId);
  61              }
  62          }
  63          $this->setState('module.id', $pk);
  64  
  65          // Load the parameters.
  66          $params    = JComponentHelper::getParams('com_modules');
  67          $this->setState('params', $params);
  68      }
  69  
  70      /**
  71       * Method to perform batch operations on a set of modules.
  72       *
  73       * @param   array  $commands  An array of commands to perform.
  74       * @param   array  $pks       An array of item ids.
  75       * @param   array  $contexts  An array of item contexts.
  76       *
  77       * @return  boolean  Returns true on success, false on failure.
  78       *
  79       * @since   1.7
  80       */
  81  	public function batch($commands, $pks, $contexts)
  82      {
  83          // Sanitize user ids.
  84          $pks = array_unique($pks);
  85          JArrayHelper::toInteger($pks);
  86  
  87          // Remove any values of zero.
  88          if (array_search(0, $pks, true))
  89          {
  90              unset($pks[array_search(0, $pks, true)]);
  91          }
  92  
  93          if (empty($pks))
  94          {
  95              $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
  96              return false;
  97          }
  98  
  99          $done = false;
 100  
 101          if (!empty($commands['position_id']))
 102          {
 103              $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c');
 104  
 105              if (!empty($commands['position_id']))
 106              {
 107                  if ($cmd == 'c')
 108                  {
 109                      $result = $this->batchCopy($commands['position_id'], $pks, $contexts);
 110                      if (is_array($result))
 111                      {
 112                          $pks = $result;
 113                      }
 114                      else
 115                      {
 116                          return false;
 117                      }
 118                  }
 119                  elseif ($cmd == 'm' && !$this->batchMove($commands['position_id'], $pks, $contexts))
 120                  {
 121                      return false;
 122                  }
 123                  $done = true;
 124              }
 125          }
 126  
 127          if (!empty($commands['assetgroup_id']))
 128          {
 129              if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts))
 130              {
 131                  return false;
 132              }
 133  
 134              $done = true;
 135          }
 136  
 137          if (!empty($commands['language_id']))
 138          {
 139              if (!$this->batchLanguage($commands['language_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              return false;
 151          }
 152  
 153          // Clear the cache
 154          $this->cleanCache();
 155  
 156          return true;
 157      }
 158  
 159      /**
 160       * Batch copy modules to a new position or current.
 161       *
 162       * @param   integer  $value      The new value matching a module position.
 163       * @param   array    $pks        An array of row IDs.
 164       * @param   array    $contexts   An array of item contexts.
 165       *
 166       * @return  boolean  True if successful, false otherwise and internal error is set.
 167       *
 168       * @since   11.1
 169       */
 170  	protected function batchCopy($value, $pks, $contexts)
 171      {
 172          // Set the variables
 173          $user = JFactory::getUser();
 174          $table = $this->getTable();
 175          $i = 0;
 176  
 177          foreach ($pks as $pk)
 178          {
 179              if ($user->authorise('core.create', 'com_modules'))
 180              {
 181                  $table->reset();
 182                  $table->load($pk);
 183  
 184                  // Set the new position
 185                  if ($value == 'noposition')
 186                  {
 187                      $position = '';
 188                  }
 189                  elseif ($value == 'nochange')
 190                  {
 191                      $position = $table->position;
 192                  }
 193                  else
 194                  {
 195                      $position = $value;
 196                  }
 197                  $table->position = $position;
 198  
 199                  // Alter the title if necessary
 200                  $data = $this->generateNewTitle(0, $table->title, $table->position);
 201                  $table->title = $data['0'];
 202  
 203                  // Reset the ID because we are making a copy
 204                  $table->id = 0;
 205  
 206                  // Unpublish the new module
 207                  $table->published = 0;
 208  
 209                  if (!$table->store())
 210                  {
 211                      $this->setError($table->getError());
 212                      return false;
 213                  }
 214  
 215                  // Get the new item ID
 216                  $newId = $table->get('id');
 217  
 218                  // Add the new ID to the array
 219                  $newIds[$i]    = $newId;
 220                  $i++;
 221  
 222                  // Now we need to handle the module assignments
 223                  $db = $this->getDbo();
 224                  $query = $db->getQuery(true);
 225                  $query->select($db->quoteName('menuid'));
 226                  $query->from($db->quoteName('#__modules_menu'));
 227                  $query->where($db->quoteName('moduleid') . ' = ' . $pk);
 228                  $db->setQuery($query);
 229                  $menus = $db->loadColumn();
 230  
 231                  // Insert the new records into the table
 232                  foreach ($menus as $menu)
 233                  {
 234                      $query->clear();
 235                      $query->insert($db->quoteName('#__modules_menu'));
 236                      $query->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')));
 237                      $query->values($newId . ', ' . $menu);
 238                      $db->setQuery($query);
 239                      $db->query();
 240                  }
 241              }
 242              else
 243              {
 244                  $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE'));
 245                  return false;
 246              }
 247          }
 248  
 249          // Clean the cache
 250          $this->cleanCache();
 251  
 252          return $newIds;
 253      }
 254  
 255      /**
 256       * Batch move modules to a new position or current.
 257       *
 258       * @param   integer  $value      The new value matching a module position.
 259       * @param   array    $pks        An array of row IDs.
 260       * @param   array    $contexts   An array of item contexts.
 261       *
 262       * @return  boolean  True if successful, false otherwise and internal error is set.
 263       *
 264       * @since   11.1
 265       */
 266  	protected function batchMove($value, $pks, $contexts)
 267      {
 268          // Set the variables
 269          $user = JFactory::getUser();
 270          $table = $this->getTable();
 271          $i = 0;
 272  
 273          foreach ($pks as $pk)
 274          {
 275              if ($user->authorise('core.edit', 'com_modules'))
 276              {
 277                  $table->reset();
 278                  $table->load($pk);
 279  
 280                  // Set the new position
 281                  if ($value == 'noposition')
 282                  {
 283                      $position = '';
 284                  }
 285                  elseif ($value == 'nochange')
 286                  {
 287                      $position = $table->position;
 288                  }
 289                  else
 290                  {
 291                      $position = $value;
 292                  }
 293                  $table->position = $position;
 294  
 295                  // Alter the title if necessary
 296                  $data = $this->generateNewTitle(0, $table->title, $table->position);
 297                  $table->title = $data['0'];
 298  
 299                  // Unpublish the moved module
 300                  $table->published = 0;
 301  
 302                  if (!$table->store())
 303                  {
 304                      $this->setError($table->getError());
 305                      return false;
 306                  }
 307              }
 308              else
 309              {
 310                  $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
 311                  return false;
 312              }
 313          }
 314  
 315          // Clean the cache
 316          $this->cleanCache();
 317  
 318          return true;
 319      }
 320  
 321      /**
 322       * Method to delete rows.
 323       *
 324       * @param   array  &$pks  An array of item ids.
 325       *
 326       * @return  boolean  Returns true on success, false on failure.
 327       *
 328       * @since   1.6
 329       */
 330  	public function delete(&$pks)
 331      {
 332          // Initialise variables.
 333          $pks    = (array) $pks;
 334          $user    = JFactory::getUser();
 335          $table    = $this->getTable();
 336  
 337          // Iterate the items to delete each one.
 338          foreach ($pks as $i => $pk)
 339          {
 340              if ($table->load($pk))
 341              {
 342                  // Access checks.
 343                  if (!$user->authorise('core.delete', 'com_modules') || $table->published != -2)
 344                  {
 345                      JError::raiseWarning(403, JText::_('JERROR_CORE_DELETE_NOT_PERMITTED'));
 346                      //    throw new Exception(JText::_('JERROR_CORE_DELETE_NOT_PERMITTED'));
 347                      return;
 348                  }
 349  
 350                  if (!$table->delete($pk))
 351                  {
 352                      throw new Exception($table->getError());
 353                  }
 354                  else
 355                  {
 356                      // Delete the menu assignments
 357                      $db        = $this->getDbo();
 358                      $query    = $db->getQuery(true);
 359                      $query->delete();
 360                      $query->from('#__modules_menu');
 361                      $query->where('moduleid='.(int)$pk);
 362                      $db->setQuery((string)$query);
 363                      $db->query();
 364                  }
 365  
 366                  // Clear module cache
 367                  parent::cleanCache($table->module, $table->client_id);
 368              }
 369              else
 370              {
 371                  throw new Exception($table->getError());
 372              }
 373          }
 374  
 375          // Clear modules cache
 376          $this->cleanCache();
 377  
 378          return true;
 379      }
 380  
 381      /**
 382       * Method to duplicate modules.
 383       *
 384       * @param   array  &$pks  An array of primary key IDs.
 385       *
 386       * @return  boolean  True if successful.
 387       *
 388       * @since   1.6
 389       * @throws  Exception
 390       */
 391  	public function duplicate(&$pks)
 392      {
 393          // Initialise variables.
 394          $user    = JFactory::getUser();
 395          $db        = $this->getDbo();
 396  
 397          // Access checks.
 398          if (!$user->authorise('core.create', 'com_modules'))
 399          {
 400              throw new Exception(JText::_('JERROR_CORE_CREATE_NOT_PERMITTED'));
 401          }
 402  
 403          $table = $this->getTable();
 404  
 405          foreach ($pks as $pk)
 406          {
 407              if ($table->load($pk, true))
 408              {
 409                  // Reset the id to create a new record.
 410                  $table->id = 0;
 411  
 412                  // Alter the title.
 413                  $m = null;
 414                  if (preg_match('#\((\d+)\)$#', $table->title, $m))
 415                  {
 416                      $table->title = preg_replace('#\(\d+\)$#', '('.($m[1] + 1).')', $table->title);
 417                  }
 418                  else
 419                  {
 420                      $table->title .= ' (2)';
 421                  }
 422                  // Unpublish duplicate module
 423                  $table->published = 0;
 424  
 425                  if (!$table->check() || !$table->store())
 426                  {
 427                      throw new Exception($table->getError());
 428                  }
 429  
 430                  // $query = 'SELECT menuid'
 431                  //    . ' FROM #__modules_menu'
 432                  //    . ' WHERE moduleid = '.(int) $pk
 433                  //    ;
 434  
 435                  $query    = $db->getQuery(true);
 436                  $query->select('menuid');
 437                  $query->from('#__modules_menu');
 438                  $query->where('moduleid='.(int)$pk);
 439  
 440                  $this->_db->setQuery((string)$query);
 441                  $rows = $this->_db->loadColumn();
 442  
 443                  foreach ($rows as $menuid)
 444                  {
 445                      $tuples[] = '('.(int) $table->id.','.(int) $menuid.')';
 446                  }
 447              }
 448              else
 449              {
 450                  throw new Exception($table->getError());
 451              }
 452          }
 453  
 454          if (!empty($tuples))
 455          {
 456              // Module-Menu Mapping: Do it in one query
 457              $query = 'INSERT INTO #__modules_menu (moduleid,menuid) VALUES '.implode(',', $tuples);
 458              $this->_db->setQuery($query);
 459  
 460              if (!$this->_db->query())
 461              {
 462                  return JError::raiseWarning(500, $this->_db->getErrorMsg());
 463              }
 464          }
 465  
 466          // Clear modules cache
 467          $this->cleanCache();
 468  
 469          return true;
 470      }
 471  
 472      /**
 473       * Method to change the title.
 474       *
 475       * @param   integer $category_id  The id of the category. Not used here.
 476       * @param   string  $title     The title.
 477       * @param   string  $position  The position.
 478       *
 479       * @return    array  Contains the modified title.
 480       *
 481       * @since    2.5
 482       */
 483  	protected function generateNewTitle($category_id, $title, $position)
 484      {
 485          // Alter the title & alias
 486          $table = $this->getTable();
 487          while ($table->load(array('position' => $position, 'title' => $title)))
 488          {
 489              $title = JString::increment($title);
 490          }
 491  
 492          return array($title);
 493      }
 494  
 495      /**
 496       * Method to get the client object
 497       *
 498       * @return  void
 499       *
 500       * @since   1.6
 501       */
 502      function &getClient()
 503      {
 504          return $this->_client;
 505      }
 506  
 507      /**
 508       * Method to get the record form.
 509       *
 510       * @param   array    $data      Data for the form.
 511       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
 512       *
 513       * @return  JForm  A JForm object on success, false on failure
 514       *
 515       * @since   1.6
 516       */
 517  	public function getForm($data = array(), $loadData = true)
 518      {
 519          // The folder and element vars are passed when saving the form.
 520          if (empty($data))
 521          {
 522              $item        = $this->getItem();
 523              $clientId    = $item->client_id;
 524              $module        = $item->module;
 525          }
 526          else
 527          {
 528              $clientId    = JArrayHelper::getValue($data, 'client_id');
 529              $module        = JArrayHelper::getValue($data, 'module');
 530          }
 531  
 532          // These variables are used to add data from the plugin XML files.
 533          $this->setState('item.client_id',    $clientId);
 534          $this->setState('item.module',        $module);
 535  
 536          // Get the form.
 537          $form = $this->loadForm('com_modules.module', 'module', array('control' => 'jform', 'load_data' => $loadData));
 538          if (empty($form))
 539          {
 540              return false;
 541          }
 542  
 543          $form->setFieldAttribute('position', 'client', $this->getState('item.client_id') == 0 ? 'site' : 'administrator');
 544  
 545          // Modify the form based on access controls.
 546          if (!$this->canEditState((object) $data))
 547          {
 548              // Disable fields for display.
 549              $form->setFieldAttribute('ordering', 'disabled', 'true');
 550              $form->setFieldAttribute('published', 'disabled', 'true');
 551              $form->setFieldAttribute('publish_up', 'disabled', 'true');
 552              $form->setFieldAttribute('publish_down', 'disabled', 'true');
 553  
 554              // Disable fields while saving.
 555              // The controller has already verified this is a record you can edit.
 556              $form->setFieldAttribute('ordering', 'filter', 'unset');
 557              $form->setFieldAttribute('published', 'filter', 'unset');
 558              $form->setFieldAttribute('publish_up', 'filter', 'unset');
 559              $form->setFieldAttribute('publish_down', 'filter', 'unset');
 560          }
 561  
 562          return $form;
 563      }
 564  
 565      /**
 566       * Method to get the data that should be injected in the form.
 567       *
 568       * @return  mixed  The data for the form.
 569       *
 570       * @since   1.6
 571       */
 572  	protected function loadFormData()
 573      {
 574          $app = JFactory::getApplication();
 575  
 576          // Check the session for previously entered form data.
 577          $data = JFactory::getApplication()->getUserState('com_modules.edit.module.data', array());
 578  
 579          if (empty($data))
 580          {
 581              $data = $this->getItem();
 582  
 583              // This allows us to inject parameter settings into a new module.
 584              $params = $app->getUserState('com_modules.add.module.params');
 585              if (is_array($params))
 586              {
 587                  $data->set('params', $params);
 588              }
 589          }
 590  
 591          return $data;
 592      }
 593  
 594      /**
 595       * Method to get a single record.
 596       *
 597       * @param   integer  $pk  The id of the primary key.
 598       *
 599       * @return  mixed  Object on success, false on failure.
 600       *
 601       * @since   1.6
 602       */
 603  	public function getItem($pk = null)
 604      {
 605          // Initialise variables.
 606          $pk    = (!empty($pk)) ? (int) $pk : (int) $this->getState('module.id');
 607          $db    = $this->getDbo();
 608  
 609          if (!isset($this->_cache[$pk]))
 610          {
 611              $false    = false;
 612  
 613              // Get a row instance.
 614              $table = $this->getTable();
 615  
 616              // Attempt to load the row.
 617              $return = $table->load($pk);
 618  
 619              // Check for a table object error.
 620              if ($return === false && $error = $table->getError())
 621              {
 622                  $this->setError($error);
 623                  return $false;
 624              }
 625  
 626              // Check if we are creating a new extension.
 627              if (empty($pk))
 628              {
 629                  if ($extensionId = (int) $this->getState('extension.id'))
 630                  {
 631                      $query    = $db->getQuery(true);
 632                      $query->select('element, client_id');
 633                      $query->from('#__extensions');
 634                      $query->where('extension_id = '.$extensionId);
 635                      $query->where('type = '.$db->quote('module'));
 636                      $db->setQuery($query);
 637  
 638                      $extension = $db->loadObject();
 639                      if (empty($extension))
 640                      {
 641                          if ($error = $db->getErrorMsg())
 642                          {
 643                              $this->setError($error);
 644                          }
 645                          else
 646                          {
 647                              $this->setError('COM_MODULES_ERROR_CANNOT_FIND_MODULE');
 648                          }
 649                          return false;
 650                      }
 651  
 652                      // Extension found, prime some module values.
 653                      $table->module        = $extension->element;
 654                      $table->client_id    = $extension->client_id;
 655                  }
 656                  else
 657                  {
 658                      $app = JFactory::getApplication();
 659                      $app->redirect(JRoute::_('index.php?option=com_modules&view=modules', false));
 660                      return false;
 661                  }
 662              }
 663  
 664              // Convert to the JObject before adding other data.
 665              $properties = $table->getProperties(1);
 666              $this->_cache[$pk] = JArrayHelper::toObject($properties, 'JObject');
 667  
 668              // Convert the params field to an array.
 669              $registry = new JRegistry;
 670              $registry->loadString($table->params);
 671              $this->_cache[$pk]->params = $registry->toArray();
 672  
 673              // Determine the page assignment mode.
 674              $db->setQuery(
 675                  'SELECT menuid' .
 676                  ' FROM #__modules_menu' .
 677                  ' WHERE moduleid = '.$pk
 678              );
 679              $assigned = $db->loadColumn();
 680  
 681              if (empty($pk))
 682              {
 683                  // If this is a new module, assign to all pages.
 684                  $assignment = 0;
 685              }
 686              elseif (empty($assigned))
 687              {
 688                  // For an existing module it is assigned to none.
 689                  $assignment = '-';
 690              }
 691              else
 692              {
 693                  if ($assigned[0] > 0)
 694                  {
 695                      $assignment = +1;
 696                  }
 697                  elseif ($assigned[0] < 0)
 698                  {
 699                      $assignment = -1;
 700                  }
 701                  else
 702                  {
 703                      $assignment = 0;
 704                  }
 705              }
 706  
 707              $this->_cache[$pk]->assigned = $assigned;
 708              $this->_cache[$pk]->assignment = $assignment;
 709  
 710              // Get the module XML.
 711              $client    = JApplicationHelper::getClientInfo($table->client_id);
 712              $path    = JPath::clean($client->path.'/modules/'.$table->module.'/'.$table->module.'.xml');
 713  
 714              if (file_exists($path))
 715              {
 716                  $this->_cache[$pk]->xml = simplexml_load_file($path);
 717              }
 718              else
 719              {
 720                  $this->_cache[$pk]->xml = null;
 721              }
 722          }
 723  
 724          return $this->_cache[$pk];
 725      }
 726  
 727      /**
 728       * Get the necessary data to load an item help screen.
 729       *
 730       * @return  object  An object with key, url, and local properties for loading the item help screen.
 731       *
 732       * @since   1.6
 733       */
 734  	public function getHelp()
 735      {
 736          return (object) array('key' => $this->helpKey, 'url' => $this->helpURL);
 737      }
 738  
 739      /**
 740       * Returns a reference to the a Table object, always creating it.
 741       *
 742       * @param   string  $type    The table type to instantiate
 743       * @param   string  $prefix  A prefix for the table class name. Optional.
 744       * @param   array   $config  Configuration array for model. Optional.
 745       *
 746       * @return  JTable  A database object
 747       *
 748       * @since   1.6
 749      */
 750  	public function getTable($type = 'Module', $prefix = 'JTable', $config = array())
 751      {
 752          return JTable::getInstance($type, $prefix, $config);
 753      }
 754  
 755      /**
 756       * Prepare and sanitise the table prior to saving.
 757       *
 758       * @param   JTable  &$table  The database object
 759       *
 760       * @return  void
 761       *
 762       * @since   1.6
 763       */
 764  	protected function prepareTable(&$table)
 765      {
 766          $date = JFactory::getDate();
 767          $user = JFactory::getUser();
 768  
 769          $table->title        = htmlspecialchars_decode($table->title, ENT_QUOTES);
 770  
 771          if (empty($table->id))
 772          {
 773              // Set the values
 774              //$table->created    = $date->toSql();
 775          }
 776          else
 777          {
 778              // Set the values
 779              //$table->modified    = $date->toSql();
 780              //$table->modified_by    = $user->get('id');
 781          }
 782      }
 783  
 784      /**
 785       * Method to preprocess the form
 786       *
 787       * @param   JForm   $form   A form object.
 788       * @param   mixed   $data   The data expected for the form.
 789       * @param   string  $group  The name of the plugin group to import (defaults to "content").
 790       *
 791       * @return  void
 792       *
 793       * @since   1.6
 794       * @throws  Exception if there is an error loading the form.
 795       */
 796  	protected function preprocessForm(JForm $form, $data, $group = 'content')
 797      {
 798          jimport('joomla.filesystem.file');
 799          jimport('joomla.filesystem.folder');
 800  
 801          // Initialise variables.
 802          $lang        = JFactory::getLanguage();
 803          $clientId    = $this->getState('item.client_id');
 804          $module        = $this->getState('item.module');
 805  
 806          $client        = JApplicationHelper::getClientInfo($clientId);
 807          $formFile    = JPath::clean($client->path.'/modules/'.$module.'/'.$module.'.xml');
 808  
 809          // Load the core and/or local language file(s).
 810              $lang->load($module, $client->path, null, false, false)
 811          ||    $lang->load($module, $client->path.'/modules/'.$module, null, false, false)
 812          ||    $lang->load($module, $client->path, $lang->getDefault(), false, false)
 813          ||    $lang->load($module, $client->path.'/modules/'.$module, $lang->getDefault(), false, false);
 814  
 815          if (file_exists($formFile))
 816          {
 817              // Get the module form.
 818              if (!$form->loadFile($formFile, false, '//config'))
 819              {
 820                  throw new Exception(JText::_('JERROR_LOADFILE_FAILED'));
 821              }
 822  
 823              // Attempt to load the xml file.
 824              if (!$xml = simplexml_load_file($formFile))
 825              {
 826                  throw new Exception(JText::_('JERROR_LOADFILE_FAILED'));
 827              }
 828  
 829              // Get the help data from the XML file if present.
 830              $help = $xml->xpath('/extension/help');
 831              if (!empty($help))
 832              {
 833                  $helpKey = trim((string) $help[0]['key']);
 834                  $helpURL = trim((string) $help[0]['url']);
 835  
 836                  $this->helpKey = $helpKey ? $helpKey : $this->helpKey;
 837                  $this->helpURL = $helpURL ? $helpURL : $this->helpURL;
 838              }
 839  
 840          }
 841  
 842          // Trigger the default form events.
 843          parent::preprocessForm($form, $data, $group);
 844      }
 845  
 846      /**
 847       * Loads ContentHelper for filters before validating data.
 848       *
 849       * @param   object  $form  The form to validate against.
 850       * @param   array   $data  The data to validate.
 851       *
 852       * @return  mixed  Array of filtered data if valid, false otherwise.
 853       *
 854       * @since   1.1
 855       */
 856  	function validate($form, $data, $group = null)
 857      {
 858          require_once JPATH_ADMINISTRATOR.'/components/com_content/helpers/content.php';
 859  
 860          return parent::validate($form, $data, $group);
 861      }
 862  
 863      /**
 864       * Method to save the form data.
 865       *
 866       * @param   array  $data  The form data.
 867       *
 868       * @return  boolean  True on success.
 869       *
 870       * @since   1.6
 871       */
 872  	public function save($data)
 873      {
 874          // Initialise variables;
 875          $dispatcher = JDispatcher::getInstance();
 876          $table        = $this->getTable();
 877          $pk            = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('module.id');
 878          $isNew        = true;
 879  
 880          // Include the content modules for the onSave events.
 881          JPluginHelper::importPlugin('extension');
 882  
 883          // Load the row if saving an existing record.
 884          if ($pk > 0)
 885          {
 886              $table->load($pk);
 887              $isNew = false;
 888          }
 889  
 890          // Alter the title and published state for Save as Copy
 891          if (JRequest::getVar('task') == 'save2copy')
 892          {
 893              $orig_data    = JRequest::getVar('jform', array(), 'post', 'array');
 894              $orig_table = clone($this->getTable());
 895              $orig_table->load((int) $orig_data['id']);
 896  
 897              if ($data['title'] == $orig_table->title)
 898              {
 899                  $data['title'] .= ' '.JText::_('JGLOBAL_COPY');
 900                  $data['published'] = 0;
 901              }
 902          }
 903  
 904          // Bind the data.
 905          if (!$table->bind($data))
 906          {
 907              $this->setError($table->getError());
 908              return false;
 909          }
 910  
 911          // Prepare the row for saving
 912          $this->prepareTable($table);
 913  
 914          // Check the data.
 915          if (!$table->check())
 916          {
 917              $this->setError($table->getError());
 918              return false;
 919          }
 920  
 921          // Trigger the onExtensionBeforeSave event.
 922          $result = $dispatcher->trigger('onExtensionBeforeSave', array('com_modules.module', &$table, $isNew));
 923          if (in_array(false, $result, true))
 924          {
 925              $this->setError($table->getError());
 926              return false;
 927          }
 928  
 929          // Store the data.
 930          if (!$table->store())
 931          {
 932              $this->setError($table->getError());
 933              return false;
 934          }
 935  
 936          //
 937          // Process the menu link mappings.
 938          //
 939  
 940          $assignment = isset($data['assignment']) ? $data['assignment'] : 0;
 941  
 942          // Delete old module to menu item associations
 943          // $db->setQuery(
 944          //    'DELETE FROM #__modules_menu'.
 945          //    ' WHERE moduleid = '.(int) $table->id
 946          // );
 947  
 948          $db        = $this->getDbo();
 949          $query    = $db->getQuery(true);
 950          $query->delete();
 951          $query->from('#__modules_menu');
 952          $query->where('moduleid = '.(int)$table->id);
 953          $db->setQuery((string)$query);
 954          $db->query();
 955  
 956          if (!$db->query())
 957          {
 958              $this->setError($db->getErrorMsg());
 959              return false;
 960          }
 961  
 962          // If the assignment is numeric, then something is selected (otherwise it's none).
 963          if (is_numeric($assignment))
 964          {
 965              // Variable is numeric, but could be a string.
 966              $assignment = (int) $assignment;
 967  
 968              // Logic check: if no module excluded then convert to display on all.
 969              if ($assignment == -1 && empty($data['assigned']))
 970              {
 971                  $assignment = 0;
 972              }
 973  
 974              // Check needed to stop a module being assigned to `All`
 975              // and other menu items resulting in a module being displayed twice.
 976              if ($assignment === 0)
 977              {
 978                  // assign new module to `all` menu item associations
 979                  // $this->_db->setQuery(
 980                  //    'INSERT INTO #__modules_menu'.
 981                  //    ' SET moduleid = '.(int) $table->id.', menuid = 0'
 982                  // );
 983  
 984                  $query->clear();
 985                  $query->insert('#__modules_menu');
 986                  $query->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')));
 987                  $query->values((int)$table->id . ', 0');
 988                  $db->setQuery((string)$query);
 989                  if (!$db->query())
 990                  {
 991                      $this->setError($db->getErrorMsg());
 992                      return false;
 993                  }
 994              }
 995              elseif (!empty($data['assigned']))
 996              {
 997                  // Get the sign of the number.
 998                  $sign = $assignment < 0 ? -1 : +1;
 999  
1000                  // Preprocess the assigned array.
1001                  $tuples = array();
1002                  foreach ($data['assigned'] as &$pk)
1003                  {
1004                      $tuples[] = '('.(int) $table->id.','.(int) $pk * $sign.')';
1005                  }
1006  
1007                  $this->_db->setQuery(
1008                      'INSERT INTO #__modules_menu (moduleid, menuid) VALUES '.
1009                      implode(',', $tuples)
1010                  );
1011  
1012                  if (!$db->query())
1013                  {
1014                      $this->setError($db->getErrorMsg());
1015                      return false;
1016                  }
1017              }
1018          }
1019  
1020          // Trigger the onExtensionAfterSave event.
1021          $dispatcher->trigger('onExtensionAfterSave', array('com_modules.module', &$table, $isNew));
1022  
1023          // Compute the extension id of this module in case the controller wants it.
1024          $query    = $db->getQuery(true);
1025          $query->select('extension_id');
1026          $query->from('#__extensions AS e');
1027          $query->leftJoin('#__modules AS m ON e.element = m.module');
1028          $query->where('m.id = '.(int) $table->id);
1029          $db->setQuery($query);
1030  
1031          $extensionId = $db->loadResult();
1032  
1033          if ($error = $db->getErrorMsg())
1034          {
1035              JError::raiseWarning(500, $error);
1036              return;
1037          }
1038  
1039          $this->setState('module.extension_id',    $extensionId);
1040          $this->setState('module.id',            $table->id);
1041  
1042          // Clear modules cache
1043          $this->cleanCache();
1044  
1045          // Clean module cache
1046          parent::cleanCache($table->module, $table->client_id);
1047  
1048          return true;
1049      }
1050  
1051      /**
1052       * A protected method to get a set of ordering conditions.
1053       *
1054       * @param   object  $table  A record object.
1055       *
1056       * @return  array  An array of conditions to add to add to ordering queries.
1057       *
1058       * @since   1.6
1059       */
1060  	protected function getReorderConditions($table)
1061      {
1062          $condition = array();
1063          $condition[] = 'client_id = '.(int) $table->client_id;
1064          $condition[] = 'position = '. $this->_db->Quote($table->position);
1065  
1066          return $condition;
1067      }
1068  
1069      /**
1070       * Custom clean cache method for different clients
1071       *
1072       * @return  void
1073       *
1074       * @since    1.6
1075       */
1076  	protected function cleanCache($group = null, $client_id = 0)
1077      {
1078          parent::cleanCache('com_modules', $this->getClient());
1079      }
1080  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1