@my wife Roline van der Merwe
', \$errors));";
$script['display'][] = "\t\t\treturn false;";
$script['display'][] = "\t\t}";
$script['display'][] = "\n\t\t\$paths = new stdClass;";
$script['display'][] = "\t\t\$paths->first = '';";
$script['display'][] = "\t\t\$state = \$this->get('state');";
$script['display'][] = "\n\t\t\$this->paths = &\$paths;";
$script['display'][] = "\t\t\$this->state = &\$state;";
$script['display'][] = "\t\t// get global action permissions";
$script['display'][] = "\t\t\$this->canDo = ###-#-#-Component###Helper::getActions('import');";
$script['display'][] = "\n\t\t// We don't need toolbar in the modal window.";
$script['display'][] = "\t\tif (\$this->getLayout() !== 'modal')";
$script['display'][] = "\t\t{";
$script['display'][] = "\t\t\t\$this->addToolbar();";
$script['display'][] = "\t\t\t\$this->sidebar = JHtmlSidebar::render();";
$script['display'][] = "\t\t}";
$script['display'][] = "\n\t\t// get the session object";
$script['display'][] = "\t\t\$session = JFactory::getSession();";
$script['display'][] = "\t\t// check if it has package";
$script['display'][] = "\t\t\$this->hasPackage \t= \$session->get('hasPackage', false);";
$script['display'][] = "\t\t\$this->dataType \t= \$session->get('dataType', false);";
$script['display'][] = "\t\tif(\$this->hasPackage && \$this->dataType)";
$script['display'][] = "\t\t{";
$script['display'][] = "\t\t\t\$this->headerList \t= json_decode(\$session->get(\$this->dataType.'_VDM_IMPORTHEADERS', false),true);";
$script['display'][] = "\t\t\t\$this->headers \t\t= ###-#-#-Component###Helper::getFileHeaders(\$this->dataType);";
$script['display'][] = "\t\t\t// clear the data type";
$script['display'][] = "\t\t\t\$session->clear('dataType');";
$script['display'][] = "\t\t}";
$script['display'][] = "\n\t\t// Display the template";
$script['display'][] = "\t\tparent::display(\$tpl);";
$script['display'][] = "\t}";
}
elseif ('setdata' == $type)
{
// set the setdata script
$script['setdata'] = array();
$script['setdata'][] = "\t/**";
$script['setdata'][] = "\t* Set the data from the spreadsheet to the database";
$script['setdata'][] = "\t*";
$script['setdata'][] = "\t* @param string \$package Paths to the uploaded package file";
$script['setdata'][] = "\t*";
$script['setdata'][] = "\t* @return boolean false on failure";
$script['setdata'][] = "\t*";
$script['setdata'][] = "\t**/";
$script['setdata'][] = "\tprotected function setData(\$package,\$table,\$target_headers)";
$script['setdata'][] = "\t{";
$script['setdata'][] = "\t\tif (###-#-#-Component###Helper::checkArray(\$target_headers))";
$script['setdata'][] = "\t\t{";
$script['setdata'][] = "\t\t\t// make sure the file is loaded\t\t";
$script['setdata'][] = "\t\t\tJLoader::import('PHPExcel', JPATH_COMPONENT_ADMINISTRATOR . '/helpers');";
$script['setdata'][] = "\t\t\t\$jinput = JFactory::getApplication()->input;";
$script['setdata'][] = "\t\t\tforeach(\$target_headers as \$header)";
$script['setdata'][] = "\t\t\t{";
$script['setdata'][] = "\t\t\t\t\$data['target_headers'][\$header] = \$jinput->getString(\$header, null);";
$script['setdata'][] = "\t\t\t}";
$script['setdata'][] = "\t\t\t// set the data";
$script['setdata'][] = "\t\t\tif(isset(\$package['dir']))";
$script['setdata'][] = "\t\t\t{";
$script['setdata'][] = "\t\t\t\t\$inputFileType = PHPExcel_IOFactory::identify(\$package['dir']);";
$script['setdata'][] = "\t\t\t\t\$excelReader = PHPExcel_IOFactory::createReader(\$inputFileType);";
$script['setdata'][] = "\t\t\t\t\$excelReader->setReadDataOnly(true);";
$script['setdata'][] = "\t\t\t\t\$excelObj = \$excelReader->load(\$package['dir']);";
$script['setdata'][] = "\t\t\t\t\$data['array'] = \$excelObj->getActiveSheet()->toArray(null, true,true,true);";
$script['setdata'][] = "\t\t\t\t\$excelObj->disconnectWorksheets();";
$script['setdata'][] = "\t\t\t\tunset(\$excelObj);";
$script['setdata'][] = "\t\t\t\treturn \$this->save(\$data,\$table);";
$script['setdata'][] = "\t\t\t}";
$script['setdata'][] = "\t\t}";
$script['setdata'][] = "\t\treturn false;";
$script['setdata'][] = "\t}";
}
elseif ('save' == $type)
{
$script['save'] = array();
$script['save'][] = "\t/**";
$script['save'][] = "\t* Save the data from the file to the database";
$script['save'][] = "\t*";
$script['save'][] = "\t* @param string \$package Paths to the uploaded package file";
$script['save'][] = "\t*";
$script['save'][] = "\t* @return boolean false on failure";
$script['save'][] = "\t*";
$script['save'][] = "\t**/";
$script['save'][] = "\tprotected function save(\$data,\$table)";
$script['save'][] = "\t{";
$script['save'][] = "\t\t// import the data if there is any";
$script['save'][] = "\t\tif(###-#-#-Component###Helper::checkArray(\$data['array']))";
$script['save'][] = "\t\t{";
$script['save'][] = "\t\t\t// get user object";
$script['save'][] = "\t\t\t\$user \t\t= JFactory::getUser();";
$script['save'][] = "\t\t\t// remove header if it has headers";
$script['save'][] = "\t\t\t\$id_key \t= \$data['target_headers']['id'];";
$script['save'][] = "\t\t\t\$published_key \t= \$data['target_headers']['published'];";
$script['save'][] = "\t\t\t\$ordering_key \t= \$data['target_headers']['ordering'];";
$script['save'][] = "\t\t\t// get the first array set";
$script['save'][] = "\t\t\t\$firstSet = reset(\$data['array']);";
$script['save'][] = "";
$script['save'][] = "\t\t\t// check if first array is a header array and remove if true";
$script['save'][] = "\t\t\tif(\$firstSet[\$id_key] == 'id' || \$firstSet[\$published_key] == 'published' || \$firstSet[\$ordering_key] == 'ordering')";
$script['save'][] = "\t\t\t{";
$script['save'][] = "\t\t\t\tarray_shift(\$data['array']);";
$script['save'][] = "\t\t\t}";
$script['save'][] = "\t\t\t";
$script['save'][] = "\t\t\t// make sure there is still values in array and that it was not only headers";
$script['save'][] = "\t\t\tif(###-#-#-Component###Helper::checkArray(\$data['array']) && \$user->authorise(\$table.'.import', 'com_###-#-#-component###') && \$user->authorise('core.import', 'com_###-#-#-component###'))";
$script['save'][] = "\t\t\t{";
$script['save'][] = "\t\t\t\t// set target.";
$script['save'][] = "\t\t\t\t\$target\t= array_flip(\$data['target_headers']);";
$script['save'][] = "\t\t\t\t// Get a db connection.";
$script['save'][] = "\t\t\t\t\$db = JFactory::getDbo();";
$script['save'][] = "\t\t\t\t// set some defaults";
$script['save'][] = "\t\t\t\t\$todayDate\t\t= JFactory::getDate()->toSql();";
$script['save'][] = "\t\t\t\t// get global action permissions";
$script['save'][] = "\t\t\t\t\$canDo\t\t\t= ###-#-#-Component###Helper::getActions(\$table);";
$script['save'][] = "\t\t\t\t\$canEdit\t\t= \$canDo->get('core.edit');";
$script['save'][] = "\t\t\t\t\$canState\t\t= \$canDo->get('core.edit.state');";
$script['save'][] = "\t\t\t\t\$canCreate\t\t= \$canDo->get('core.create');";
$script['save'][] = "\t\t\t\t\$hasAlias\t\t= \$this->getAliasesUsed(\$table);";
$script['save'][] = "\t\t\t\t// prosses the data";
$script['save'][] = "\t\t\t\tforeach(\$data['array'] as \$row)";
$script['save'][] = "\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\$found = false;";
$script['save'][] = "\t\t\t\t\tif (isset(\$row[\$id_key]) && is_numeric(\$row[\$id_key]) && \$row[\$id_key] > 0)";
$script['save'][] = "\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t// raw items import & update!";
$script['save'][] = "\t\t\t\t\t\t\$query = \$db->getQuery(true);";
$script['save'][] = "\t\t\t\t\t\t\$query";
$script['save'][] = "\t\t\t\t\t\t\t->select('version')";
$script['save'][] = "\t\t\t\t\t\t\t->from(\$db->quoteName('#__###-#-#-component###_'.\$table))";
$script['save'][] = "\t\t\t\t\t\t\t->where(\$db->quoteName('id') . ' = '. \$db->quote(\$row[\$id_key]));";
$script['save'][] = "\t\t\t\t\t\t// Reset the query using our newly populated query object.";
$script['save'][] = "\t\t\t\t\t\t\$db->setQuery(\$query);";
$script['save'][] = "\t\t\t\t\t\t\$db->execute();";
$script['save'][] = "\t\t\t\t\t\t\$found = \$db->getNumRows();";
$script['save'][] = "\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t";
$script['save'][] = "\t\t\t\t\tif(\$found && \$canEdit)";
$script['save'][] = "\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t// update item";
$script['save'][] = "\t\t\t\t\t\t\$id \t\t= \$row[\$id_key];";
$script['save'][] = "\t\t\t\t\t\t\$version\t= \$db->loadResult();";
$script['save'][] = "\t\t\t\t\t\t// reset all buckets";
$script['save'][] = "\t\t\t\t\t\t\$query \t\t= \$db->getQuery(true);";
$script['save'][] = "\t\t\t\t\t\t\$fields \t= array();";
$script['save'][] = "\t\t\t\t\t\t// Fields to update.";
$script['save'][] = "\t\t\t\t\t\tforeach(\$row as \$key => \$cell)";
$script['save'][] = "\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t// ignore column";
$script['save'][] = "\t\t\t\t\t\t\tif ('IGNORE' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\tcontinue;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// update modified";
$script['save'][] = "\t\t\t\t\t\t\tif ('modified_by' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\tcontinue;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// update modified";
$script['save'][] = "\t\t\t\t\t\t\tif ('modified' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\tcontinue;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// update version";
$script['save'][] = "\t\t\t\t\t\t\tif ('version' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t\$cell = (int) \$version + 1;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// verify publish authority";
$script['save'][] = "\t\t\t\t\t\t\tif ('published' == \$target[\$key] && !\$canState)";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\tcontinue;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// set to update array";
$script['save'][] = "\t\t\t\t\t\t\tif(in_array(\$key, \$data['target_headers']) && is_numeric(\$cell))";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t\$fields[] = \$db->quoteName(\$target[\$key]) . ' = ' . \$cell;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\telseif(in_array(\$key, \$data['target_headers']) && is_string(\$cell))";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t\$fields[] = \$db->quoteName(\$target[\$key]) . ' = ' . \$db->quote(\$cell);";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\telseif(in_array(\$key, \$data['target_headers']) && is_null(\$cell))";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t// if import data is null then set empty";
$script['save'][] = "\t\t\t\t\t\t\t\t\$fields[] = \$db->quoteName(\$target[\$key]) . \" = ''\";";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t// load the defaults";
$script['save'][] = "\t\t\t\t\t\t\$fields[]\t= \$db->quoteName('modified_by') . ' = ' . \$db->quote(\$user->id);";
$script['save'][] = "\t\t\t\t\t\t\$fields[]\t= \$db->quoteName('modified') . ' = ' . \$db->quote(\$todayDate);";
$script['save'][] = "\t\t\t\t\t\t// Conditions for which records should be updated.";
$script['save'][] = "\t\t\t\t\t\t\$conditions = array(";
$script['save'][] = "\t\t\t\t\t\t\t\$db->quoteName('id') . ' = ' . \$id";
$script['save'][] = "\t\t\t\t\t\t);";
$script['save'][] = "\t\t\t\t\t\t";
$script['save'][] = "\t\t\t\t\t\t\$query->update(\$db->quoteName('#__###-#-#-component###_'.\$table))->set(\$fields)->where(\$conditions);";
$script['save'][] = "\t\t\t\t\t\t\$db->setQuery(\$query);";
$script['save'][] = "\t\t\t\t\t\t\$db->execute();";
$script['save'][] = "\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\telseif (\$canCreate)";
$script['save'][] = "\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t// insert item";
$script['save'][] = "\t\t\t\t\t\t\$query = \$db->getQuery(true);";
$script['save'][] = "\t\t\t\t\t\t// reset all buckets";
$script['save'][] = "\t\t\t\t\t\t\$columns \t= array();";
$script['save'][] = "\t\t\t\t\t\t\$values \t= array();";
$script['save'][] = "\t\t\t\t\t\t\$version\t= false;";
$script['save'][] = "\t\t\t\t\t\t// Insert columns. Insert values.";
$script['save'][] = "\t\t\t\t\t\tforeach(\$row as \$key => \$cell)";
$script['save'][] = "\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t// ignore column";
$script['save'][] = "\t\t\t\t\t\t\tif ('IGNORE' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\tcontinue;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// remove id";
$script['save'][] = "\t\t\t\t\t\t\tif ('id' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\tcontinue;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// update created";
$script['save'][] = "\t\t\t\t\t\t\tif ('created_by' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\tcontinue;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// update created";
$script['save'][] = "\t\t\t\t\t\t\tif ('created' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\tcontinue;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// Make sure the alias is incremented";
$script['save'][] = "\t\t\t\t\t\t\tif ('alias' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t\$cell = \$this->getAlias(\$cell,\$table);";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// update version";
$script['save'][] = "\t\t\t\t\t\t\tif ('version' == \$target[\$key])";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t\$cell = 1;";
$script['save'][] = "\t\t\t\t\t\t\t\t\$version = true;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\t// set to insert array";
$script['save'][] = "\t\t\t\t\t\t\tif(in_array(\$key, \$data['target_headers']) && is_numeric(\$cell))";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t\$columns[] \t= \$target[\$key];";
$script['save'][] = "\t\t\t\t\t\t\t\t\$values[] \t= \$cell;";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\telseif(in_array(\$key, \$data['target_headers']) && is_string(\$cell))";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t\$columns[] \t= \$target[\$key];";
$script['save'][] = "\t\t\t\t\t\t\t\t\$values[] \t= \$db->quote(\$cell);";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t\telseif(in_array(\$key, \$data['target_headers']) && is_null(\$cell))";
$script['save'][] = "\t\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\t// if import data is null then set empty";
$script['save'][] = "\t\t\t\t\t\t\t\t\$columns[] \t= \$target[\$key];";
$script['save'][] = "\t\t\t\t\t\t\t\t\$values[] \t= \"''\";";
$script['save'][] = "\t\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t// load the defaults";
$script['save'][] = "\t\t\t\t\t\t\$columns[] \t= 'created_by';";
$script['save'][] = "\t\t\t\t\t\t\$values[] \t= \$db->quote(\$user->id);";
$script['save'][] = "\t\t\t\t\t\t\$columns[] \t= 'created';";
$script['save'][] = "\t\t\t\t\t\t\$values[] \t= \$db->quote(\$todayDate);";
$script['save'][] = "\t\t\t\t\t\tif (!\$version)";
$script['save'][] = "\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\$columns[] \t= 'version';";
$script['save'][] = "\t\t\t\t\t\t\t\$values[] \t= 1;";
$script['save'][] = "\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t\t// Prepare the insert query.";
$script['save'][] = "\t\t\t\t\t\t\$query";
$script['save'][] = "\t\t\t\t\t\t\t->insert(\$db->quoteName('#__###-#-#-component###_'.\$table))";
$script['save'][] = "\t\t\t\t\t\t\t->columns(\$db->quoteName(\$columns))";
$script['save'][] = "\t\t\t\t\t\t\t->values(implode(',', \$values));";
$script['save'][] = "\t\t\t\t\t\t// Set the query using our newly populated query object and execute it.";
$script['save'][] = "\t\t\t\t\t\t\$db->setQuery(\$query);";
$script['save'][] = "\t\t\t\t\t\t\$done = \$db->execute();";
$script['save'][] = "\t\t\t\t\t\tif (\$done)";
$script['save'][] = "\t\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\t\t\$aId = \$db->insertid();";
$script['save'][] = "\t\t\t\t\t\t\t// make sure the access of asset is set";
$script['save'][] = "\t\t\t\t\t\t\t###-#-#-Component###Helper::setAsset(\$aId,\$table);";
$script['save'][] = "\t\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t\telse";
$script['save'][] = "\t\t\t\t\t{";
$script['save'][] = "\t\t\t\t\t\treturn false;";
$script['save'][] = "\t\t\t\t\t}";
$script['save'][] = "\t\t\t\t}";
$script['save'][] = "\t\t\t\treturn true;";
$script['save'][] = "\t\t\t}";
$script['save'][] = "\t\t}";
$script['save'][] = "\t\treturn false;";
$script['save'][] = "\t}";
}
elseif ('view' == $type)
{
$script['view'] = array();
$script['view'][] = "";
$script['view'][] = "";
$script['view'][] = "\n