Skip to content

Commit

Permalink
More flexible login for mapping to Joomla tags in backend FC tags man…
Browse files Browse the repository at this point in the history
…ager
  • Loading branch information
ggppdk committed Feb 27, 2018
1 parent 614e4ae commit b701ca4
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 102 deletions.
72 changes: 32 additions & 40 deletions admin/controllers/tags.raw.php
Expand Up @@ -208,10 +208,10 @@ function countrows()
$session->set($indexer . '_items_to_index', $tags_ids, 'flexicontent');

echo 'success';
// echo count($fieldids)*count($itemids).'|';
// echo count($fieldids)*count($tagids).'|';

// WARNING: json_encode will output object if given an array with gaps in the indexing
// echo '|' . json_encode($itemids);
// echo '|' . json_encode($tagids);
// echo '|' . json_encode($fieldids);
echo '|' . count($tags_ids);
echo '|' . count(array());
Expand Down Expand Up @@ -265,8 +265,19 @@ function index()
die("'rebuildmode': '" . $rebuildmode . "'. not supported");
}

// Get items ids that have value for any of the searchable fields, but use session to avoid recalculation
$itemids = $session->get($indexer . '_items_to_index', array(), 'flexicontent');
$log_filename = 'tags_index_mappings_' . \JFactory::getUser()->id . '.php';
jimport('joomla.log.log');
JLog::addLogger(
array(
'text_file' => $log_filename, // Sets the target log file
'text_entry_format' => '{DATETIME} {PRIORITY} {MESSAGE}' // Sets the format of each line
),
JLog::ALL, // Sets messages of all log levels to be sent to the file
array('com_flexicontent.tags.mappings_indexer') // category of logged messages
);

// Get tag ids that have value for any of the searchable fields, but use session to avoid recalculation
$tagids = $session->get($indexer . '_items_to_index', array(), 'flexicontent');

// Get query size limit
$query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
Expand All @@ -292,9 +303,9 @@ function index()
$max_items_per_query = $max_items_per_query > $items_per_call ? $items_per_call : $max_items_per_query;
$cnt = $itemcnt;

while ($cnt < count($itemids) && $cnt < $itemcnt + $items_per_call)
while ($cnt < count($tagids) && $cnt < $itemcnt + $items_per_call)
{
$query_itemids = array_slice($itemids, $cnt, $max_items_per_query);
$query_itemids = array_slice($tagids, $cnt, $max_items_per_query);
$cnt += $max_items_per_query;

// Get files
Expand All @@ -311,57 +322,31 @@ function index()
// Find / Create Joomla tags
foreach ($tag_data as $tag_id => $tag)
{
$jtag_id_arr = $model->createTagsFromField(array($tag->alias => '#new#' . $tag->name));
$jtag_id = $jtag_id_arr ? reset($jtag_id_arr) : 0;
$tag->jtag_id = $jtag_id_arr ? reset($jtag_id_arr) : 0;
$jtag_data_arr = $model->createTagsFromText(array($tag->alias => '#new#' . $tag->name));
$jtag_data = $jtag_data_arr ? reset($jtag_data_arr) : false;
$tag->jtag_id = $jtag_data ? $jtag_data->id : 0;
$map_index[] = ' WHEN ' . $tag->id . ' THEN ' . $tag->jtag_id;
}

// Increment error count in session, and log errors into the log file
if (count($errors))
{
$log_filename = 'tags_index_mappings_' . \JFactory::getUser()->id . '.php';
jimport('joomla.log.log');
JLog::addLogger(
array(
'text_file' => $log_filename, // Sets the target log file
'text_entry_format' => '{DATETIME} {PRIORITY} {MESSAGE}' // Sets the format of each line
),
JLog::ALL, // Sets messages of all log levels to be sent to the file
array('com_flexicontent.tags.mappings_indexer') // category of logged messages
);

$mappings_indexer_error_count = $session->get('tags.mappings_indexer_error_count', 0, 'flexicontent');
$session->set('tags.mappings_indexer_error_count', $mappings_indexer_error_count + count($errors), 'flexicontent');

// mappings_indexer_errors = $session->get('tags.mappings_indexer_errors', array(), 'flexicontent');
foreach ($errors as $error_message)
{
// $mappings_indexer_errors[] = $error_message;
JLog::add($error_message, JLog::WARNING, 'com_flexicontent.tags.mappings_indexer');
}

// $session->set('tags.mappings_indexer_errors', $mappings_indexer_errors, 'flexicontent');
}
else

/*else
{
/*$log_filename = 'tags_index_mappings_' . \JFactory::getUser()->id . '.php';
jimport('joomla.log.log');
JLog::addLogger(
array(
'text_file' => $log_filename, // Sets the target log file
'text_entry_format' => '{DATETIME} {PRIORITY} {MESSAGE}' // Sets the format of each line
),
JLog::ALL, // Sets messages of all log levels to be sent to the file
array('com_flexicontent.tags.mappings_indexer') // category of logged messages
);
// mappings_indexer_errors = $session->get('tags.mappings_indexer_errors', array(), 'flexicontent');
foreach ($map_index as $map_index_clause)
{
// $mappings_indexer_errors[] = $error_message;
JLog::add($map_index_clause, JLog::INFO, 'com_flexicontent.tags.mappings_indexer');
}*/
}
}
}*/

// Create query that will update/insert data into the DB
unset($query);
Expand All @@ -382,6 +367,13 @@ function index()
}
}

// Terminate if not indexing any items
if (!$tagids)
{
echo 'fail|No tags need to be synced';
exit;
}

$elapsed_microseconds = round(1000000 * 10 * (microtime(true) - $start_microtime)) / 10;

if ($session->has($indexer . '_total_runtime', 'flexicontent'))
Expand Down
138 changes: 76 additions & 62 deletions admin/models/tags.php
Expand Up @@ -542,97 +542,111 @@ function getNotMappedTagIds()
/**
* Create any new tags by looking for #new# in the strings
*
* @param array $tags Tags text array from the field
* @param array $tags Tags text array from the field
* @param array $checkACL Flag to indicate if tag creation ACL should be used
*
* @return mixed If successful, metadata with new tag titles replaced by tag ids. Otherwise false.
* @return mixed If successful, an array of tag data, indexed via the given tag titles
*
* @since 3.3.0
*/
public function createTagsFromField($tags)
public function createTagsFromText($tags, $checkACL = true)
{
if (empty($tags) || (count($tags) === 1 && reset($tags) === ''))
{
return;
}
else

// We will use the tags table to store them
Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tags/tables');
$tagTable = Table::getInstance('Tag', 'TagsTable');
$newTags = array();
$canCreate = \JFactory::getUser()->authorise('core.create', 'com_tags');

foreach ($tags as $key => $tag)
{
// We will use the tags table to store them
Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tags/tables');
$tagTable = Table::getInstance('Tag', 'TagsTable');
$newTags = array();
$canCreate = \JFactory::getUser()->authorise('core.create', 'com_tags');
// Remove the #new# prefix that identifies new tags
$tagText = str_replace('#new#', '', $tag);

foreach ($tags as $key => $tag)
// User is not allowed to create tags, so don't create.
if ($checkACL && !$canCreate && strpos($tag, '#new#') !== false)
{
// User is not allowed to create tags, so don't create.
if (!$canCreate && strpos($tag, '#new#') !== false)
{
continue;
}
$newTags[$tagText] = null;
continue;
}

$loaded = false;

// Clear old data if exist
$tagTable->reset();

// (A) Try to load the selected tag via id
if (is_numeric($tag) && $tagTable->load((int) $tag))
{
$loaded = true;
}

// (B) Try to load the selected tag via title
elseif ($tagTable->load(array('title' => $tagText)))
{
$loaded = true;
}

// Remove the #new# prefix that identifies new tags
$tagText = str_replace('#new#', '', $tag);
else
{
// Set title then call check() method to auto-create an alias
$tagTable->title = $tagText;
$tagTable->check();

if ($tagText === $tag)
// (C) Try to load the selected tag, via auto-created alias
if ($tagTable->alias && $tagTable->load(array('alias' => $tagTable->alias)))
{
$newTags[$tagText] = (int) $tag;
$loaded = true;
}

// (D) Tag not found. Create a new tag at top-level with language 'ALL', with public access
else
{
// Clear old data if exist
$tagTable->reset();
// Prepare tag data
$tagTable->id = 0;
$tagTable->title = $tagText;
$tagTable->published = 1;

// Try to load the selected tag
if ($tagTable->load(array('title' => $tagText)))
{
$newTags[$tagText] = (int) $tagTable->id;
}
else
// Language ALL, Public access (assumed ... 1)
$tagTable->language = '*';
$tagTable->access = 1;

// Make this item a child of the root tag
$tagTable->setLocation($tagTable->getRootId(), 'last-child');

// Try to store tag
if ($tagTable->check())
{
// Try to load the selected tag, but this time via alias
$tagTable->title = $tagText;
$tagTable->check();
// Assign the alias as path (autogenerated tags have always level 1)
$tagTable->path = $tagTable->alias;

if ($tagTable->alias && $tagTable->load(array('alias' => $tagTable->alias)))
if ($tagTable->store())
{
$newTags[$tagText] = (int) $tagTable->id;
}
else
{
// Prepare tag data
$tagTable->id = 0;
$tagTable->title = $tagText;
$tagTable->published = 1;

// $tagTable->language = property_exists ($item, 'language') ? $item->language : '*';
$tagTable->language = '*';
$tagTable->access = 1;

// Make this item a child of the root tag
$tagTable->setLocation($tagTable->getRootId(), 'last-child');

// Try to store tag
if ($tagTable->check())
{
// Assign the alias as path (autogenerated tags have always level 1)
$tagTable->path = $tagTable->alias;

if ($tagTable->store())
{
$newTags[$tagText] = (int) $tagTable->id;
}
}
$loaded = true;
}
}
}
}

// At this point $tags is an array of all tag ids
$this->tags = $newTags;
$result = $newTags;
if ($loaded)
{
$newTags[$tagText] = (object) array(
'id' => (int) $tagTable->id,
'title' => $tagTable->title,
'alias' => $tagTable->alias
);
}
else
{
$newTags[$tagText] = null;
}
}

return $result;
return $newTags;
}


Expand Down

0 comments on commit b701ca4

Please sign in to comment.