function checklistPredictionTemplates() { $db = JFactory::getDbo(); $result = true; $copied = false; $i = 0; echo '<b>' . JText::_('Adding all templates to existing Prediction Games') . '</b>'; $query = 'SELECT id FROM #__joomleague_prediction_game'; $db->setQuery($query); if ($predictiongameIDs = $db->loadObjectList()) { foreach ($predictiongameIDs as $predictiongameID) { $prediction_id = $predictiongameID->id; $defaultpath = JPATH_COMPONENT_SITE . '/settings'; $extensiontpath = JPATH_COMPONENT_SITE . '/extensions/'; $templatePrefix = 'prediction'; if (!$prediction_id) { return; } // get info from prediction game $query = 'SELECT master_template FROM #__joomleague_prediction_game WHERE id=' . (int) $prediction_id; $db->setQuery($query); $params = $db->loadObject(); // if it's not a master template,do not create records. if ($params->master_template) { return true; } // otherwise,compare the records with the files // get records $query = 'SELECT template FROM #__joomleague_prediction_template WHERE prediction_id=' . (int) $prediction_id; $db->setQuery($query); $records = $db->loadColumn(); if (empty($records)) { $records = array(); } // first check extension template folder if template is not default if (isset($params->extension) && $params->extension != '') { if (is_dir($extensiontpath . $params->extension . '/settings')) { $xmldirs[] = $extensiontpath . $params->extension . '/settings'; } } // add default folder $xmldirs[] = $defaultpath . '/default'; // now check for all xml files in these folders foreach ($xmldirs as $xmldir) { if ($handle = opendir($xmldir)) { //check that each xml template has a corresponding record in the //database for this project. If not,create the rows with default values //from the xml file while ($file = readdir($handle)) { if ($file != '.' && $file != '..' && strtolower(substr($file, -3)) == 'xml' && strtolower(substr($file, 0, strlen($templatePrefix))) == $templatePrefix) { $template = substr($file, 0, strlen($file) - 4); if (empty($records) || !in_array($template, $records)) { //template not present,create a row with default values $params = new JLParameter(null, $xmldir . '/' . $file); //get the values $defaultvalues = array(); foreach ($params->getGroups() as $key => $group) { foreach ($params->getParams('params', $key) as $param) { $defaultvalues[] = $param[5] . '=' . $param[4]; } } $defaultvalues = implode('\\n', $defaultvalues); $title = JText::_($params->name); $query = "\tINSERT INTO #__joomleague_prediction_template (title,prediction_id,template,params)\n\t\t\t\t\t\t\t\t\t\t\t\tVALUES ('{$title}','{$prediction_id}','{$template}','{$defaultvalues}')"; $db->setQuery($query); //echo error,allows to check if there is a mistake in the template file if (!$db->execute()) { //$this->setError($db->getErrorMsg()); //return false; } array_push($records, $template); } } } closedir($handle); } } } } return ''; }
/** * check that all templates in default location have a corresponding record,except if project has a master template * */ private function _checklist() { $project_id = $this->_project_id; $defaultpath = JPATH_COMPONENT_SITE . DS . 'settings'; $extensiontpath = JPATH_COMPONENT_SITE . DS . 'extensions' . DS; $predictionTemplatePrefix = 'prediction'; if (!$project_id) { return; } // get info from project $query = 'SELECT master_template,extension FROM #__joomleague_project WHERE id=' . (int) $project_id; $this->_db->setQuery($query); $params = $this->_db->loadObject(); // if it's not a master template,do not create records. if ($params->master_template) { return true; } // otherwise,compare the records with the files // get records $query = 'SELECT template FROM #__joomleague_template_config WHERE project_id=' . (int) $project_id; $this->_db->setQuery($query); $records = $this->_db->loadResultArray(); if (empty($records)) { $records = array(); } // first check extension template folder if template is not default if (isset($params->extension) && $params->extension != '') { if (is_dir($extensiontpath . $params->extension . DS . 'settings')) { $xmldirs[] = $extensiontpath . $params->extension . DS . 'settings'; } } // add default folder $xmldirs[] = $defaultpath . DS . 'default'; // now check for all xml files in these folders foreach ($xmldirs as $xmldir) { if ($handle = opendir($xmldir)) { /* check that each xml template has a corresponding record in the database for this project. If not,create the rows with default values from the xml file */ while ($file = readdir($handle)) { if ($file != '.' && $file != '..' && $file != 'do_tipsl' && strtolower(substr($file, -3)) == 'xml' && strtolower(substr($file, 0, strlen($predictionTemplatePrefix))) != $predictionTemplatePrefix) { $template = substr($file, 0, strlen($file) - 4); if (empty($records) || !in_array($template, $records)) { //template not present,create a row with default values $params = new JLParameter(null, $xmldir . DS . $file); //get the values $defaultvalues = array(); foreach ($params->getGroups() as $key => $group) { foreach ($params->getParams('params', $key) as $param) { $defaultvalues[] = $param[5] . '=' . $param[4]; } } $defaultvalues = implode("\n", $defaultvalues); $query = "\tINSERT INTO #__joomleague_template_config (template,title,params,project_id)\n\t\t\t\t\t\t\t\t\t\t\t\t\tVALUES ('{$template}','{$params->name}','{$defaultvalues}','{$project_id}')"; $this->_db->setQuery($query); //echo error,allows to check if there is a mistake in the template file if (!$this->_db->query()) { $this->setError($this->_db->getErrorMsg()); return false; } array_push($records, $template); } } } closedir($handle); } } }