/** * Get the event dispatcher * * @return JEventDispatcher */ public static function getDispatcher() { if (!self::$dispatcher) { self::$dispatcher = version_compare(JVERSION, '3.0', 'lt') ? JDispatcher::getInstance() : JEventDispatcher::getInstance(); } return self::$dispatcher; }
/** * Get a database object. * * Returns the global {@link JDatabaseDriver} object, only creating it if it doesn't already exist. * * @return RDatabaseDriver * * @see JDatabaseDriver * @since 11.1 */ public static function getDbo() { if (!self::$database) { self::$database = self::createDbo(); } return self::$database; }
/** * Method to save the configuration data. * * @param array $data An array containing all global config data. * * @return bool True on success, false on failure. */ public function save($data) { $dispatcher = RFactory::getDispatcher(); $table = JTable::getInstance('Extension'); $isNew = true; // Save the rules. if (isset($data['params']) && isset($data['params']['rules'])) { $rules = new JAccessRules($data['params']['rules']); $asset = JTable::getInstance('asset'); if (!$asset->loadByName($data['option'])) { $root = JTable::getInstance('asset'); $root->loadByName('root.1'); $asset->name = $data['option']; $asset->title = $data['option']; $asset->setLocation($root->id, 'last-child'); } $asset->rules = (string) $rules; if (!$asset->check() || !$asset->store()) { $this->setError($asset->getError()); return false; } // We don't need this anymore unset($data['option']); unset($data['params']['rules']); } // Load the previous Data if (!$table->load($data['id'])) { $this->setError($table->getError()); return false; } unset($data['id']); // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onConfigurationBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, $table, $isNew)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } // Clean the component cache. $this->cleanCache('_system'); // Trigger the onConfigurationAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, $table, $isNew)); return true; }
/** * Method to save the form data. * * @param array $data The form data. * * @return boolean True on success. */ public function save($data) { $dispatcher = RFactory::getDispatcher(); $table = $this->getTable(); $pk = !empty($data['id']) ? $data['id'] : (int) $this->getState($this->getName() . '.id'); $isNew = true; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); // Load the row if saving an existing category. if ($pk > 0) { $table->load($pk); $isNew = false; } // Set the new parent id if parent id not matched OR while New/Save as Copy . if ($table->parent_id != $data['parent_id'] || $data['id'] == 0) { $table->setLocation($data['parent_id'], 'last-child'); } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Prepare the row for saving $this->prepareTable($table); // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the event_before_save event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $isNew)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } // Trigger the event_after_save event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $isNew)); // Rebuild the path for the category: if (!$table->rebuildPath($table->id)) { $this->setError($table->getError()); return false; } // Rebuild the paths of the category's children: if (!$table->rebuild($table->id, $table->lft, $table->level, $table->path)) { $this->setError($table->getError()); return false; } $this->setState($this->getName() . '.id', $table->id); // Clear the cache $this->cleanCache(); return true; }
/** * Method to get a list of field types available * * @return array The field option objects. * * @since 1.0 */ protected function getOptions() { $options = array(); $types = array(); JPluginHelper::importPlugin('rsfield'); $dispatcher = RFactory::getDispatcher(); $dispatcher->trigger('onGetFieldTypesList', array($types)); if (!empty($types)) { foreach ($types as $type) { $text = JText::_('PLG_RSFIELD_TYPE_' . strtoupper($type)); $options[$text] = JHtml::_('select.option', $type, $text); } } ksort($options); return array_merge(parent::getOptions(), $options); }
/** * Uploads file to the given media folder. * * @param array $files The array of Files (file descriptor returned by PHP) * @param string $destinationFolder Name of a folder in media/com_redcore/. * @param array $options Array of options for check * maxFileSize => Maximum allowed file size. Set 0 to disable check * allowedFileExtensions => Comma separated string list of allowed file extensions. * allowedMIMETypes => Comma separated string list of allowed MIME types. * setUniqueFileName => If set this will mangle destination file name * overrideExistingFile => If set this will override File with the same name if it exists * * @return array|bool */ public static function uploadFiles($files, $destinationFolder, $options = array()) { jimport('joomla.filesystem.file'); jimport('joomla.filesystem.path'); $app = JFactory::getApplication(); $resultFile = array(); foreach ($files as &$file) { // Get unique name if (!empty($options['setUniqueFileName'])) { $fileExtension = self::getExt($file['name']); $file['destinationFileName'] = self::getUniqueName($file['name']) . '.' . $fileExtension; } else { $file['destinationFileName'] = self::makeSafe($file['name']); } // Get full path $file['filePath'] = JPath::clean($destinationFolder . '/' . $file['destinationFileName']); // Can we upload this file type? if (!self::canUpload($file, $options)) { return false; } } JPluginHelper::importPlugin('content'); $dispatcher = RFactory::getDispatcher(); foreach ($files as &$file) { // Trigger the onContentBeforeSave event. $objectFile = new JObject($file); $result = $dispatcher->trigger('onContentBeforeSave', array('com_redcore.file', &$objectFile, true)); if (in_array(false, $result, true)) { // There are some errors in the plugins $errors = $objectFile->getErrors(); $app->enqueueMessage(JText::sprintf('LIB_REDCORE_ERROR_BEFORE_SAVE', implode('<br />', $errors)), 'error'); return false; } if (!self::upload($objectFile->tmp_name, $objectFile->filePath)) { // Error in upload $app->enqueueMessage(JText::_('LIB_REDCORE_ERROR_UNABLE_TO_UPLOAD_FILE'), 'error'); return false; } else { // Trigger the onContentAfterSave event. $dispatcher->trigger('onContentAfterSave', array('com_redcore.file', &$objectFile, true)); } $resultFile[] = array('original_filename' => $objectFile->name, 'uploaded_filename' => $objectFile->destinationFileName, 'mime_type' => !empty($objectFile->mimeTypeName) ? $objectFile->mimeTypeName : self::getMimeType($file), 'filepath' => $objectFile->filePath); } // Return the file info return $resultFile; }
/** * Method to get the field options. * * @return array The field option objects. */ protected function getOptions() { if (empty($this->cache)) { $app = RFactory::getApplication(); $options = array(); // Load specifc data channels? $types = $this->element['types'] ? explode(',', $this->element['types']) : array(); // Try to load the active dataChannels JPluginHelper::importPlugin('rschannel'); if ($dataChannels = $app->triggerEvent('onGetChannelTypes', array($types))) { foreach ($dataChannels as $channel) { $options[] = (object) array('value' => $channel->getType(), 'text' => $channel->getTitle()); } $options = array_merge(parent::getOptions(), $options); } $this->cache = $options; } return $this->cache; }
/** * Method to get the field options. * * @return array The field option objects. */ protected function getOptions() { if (empty($this->cache)) { $app = RFactory::getApplication(); $options = array(); $db = JFactory::getDbo(); $query = $db->getQuery(true)->select('id as value, name as text')->from('#__redsource_channel'); // Load specifc data channels? $states = $this->element['states'] ? $this->element['states'] : 1; if ($states !== null) { $states = explode(',', $states); JArrayHelper::toInteger($states); $query->where('state IN(' . implode(',', $states) . ')'); } $db->setQuery($query); if ($options = $db->loadObjectList()) { $options = array_merge(parent::getOptions(), $options); } $this->cache = $options; } return $this->cache; }
/** * Method to store a node in the database table. * * @param boolean $updateNulls True to update null values as well. * * @return boolean True on success. */ public function store($updateNulls = false) { $dispatcher = RFactory::getDispatcher(); // Import plugin types if ($this->_eventBeforeStore || $this->_eventAfterStore) { foreach ($this->_pluginTypesToImport as $type) { JPluginHelper::importPlugin($type); } } // Trigger before store if ($this->_eventBeforeStore) { $results = $dispatcher->trigger($this->_eventBeforeStore, array($this, $updateNulls)); if (count($results) && in_array(false, $results, true)) { return false; } } // Store if (!parent::store($updateNulls)) { return false; } // Trigger after store if ($this->_eventAfterStore) { $results = $dispatcher->trigger($this->_eventAfterStore, array($this, $updateNulls)); if (count($results) && in_array(false, $results, true)) { return false; } } return true; }
/** * Method to allow derived classes to preprocess the form. * * @param JForm $form A JForm object. * @param mixed $data The data expected for the form. * @param string $group The name of the plugin group to import (defaults to "content"). * * @return void * * @throws Exception if there is an error in the form event. */ protected function preprocessForm(JForm $form, $data, $group = 'content') { // Import the appropriate plugin group. JPluginHelper::importPlugin($group); // Get the dispatcher. $dispatcher = RFactory::getDispatcher(); // Trigger the form preparation event. $results = $dispatcher->trigger('onContentPrepareForm', array($form, $data)); // Check for errors encountered while preparing the form. if (count($results) && in_array(false, $results, true)) { // Get the last error. $error = $dispatcher->getError(); if (!$error instanceof Exception) { throw new Exception($error); } } }
/** * Called after store(). * * @param boolean $updateNulls True to update null values as well. * * @return boolean True on success. */ protected function afterStore($updateNulls = false) { if ($this->_eventAfterStore) { // Import the plugin types $this->importPluginTypes(); // Trigger the event $results = RFactory::getDispatcher()->trigger($this->_eventAfterStore, array($this, $updateNulls)); if (count($results) && in_array(false, $results, true)) { return false; } } return true; }
/** * Effectively bootstrap redCORE. * * @param bool $loadBootstrap Load bootstrap with redcore plugin options * * @return void */ public static function bootstrap($loadBootstrap = true) { if ($loadBootstrap && !defined('REDCORE_BOOTSTRAPPED')) { define('REDCORE_BOOTSTRAPPED', 1); } if (!defined('REDCORE_LIBRARY_LOADED')) { // Sets bootstrapped variable, to avoid bootstrapping redCORE twice define('REDCORE_LIBRARY_LOADED', 1); // Use our own base field if (!class_exists('JFormField', false)) { $baseField = JPATH_LIBRARIES . '/redcore/joomla/form/field.php'; if (file_exists($baseField)) { require_once $baseField; } } // Register the classes for autoload. JLoader::registerPrefix('R', JPATH_REDCORE); // Setup the RLoader. RLoader::setup(); // Make available the redCORE fields JFormHelper::addFieldPath(JPATH_REDCORE . '/form/field'); JFormHelper::addFieldPath(JPATH_REDCORE . '/form/fields'); // Make available the redCORE form rules JFormHelper::addRulePath(JPATH_REDCORE . '/form/rules'); // HTML helpers JHtml::addIncludePath(JPATH_REDCORE . '/html'); RHtml::addIncludePath(JPATH_REDCORE . '/html'); // Load library language $lang = JFactory::getLanguage(); $lang->load('lib_redcore', JPATH_REDCORE); // For Joomla! 2.5 compatibility we add some core functions if (version_compare(JVERSION, '3.0', '<')) { RLoader::registerPrefix('J', JPATH_LIBRARIES . '/redcore/joomla', false, true); } // Make available the fields JFormHelper::addFieldPath(JPATH_LIBRARIES . '/redcore/form/fields'); // Make available the rules JFormHelper::addRulePath(JPATH_LIBRARIES . '/redcore/form/rules'); // Replaces Joomla database driver for redCORE database driver JFactory::$database = null; JFactory::$database = RFactory::getDbo(); if (self::getConfig('enable_translations', 0) == 1 && !JFactory::getApplication()->isAdmin()) { // This is our object now $db = JFactory::getDbo(); // Enable translations $db->translate = self::getConfig('enable_translations', 0) == 1; // Reset plugin translations params if needed RTranslationHelper::resetPluginTranslation(); } } }
/** * Method to save the form data. * * @param array $data The form data. * * @return boolean True on success. */ public function save($data) { $translationTable = RedcoreHelpersTranslation::getTranslationTable(); $contentElement = RTranslationHelper::getContentElement($translationTable->option, $translationTable->xml); $translation = JFactory::getApplication()->input->get('translation', array(), 'array'); $original = JFactory::getApplication()->input->get('original', array(), 'array'); $id = !empty($data['rctranslations_id']) ? (int) $data['rctranslations_id'] : 0; $data = array_merge($data, $translation); $fieldsXml = $contentElement->getTranslateFields(); foreach ($fieldsXml as $field) { if ((string) $field['type'] == 'params' && (string) $field['translate'] == '1') { $fieldName = (string) $field['name']; $original[$fieldName] = $original['params_' . $fieldName]; $paramsChanged = false; if (!empty($data[$fieldName])) { $registry = new JRegistry(); $registry->loadString($original[$fieldName]); $originalParams = $registry->toArray(); foreach ($data[$fieldName] as $paramKey => $paramValue) { if (!isset($originalParams[$paramKey]) && $paramValue != '' || $originalParams[$paramKey] != $paramValue) { $paramsChanged = true; break; } } if ($paramsChanged) { $data[$fieldName] = json_encode($data[$fieldName]); } else { $data[$fieldName] = ''; } } } } $dispatcher = RFactory::getDispatcher(); /** @var RedcoreTableTranslation $table */ $table = $this->getTable(); if (empty($id)) { $db = $this->getDbo(); $query = $db->getQuery(true)->select('rctranslations_id')->from($db->qn(RTranslationTable::getTranslationsTableName($translationTable->table, '')))->where('rctranslations_language = ' . $db->q($data['rctranslations_language'])); foreach ($translationTable->primaryKeys as $primaryKey) { if (!empty($data[$primaryKey])) { $query->where($db->qn($primaryKey) . ' = ' . $db->q($data[$primaryKey])); } } $db->setQuery($query); $id = $db->loadResult(); } foreach ($translationTable->primaryKeys as $primaryKey) { $original[$primaryKey] = $data[$primaryKey]; } $isNew = true; // Load the row if saving an existing item. $table->load((int) $id); if ($table->rctranslations_modified) { $isNew = false; } $data['rctranslations_originals'] = RTranslationTable::createOriginalValueFromColumns($original, $translationTable->columns); // We run posthandler methods foreach ($fieldsXml as $field) { $postHandler = (string) $field['posthandler']; $fieldName = (string) $field['name']; if (!empty($postHandler) && (string) $field['translate'] == '1') { $postHandlerFunctions = explode(',', $postHandler); foreach ($postHandlerFunctions as $postHandlerFunction) { $postHandlerFunctionArray = explode('::', $postHandlerFunction); if (empty($postHandlerFunctionArray[1])) { $postHandlerFunctionArray[1] = $postHandlerFunctionArray[0]; $postHandlerFunctionArray[0] = 'RTranslationContentHelper'; $postHandlerFunction = 'RTranslationContentHelper::' . $postHandlerFunction; } if (method_exists($postHandlerFunctionArray[0], $postHandlerFunctionArray[1])) { call_user_func_array(array($postHandlerFunctionArray[0], $postHandlerFunctionArray[1]), array($field, &$data[$fieldName], &$data, $translationTable)); } } } } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Prepare the row for saving $this->prepareTable($table); // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $isNew)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $isNew)); $this->setState($this->getName() . '.id', $table->rctranslations_id); // Clear the cache $this->cleanCache(); return true; }
/** * Method to allow derived classes to preprocess the form. * * @param JForm $form A JForm object. * @param mixed $data The data expected for the form. * @param string $group The name of the plugin group to import (defaults to "content"). * @param array $column Content element column * @param RTranslationContentElement $contentElement Content element * * @return void * * @see JFormField * @since 12.2 * @throws Exception if there is an error in the form event. */ public static function preprocessForm(JForm $form, $data, $group = 'content', $column = array(), $contentElement = null) { if (strtolower($contentElement->name) == 'modules') { $form = self::preprocessFormModules($form, $data); } elseif (strtolower($contentElement->name) == 'menus') { $form = self::preprocessFormMenu($form, $data); } elseif (strtolower($contentElement->name) == 'plugins') { $form = self::preprocessFormPlugins($form, $data); } // Import the appropriate plugin group. JPluginHelper::importPlugin($group); // Get the dispatcher. $dispatcher = RFactory::getDispatcher(); // Trigger the form preparation event. $results = $dispatcher->trigger('onContentPrepareForm', array($form, $data)); // Check for errors encountered while preparing the form. if (count($results) && in_array(false, $results, true)) { // Get the last error. $error = $dispatcher->getError(); if (!$error instanceof Exception) { throw new Exception($error); } } }
/** * Method to process SQL updates previous to the install process * * @param JInstallerAdapter $parent Class calling this method * * @return boolean True on success */ public function preprocessUpdates($parent) { $manifest = $parent->get('manifest'); if (isset($manifest->update)) { if (isset($manifest->update->attributes()->folder)) { $path = $manifest->update->attributes()->folder; if (isset($manifest->update->pre) && isset($manifest->update->pre->schemas)) { $schemapaths = $manifest->update->pre->schemas->children(); if (count($schemapaths)) { $sourcePath = $parent->getParent()->getPath('source'); // If it just upgraded redCORE to a newer version using RFactory for database, it forces using the redCORE database drivers if (substr(get_class(JFactory::$database), 0, 1) == 'J' && $this->extensionElement != 'com_redcore') { RFactory::$database = null; JFactory::$database = null; JFactory::$database = RFactory::getDbo(); } $db = JFactory::getDbo(); $dbDriver = strtolower($db->name); $schemapath = ''; if ($dbDriver == 'mysqli') { $dbDriver = 'mysql'; } foreach ($schemapaths as $entry) { if (isset($entry->attributes()->type)) { $uDriver = strtolower($entry->attributes()->type); if ($uDriver == 'mysqli') { $uDriver = 'mysql'; } if ($uDriver == $dbDriver) { $schemapath = (string) $entry; break; } } } if ($schemapath != '') { $files = str_replace('.sql', '', JFolder::files($sourcePath . '/' . $path . '/' . $schemapath, '\\.sql$')); usort($files, 'version_compare'); if (count($files)) { foreach ($files as $file) { if (version_compare($file, $this->oldVersion) > 0) { $buffer = file_get_contents($sourcePath . '/' . $path . '/' . $schemapath . '/' . $file . '.sql'); $queries = RHelperDatabase::splitSQL($buffer); if (count($queries)) { foreach ($queries as $query) { if ($query != '' && $query[0] != '#') { $db->setQuery($query); if (!$db->execute(true)) { JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)), JLog::WARNING, 'jerror'); return false; } } } } } } } } } } } } return true; }
/** * Method to save the configuration data. * * @return bool True on success, false on failure. */ public static function saveRedcoreTranslationConfig() { $data = array(); $component = JComponentHelper::getComponent('com_redcore'); $component->params->set('translations', RTranslationHelper::getInstalledTranslationTables()); $data['params'] = $component->params->toString('JSON'); $dispatcher = RFactory::getDispatcher(); $table = JTable::getInstance('Extension'); $isNew = true; // Load the previous Data if (!$table->load($component->id)) { return false; } // Bind the data. if (!$table->bind($data)) { return false; } // Check the data. if (!$table->check()) { return false; } // Trigger the onConfigurationBeforeSave event. $result = $dispatcher->trigger('onExtensionBeforeSave', array('com_redcore.config', &$table, $isNew)); if (in_array(false, $result, true)) { return false; } // Store the data. if (!$table->store()) { return false; } // Trigger the onConfigurationAfterSave event. $dispatcher->trigger('onExtensionAfterSave', array('com_redcore.config', &$table, $isNew)); return true; }