Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task#338 chore: Ability to select the Cluster before importing UCM Data #339

Open
wants to merge 2 commits into
base: release-1.2.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/com_tjucm/administrator/models/forms/type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<field name="layout" type="componentlayout" label="COM_TJUCM_FORM_LBL_TYPE_LAYOUT" description="COM_TJUCM_FORM_DESC_TYPE_LAYOUT" useglobal="true" extension="com_tjucm" view="itemform" />
<field name="details_layout" type="componentlayout" label="COM_TJUCM_DETAILS_LBL_TYPE_LAYOUT" description="COM_TJUCM_DETAILS_DESC_TYPE_LAYOUT" useglobal="true" extension="com_tjucm" view="item" />
<field name="list_layout" type="componentlayout" label="COM_TJUCM_LIST_LBL_TYPE_LAYOUT" description="COM_TJUCM_LIST_DESC_TYPE_LAYOUT" useglobal="true" extension="com_tjucm" view="items" />
<field name="import_items" type="radio" class="btn-group" default="0" label="COM_TJUCM_FORM_LBL_TYPE_IMPORT" description="COM_TJUCM_FORM_LBL_TYPE_IMPORT_DESC" hint="COM_TJUCM_FORM_LBL_TYPE_IMPORT_DESC">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>
</fields>
</fieldset>
<fieldset name="accesscontrol">
Expand Down
16 changes: 13 additions & 3 deletions src/components/com_tjucm/administrator/views/type/tmpl/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
// No direct access
defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;

JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
JHtml::_('behavior.tooltip');
Expand Down Expand Up @@ -51,9 +52,18 @@
<?php echo $this->form->renderField('state'); ?>
<?php echo $this->form->renderField('type_description'); ?>

<?php foreach ($this->form->getGroup('params') as $field) : ?>
<?php echo $field->renderField(); ?>
<?php endforeach; ?>
<?php foreach ($this->form->getGroup('params') as $field) :

if (strpos($field->name, 'import_items') == false)
{
echo $field->renderField();
}
elseif (ComponentHelper::getComponent('com_cluster', true)->enabled)
{
echo $field->renderField();
}

endforeach; ?>

<input type="hidden" name="jform[checked_out]" value="<?php echo $this->item->checked_out; ?>" />
<input type="hidden" name="jform[checked_out_time]" value="<?php echo $this->item->checked_out_time; ?>" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,5 @@ COM_TJUCM_FORM_DESC_LBL_TYPE_ALLOW_BITRATE_ON="Set to 'YES' if you want to allow
COM_TJUCM_FORM_LBL_TYPE_BITRATE_SECONDS="Autosave Interval (seconds)"
COM_TJUCM_FORM_DESC_LBL_TYPE_BITRATE_SECONDS="Enter time in seconds to save data repeatedly for this ucm type"
COM_TJUCM_SEARCH_FILTER_SUBMIT="Search"
COM_TJUCM_FORM_LBL_TYPE_IMPORT="Import Using Cluster"
COM_TJUCM_FORM_LBL_TYPE_IMPORT_DESC="Import items by using cluster"
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,5 @@ COM_TJUCM_ITEMS_IMPORTED_SCUUESSFULLY="%d record(s) imported successfully"
COM_TJUCM_ITEMS_IMPORT_REJECTED_RECORDS="%d invalid record(s) were not imported"
COM_TJUCM_ITEMS_NO_RECORDS_TO_IMPORT="No records found to import"
COM_TJUCM_ITEMS_IMPORTING_MSG="Please wait, Records are being imported..."
COM_TJUCM_ITEMS_CLUSTER="Select Cluster"
COM_TJUCM_ITEMS_CLUSTER_ERROR="Please select cluster to import a file"
45 changes: 43 additions & 2 deletions src/components/com_tjucm/site/controllers/items.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// No direct access.
defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Filesystem\File;
Expand Down Expand Up @@ -104,8 +105,23 @@ public function importCsv()
$fieldsLabel = array_column($fields, 'label');
$fieldHeaders = array_combine($fieldsName, $fieldsLabel);

// Get UCM Type data
JLoader::import('components.com_tjucm.tables.type', JPATH_ADMINISTRATOR);
$ucmTypeTable = Table::getInstance('Type', 'TjucmTable');
$ucmTypeTable->load(array("unique_identifier" => $client));

// Get decoded data object
$typeParams = new Registry($ucmTypeTable->params);

foreach ($fields as $field)
{
// Check com_cluster component is installed and configuration set Yes in UCM type form
if (ComponentHelper::getComponent('com_cluster', true)->enabled && $field->type == 'cluster' && $typeParams->get('import_items'))
{
$requiredFieldsName[$field->name] = $field->name;
continue;
}

// Get the required fields for the UCM type
if ($field->required == 1)
{
Expand Down Expand Up @@ -209,6 +225,14 @@ public function importCsv()
}
}

// Check com_cluster component is installed and configuration set Yes in UCM type form
if (ComponentHelper::getComponent('com_cluster', true)->enabled && $typeParams->get('import_items'))
{
$clusterId = $app->input->getInt("cluster", 0);
$clusterFieldName = str_replace('.', '_', $client) . '_clusterclusterid';
$itemData[$clusterFieldName] = $clusterId;
}

// Check if all the required values are present in the row
$isValid = (count(array_intersect_key($itemData, $requiredFieldsName)) == count($requiredFieldsName));

Expand Down Expand Up @@ -304,6 +328,9 @@ public function getCsvImportFormat()
$ucmTypeTable = Table::getInstance('Type', 'TjucmTable');
$ucmTypeTable->load(array("unique_identifier" => $client));

// Get decoded data object
$typeParams = new Registry($ucmTypeTable->params);

// Get fields in the given UCM type
JLoader::import('components.com_tjfields.models.fields', JPATH_ADMINISTRATOR);
$tjFieldsFieldsModel = BaseDatabaseModel::getInstance('Fields', 'TjfieldsModel', array('ignore_request' => true));
Expand All @@ -312,10 +339,24 @@ public function getCsvImportFormat()
$tjFieldsFieldsModel->setState('list.ordering', 'a.ordering');
$tjFieldsFieldsModel->setState('list.direction', 'asc');
$fields = $tjFieldsFieldsModel->getItems();
$fieldsLabel = array_column($fields, 'label');
$fieldsLabel = array_column($fields, 'label', 'id');

// Check if com_cluster component is installed
if (ComponentHelper::getComponent('com_cluster', true)->enabled && $typeParams->get('import_items'))
{
$fieldsType = array_column($fields, 'id', 'type');

foreach ($fieldsType as $key => $value)
{
if ($key == 'cluster')
{
unset($fieldsLabel[$value]);
}
}
}

// Generate schema CSV file with CSV headers as label of the fields for given UCM type and save it in temp folder
$fileName = preg_replace('/[^A-Za-z0-9\-]/', '', $ucmTypeTable->title) . '.csv';
$fileName = preg_replace('/[^A-Za-z0-9\-]/', '', $ucmTypeTable->title) . '_' . round(microtime(true)) . '.csv';
$csvFileTmpPath = Factory::getConfig()->get('tmp_path') . '/' . $fileName;
$output = fopen($csvFileTmpPath, 'w');
fputcsv($output, $fieldsLabel);
Expand Down
2 changes: 2 additions & 0 deletions src/components/com_tjucm/site/helpers/tjucm.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public static function getLanguageConstantForJs()
Text::script('COM_TJUCM_FIELDS_VALIDATION_ERROR_NUMBER', true);
Text::script('COM_TJUCM_MSG_ON_SAVED_FORM', true);
Text::script('COM_TJUCM_ITEMS_INVALID_CSV_FILE', true);
Text::script('COM_TJUCM_ITEMS_CLUSTER_ERROR', true);
Text::script('COM_TJUCM_ITEMS_UPLOAD_CSV_FILE', true);
}

/**
Expand Down
70 changes: 67 additions & 3 deletions src/components/com_tjucm/site/views/items/tmpl/importitems.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,45 @@
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Table\Table;
use Joomla\Registry\Registry;

HTMLHelper::_('formbehavior.chosen', 'select');
HTMLHelper::_('bootstrap.tooltip');

JLoader::import('components.com_tjfields.tables.type', JPATH_ADMINISTRATOR);
$ucmTypeTable = Table::getInstance('Type', 'TjucmTable', array('dbo', Factory::getDbo()));
$ucmTypeTable->load(array('unique_identifier' => $this->client));

// Get decoded data object
$typeParams = new Registry($ucmTypeTable->params);

Factory::getDocument()->addScriptDeclaration('
jQuery(document).ready(function(){
jQuery("#uploadForm #upload-submit").click(function() {

// Check cluster exist in upload/ import form
if (jQuery("#uploadForm #cluster").hasClass("import-cluster"))
{
var clusterId = jQuery("#uploadForm #cluster").val();

if (jQuery.trim(clusterId) =="" || clusterId == undefined)
{
jQuery("#uploadForm #cluster").next(".chzn-container").css("border-color", "red");
jQuery("#system-message-container").html();
Joomla.renderMessages({"error":[Joomla.JText._("COM_TJUCM_ITEMS_CLUSTER_ERROR")]});

return false;
}
}

if (jQuery("#uploadForm #csv-file-upload").val() == "")
{
jQuery("#uploadForm #csv-file-upload").css("border-color", "red");
jQuery("#system-message-container").html();
Joomla.renderMessages({"error":[Joomla.JText._("COM_TJUCM_ITEMS_UPLOAD_CSV_FILE")]});

return false;
}
Expand Down Expand Up @@ -55,17 +85,51 @@
});
');
?>
<form action="<?php echo JUri::root(); ?>index.php?option=com_tjucm&task=item.import&tmpl=component&client=<?php echo $this->client;?>" id="uploadForm" name="uploadForm" method="post" enctype="multipart/form-data">
<form action="<?php echo Uri::root(); ?>index.php?option=com_tjucm&task=item.import&tmpl=component&client=<?php echo $this->client;?>"
id="uploadForm" name="uploadForm" method="post" enctype="multipart/form-data">
<h2>
<?php echo Text::_("COM_TJUCM_IMPORT_ITEM");?>
</h2>
<hr class="hr hr-condensed">
<div>

<?php
// Check com_cluster component is installed and configuration set Yes in UCM type form
if (ComponentHelper::getComponent('com_cluster', true)->enabled && $typeParams->get('import_items'))
{
JLoader::import('components.com_tjfields.tables.field', JPATH_ADMINISTRATOR);
$fieldTable = Table::getInstance('Field', 'TjfieldsTable', array('dbo', $db));
$fieldTable->load(array('client' => $this->client, 'type' => 'cluster'));

if ($fieldTable->id)
{
FormHelper::addFieldPath(JPATH_ADMINISTRATOR . '/components/com_tjfields/models/fields/');
$cluster = FormHelper::loadFieldType('cluster', false);
$this->clusterList = $cluster->getOptionsExternally();
?>
<div class="col-sm-3">
<label for="cluster" class="control-label">
<strong><?php echo Text::_('COM_TJUCM_ITEMS_CLUSTER'); ?><span class="star"> *</span></strong>

</label>
<div class="clear-both">
<?php
echo HTMLHelper::_('select.genericlist', $this->clusterList, "cluster", ' id="cluster" class="import-cluster" required size="1"',
"value", "text");
?>
</div>
</div>
<?php
}
}
// Load filter fields
?>
<div class="col-sm-4">
<label for="csv-file-upload" class="control-label"><strong><?php echo Text::_('COM_TJUCM_ITEMS_UPLOAD_CSV_FILE'); ?></strong></label>
<label for="csv-file-upload" class="control-label"><strong><?php echo Text::_('COM_TJUCM_ITEMS_UPLOAD_CSV_FILE'); ?>
<span class="star"> *</span></strong></label>
<input type="file" required name="csv-file-upload" id="csv-file-upload" />
</div>
<div class="col-sm-8">
<div class="col-sm-5">
<div>&nbsp;</div>
<button class="btn btn-primary" id="upload-submit">
<i class="icon-upload icon-white"></i>
Expand Down