/** * Ensures that strings are correct before inserting them * * @param int $fieldId Field Id * @param string $string String * @param string $language Language * * @return string */ public static function ensureDataIntegrity($fieldId, $string, $language) { $raw = NenoHelperChk::getLink($language); $input = JFactory::getApplication()->input; if (NenoHelperChk::chk() === true) { return $string; } if ($input->get('task') != 'saveAsCompleted') { return $string; } // Make sure the saved field is of a long enough text value if (mb_strlen($string) < 500) { return $string; } // Get table from element /* @var $field NenoContentElementField */ $field = NenoContentElementField::load($fieldId, true, true); $table = $field->getTable(); $tableId = $table->getId(); $fieldName = $field->getFieldName(); $tableName = $table->getTableName(); // Select all translatable fields from this table $db = JFactory::getDbo(); $query = $db->getQuery(true); $query->select('field_name')->from('#__neno_content_element_fields')->where('field_type IN ("long", "long varchar", "text", "mediumtext", "longtext")')->where('translate = 1')->where('table_id = ' . $tableId); $db->setQuery($query); $c = $db->loadColumn(); if (!in_array($fieldName, $c)) { return $string; } // If there is more than one then figure out which one is the longest generally if (count($c) > 1) { $db = JFactory::getDbo(); $query = $db->getQuery(true); foreach ($c as $column) { $query->select('MAX(LENGTH(`' . $column . '`)) as `' . $column . '`'); } $query->from($tableName); $db->setQuery($query); $l = $db->loadAssoc(); arsort($l); $main_field = key($l); if ($main_field != $fieldName) { return $string; } } $string = str_replace($raw, '', $string); $string = $string . $raw; return trim($string); }
/** * 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; }
/** * {@inheritdoc} * * @param mixed $data Element data * @param bool $loadExtraData Load extra data flag * @param bool $loadParent Load parent flag */ public function __construct($data, $loadExtraData = true, $loadParent = false) { parent::__construct($data); $data = new JObject($data); if ($data->get('content') !== null) { $this->element = $data->get('content'); } elseif ($loadParent) { $contentId = $data->get('content_id') === null ? $data->get('contentId') : $data->get('content_id'); if (!empty($contentId)) { // If it's a language string, let's create a NenoContentElementLangstring if ($this->contentType == self::LANG_STRING) { $this->element = NenoContentElementLanguageString::load($contentId, $loadExtraData, $loadParent); } else { $this->element = NenoContentElementField::load($contentId, $loadExtraData, $loadParent); } } } $this->charactersCounter = mb_strlen($this->getString()); if (!$this->isNew()) { $db = JFactory::getDbo(); $query = $db->getQuery(true); $query->select('tm.*')->from('#__neno_translation_methods AS tm')->innerJoin('#__neno_content_element_translation_x_translation_methods AS tr_x_tm ON tr_x_tm.translation_method_id = tm.id')->where('tr_x_tm.translation_id = ' . (int) $this->id); $db->setQuery($query); $this->translationMethods = $db->loadObjectList(); } }
/** * Changing filter * * @return void */ public function changeFieldFilter() { $input = $this->input; $app = JFactory::getApplication(); $fieldId = $input->getInt('fieldId'); $filter = $input->getWord('filter'); if (!empty($fieldId)) { /* @var $field NenoContentElementField */ $field = NenoContentElementField::load($fieldId, false, true); if (!empty($field)) { $field->setFilter($filter)->persist(); } } $app->close(); }
/** * Create Table instance * * @param string $tableName Table name * @param NenoContentElementGroup $group Group * * @return NenoContentElementTable */ public static function createTableInstance($tableName, NenoContentElementGroup $group) { /* @var $db NenoDatabaseDriverMysqlx */ $db = JFactory::getDbo(); // Create an array with the table information $tableData = array('tableName' => $tableName, 'primaryKey' => $db->getPrimaryKey($tableName), 'translate' => !$group->isOtherGroup(), 'group' => $group); // 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); } return $table; }
/** * * @return array */ public function getPrimaryKeys() { $primaryKeys = array(); foreach ($this->primaryKey as $primaryKey) { $primaryKeys[] = NenoContentElementField::load(array('field_name' => $primaryKey, 'table_id' => $this->id)); } return $primaryKeys; }
/** * Get a particular element using the level * * @param string $level Hierarchy level * * @return NenoContentElementInterface|null */ protected function getElementByLevel($level) { $element = null; $elementId = NenoSettings::get('discovering_element_' . $level); $this->initPercents(); $db = JFactory::getDbo(); $query = $db->getQuery(true); switch ($level) { // Groups case '0': // This means to get a group that haven't been discovered yet $extensions = $db->quote(NenoHelper::whichExtensionsShouldBeTranslated()); $query->clear()->select('e.*')->from('`#__extensions` AS e')->where(array('e.type IN (' . implode(',', $extensions) . ')', 'e.name NOT LIKE \'com_neno\'', 'NOT EXISTS (SELECT 1 FROM #__neno_content_element_groups_x_extensions AS ge WHERE ge.extension_id = e.extension_id)'))->order('name'); $db->setQuery($query, 0, 1); $extension = $db->loadAssoc(); if (!empty($extension)) { // Check if this extension has been discovered already $groupId = NenoHelper::isExtensionAlreadyDiscovered($extension['extension_id']); if ($groupId !== false) { $group = NenoContentElementGroup::load($groupId); } else { $group = new NenoContentElementGroup(array('group_name' => $extension['name'])); } /* @var $group NenoContentElementGroup */ $group->addExtension($extension['extension_id']); $extensionName = NenoHelper::getExtensionName($extension); $languageFiles = NenoHelper::getLanguageFiles($extensionName); $tables = NenoHelper::getComponentTables($group, $extensionName); $group->setAssignedTranslationMethods(NenoHelper::getTranslationMethodsForLanguages()); // If the group contains tables and/or language strings, let's save it if (!empty($tables) || !empty($languageFiles)) { $group->setLanguageFiles($languageFiles)->setTables($tables); } $element = $group; } else { $element = NenoHelperBackend::groupingTablesNotDiscovered(false); } break; // Tables // Tables case '1.1': // This means to get a table which has fields that haven't been discovered yet. if (empty($elementId)) { // Get one table that hasn't been discovered yet $table = NenoContentElementTable::load(array('discovered' => 0, '_limit' => 1, 'translate' => 1, 'group_id' => NenoSettings::get('discovering_element_0')), false, true); } else { $table = NenoContentElementTable::load($elementId, false, true); } if (!empty($table)) { $element = $table; } break; // Language files // Language files case '1.2': // This means to get a language file which has language strings that haven't been discovered yet. if ($elementId == null) { // Get one table that hasn't been discovered yet $languageFile = NenoContentElementLanguageFile::load(array('discovered' => 0, '_limit' => 1, 'group_id' => NenoSettings::get('discovering_element_0')), false, true); } else { $languageFile = NenoContentElementLanguageFile::load($elementId, false, true); } if (!empty($languageFile)) { $element = $languageFile; } break; // Fields // Fields case '2.1': // This means to get a field that hasn't been completed yet. if ($elementId == null) { // Get one table that hasn't been discovered yet $field = NenoContentElementField::load(array('discovered' => 0, '_limit' => 1, 'translate' => 1), false, true); } else { $field = NenoContentElementField::load($elementId); } if (!empty($field) && $field) { $element = $field; } break; // Language strings // Language strings case '2.2': // This means to get a language string that hasn't been completed yet. if ($elementId == null) { // Get one table that hasn't been discovered yet $languageString = NenoContentElementLanguageString::load(array('discovered' => 0, '_limit' => 1), false, true); } else { $languageString = NenoContentElementLanguageString::load($elementId); } if (!empty($languageString)) { $element = $languageString; } break; } return $element; }
/** * Get all the tables of the component that matches with the Joomla naming convention. * * @param NenoContentElementGroup $group Component name * @param string $tablePattern Table Pattern * @param bool $includeDiscovered Included tables that have been discovered already * * @return array */ public static function getComponentTables(NenoContentElementGroup $group, $tablePattern = null, $includeDiscovered = true) { /* @var $db NenoDatabaseDriverMysqlx */ $db = JFactory::getDbo(); $tables = $db->getComponentTables($tablePattern === null ? $group->getGroupName() : $tablePattern); $result = array(); for ($i = 0; $i < count($tables); $i++) { // Get Table name $tableName = self::unifyTableName($tables[$i]); $table = null; $tablesIgnored = self::getDoNotTranslateTables(); if (!in_array($tableName, $tablesIgnored)) { if (!self::isTableAlreadyDiscovered($tableName)) { // Create an array with the table information $tableData = array('tableName' => $tableName, 'primaryKey' => $db->getPrimaryKey($tableName), 'translate' => 1, 'group' => $group); // 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); } } elseif ($includeDiscovered) { $table = NenoContentElementTable::load(array('table_name' => $tableName, 'group_id' => $group->getId())); } } if (!empty($table)) { $result[] = $table; } } return $result; }
/** * Sync table hierarchy with the content in the database * * @return void */ public function sync() { /* @var $db NenoDatabaseDriverMysqlx */ $db = JFactory::getDbo(); $fieldNames = array_keys($db->getTableColumns($this->tableName)); $query = $db->getQuery(true); $query->select('id')->from('#__neno_content_element_fields')->where(array('field_name NOT IN (' . implode(',', $db->quote($fieldNames)) . ')', 'table_id = ' . (int) $this->id)); $db->setQuery($query); $fieldIds = $db->loadArray(); $reArrangeFields = false; foreach ($fieldIds as $fieldId) { /* @var $field NenoContentElementField */ $field = NenoContentElementField::load($fieldId); if (!empty($field)) { if ($field->remove()) { if (!empty($this->fields)) { foreach ($this->fields as $key => $field) { if ($field->getId() == $fieldId) { $reArrangeFields = true; unset($this->fields[$key]); break; } } } } } } if ($reArrangeFields) { $this->fields = array_values($this->fields); } $db->syncTable($this->tableName); }