Example #1
0
 public function save()
 {
     JSession::checkToken() or die('COM_JOOMLEAGUE_GLOBAL_INVALID_TOKEN');
     $post = JRequest::get('post');
     $cid = JRequest::getVar('cid', array(0), 'post', 'array');
     $index = 0;
     //$master_id=JRequest::getVar('master_id',0,'post','int');
     if (count($cid) == 1) {
         $post['id'] = (int) $cid[0];
         $model = $this->getModel('template');
         if ($model->store($post)) {
             $msg = JText::_('COM_JOOMLEAGUE_ADMIN_TEMPLATE_CTRL_SAVED_TEMPLATE');
         } else {
             $msg = JText::sprintf('COM_JOOMLEAGUE_ADMIN_TEMPLATE_CTRL_ERROR_SAVE_TEMPLATE', $index) . ' ' . $model->getError();
             break;
         }
         // Check the table in so it can be edited.... we are done with it anyway
         $model->checkin();
     } else {
         for ($index = 0; $index < count($cid); $index++) {
             $post['id'] = (int) $cid[$index];
             $model = $this->getModel('template');
             $model->setId($post['id']);
             $template = $model->getData();
             $templatepath = JPATH_COMPONENT_SITE . '/settings';
             $xmlfile = $templatepath . '/default/' . $template->template;
             $jlParams = new JLParameter($template->params, $xmlfile);
             $results = array();
             $params = null;
             $name = "params";
             foreach ($jlParams->getGroups() as $group => $groups) {
                 foreach ($jlParams->_xml[$group]->children() as $param) {
                     if (!in_array($param->attributes('name'), $template->params)) {
                         $post['params'][$param->attributes('name')] = $param->attributes('default');
                     }
                 }
             }
             if ($model->store($post)) {
                 $msg = JText::_('COM_JOOMLEAGUE_ADMIN_TEMPLATE_CTRL_REBUILD_TEMPLATES');
             } else {
                 $msg = JText::sprintf('COM_JOOMLEAGUE_ADMIN_TEMPLATE_CTRL_ERROR_REBUILD_TEMPLATE', $index) . ' ' . $model->getError();
                 break;
             }
             // Check the table in so it can be edited.... we are done with it anyway
             $model->checkin();
         }
     }
     $task = $this->getTask();
     if ($task == 'save') {
         $link = 'index.php?option=com_joomleague&view=templates&task=template.display';
     } else {
         $link = 'index.php?option=com_joomleague&task=template.edit&cid[]=' . $post['id'];
     }
     $this->setRedirect($link, $msg);
 }
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);
         }
     }
 }