/** * Parse a content element file. * * @param string $groupName Group name * @param array $contentElementFiles Content element file path * * @return bool True on success * * @throws Exception */ public static function parseContentElementFiles($groupName, $contentElementFiles) { // Create an array of group data $groupData = array('groupName' => $groupName); $group = new NenoContentElementGroup($groupData); foreach ($contentElementFiles as $contentElementFile) { $xmlDoc = new DOMDocument(); if ($xmlDoc->load($contentElementFile) === false) { throw new Exception(JText::_('Error reading content element file')); } $tables = $xmlDoc->getElementsByTagName('table'); /* @var $tableData DOMElement */ foreach ($tables as $tableData) { $tableName = $tableData->getAttribute('name'); // If the table hasn't been added yet, let's add it if (!NenoHelper::isTableAlreadyDiscovered($tableName)) { $table = new NenoContentElementTable(array('tableName' => $tableName, 'translate' => 0)); $fields = $tableData->getElementsByTagName('field'); /* @var $fieldData DOMElement */ foreach ($fields as $fieldData) { $field = new NenoContentElementField(array('fieldName' => $fieldData->getAttribute('name'), 'translate' => intval($fieldData->getAttribute('translate')), 'table' => $table)); $table->addField($field); // If the field has this attribute, it means this is the primary key field of the table if ($fieldData->hasAttribute('referenceid')) { $table->setPrimaryKey($field->getFieldName()); } } $group->addTable($table); } } } $tables = $group->getTables(); // Checking if the group has tables if (!empty($tables)) { $group->persist(); } return true; }
/** * Grouping tables that haven't been discovered * * @param bool $persist Persist the group * * @return NenoContentElementGroup */ public static function groupingTablesNotDiscovered($persist = true) { /* @var $db NenoDatabaseDriverMysqlx */ $db = JFactory::getDbo(); // Get all the tables that haven't been detected using naming convention. $tablesNotDiscovered = self::getTablesNotDiscovered(); $tablesAdded = false; $otherGroup = null; if (!empty($tablesNotDiscovered)) { // Check if this group exists already $query = $db->getQuery(true); $query->select('g.id')->from('#__neno_content_element_groups AS g')->where('NOT EXISTS (SELECT 1 FROM #__neno_content_element_groups_x_extensions AS ge WHERE ge.group_id = g.id)'); $db->setQuery($query); $groupId = $db->loadResult(); if (!empty($groupId)) { /* @var $otherGroup NenoContentElementGroup */ $otherGroup = NenoContentElementGroup::load($groupId); } else { $otherGroup = new NenoContentElementGroup(array('group_name' => 'Other')); } $tablesIgnored = NenoHelper::getDoNotTranslateTables(); foreach ($tablesNotDiscovered as $tableNotDiscovered) { if (!in_array($tableNotDiscovered, $tablesIgnored)) { // Create an array with the table information $tableData = array('tableName' => $tableNotDiscovered, 'primaryKey' => $db->getPrimaryKey($tableNotDiscovered), 'translate' => 1, 'group' => $otherGroup); // Create ContentElement object $table = new NenoContentElementTable($tableData); // Get all the columns a table contains $fields = $db->getTableColumns($table->getTableName()); foreach ($fields as $fieldName => $fieldType) { $fieldData = array('fieldName' => $fieldName, 'fieldType' => $fieldType, 'translate' => NenoContentElementField::isTranslatableType($fieldType), 'table' => $table); $field = new NenoContentElementField($fieldData); $table->addField($field); } $otherGroup->addTable($table); $tablesAdded = true; } } $otherGroup->setAssignedTranslationMethods(NenoHelper::getTranslationMethodsForLanguages()); if ($persist) { $otherGroup->persist(); } } if (!$tablesAdded) { $otherGroup = null; } return $otherGroup; }