/**
  * Get results
  */
 public function latest_results($cat = 0, $c = 0, $limit = 0)
 {
     if ($c <= 0) {
         $champ = self::current_championship();
     } else {
         $champ = new MCHChampionship($c);
     }
     if (!is_object($champ)) {
         $champ = self::last_championship();
     }
     $db = XoopsDatabaseFactory::getDatabaseConnection();
     $tbr = $db->prefix("mch_role");
     $tbs = $db->prefix("mch_score");
     $sql = "SELECT a.*, s.item, s.local as loc, s.visitor as vis, s.win, s.champ FROM {$tbr} as a, {$tbs} as s WHERE a.champ='" . $champ->id() . "' AND a.time<" . time();
     if ($cat > 0) {
         $sql .= " AND a.category='" . $cat . "'";
     }
     $sql .= " AND s.item=a.id_role ORDER BY a.time DESC LIMIT 0, {$limit}";
     $result = $db->query($sql);
     $tf = new RMTimeFormatter();
     while ($row = $db->fetchArray($result)) {
         $local = new MCHTeam($row['local']);
         $visitor = new MCHTeam($row['visitor']);
         $category = new MCHCategory($row['category']);
         $field = new MCHField($row['field']);
         $data[] = array('local' => array('id' => $local->id(), 'name' => $local->getVar('name'), 'logo' => XOOPS_UPLOAD_URL . '/teams/' . $local->getVar('logo'), 'score' => $row['loc']), 'visitor' => array('id' => $visitor->id(), 'name' => $visitor->getVar('name'), 'logo' => XOOPS_UPLOAD_URL . '/teams/' . $visitor->getVar('logo'), 'score' => $row['vis']), 'day' => $tf->format($row['time'], __('%M% %d%', 'match')), 'hour' => $tf->format($row['time'], __('%h%:%i%', 'match')), 'category' => array('name' => $category->getVar('name'), 'link' => $category->permalink()), 'field' => $field->getVar('name'), 'win' => $row['win']);
     }
     return $data;
 }
/**
* Save a new or edited category
*/
function m_save_category($edit = 0)
{
    global $xoopsSecurity;
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    if (!$xoopsSecurity->check()) {
        redirectMsg('./categories.php?action=' . ($edit ? 'edit&id=' . $id : 'new'), __('Session token expired!', 'match'), 1);
        die;
    }
    $db = XoopsDatabaseFactory::getDatabaseConnection();
    if ($edit) {
        //Verificamos si la categoría es válida
        if ($id <= 0) {
            redirectMsg('./categories.php?action=edit&id=' . $id, __('Wrong category ID!', 'match'), 1);
            die;
        }
        //Verificamos si la categoría existe
        $cat = new MCHCategory($id);
        if ($cat->isNew()) {
            redirectMsg('./categories.php?action=edit&id=' . $id, __('Specified category does not exists!', 'match'), 1);
            die;
        }
        //Verificamos el nombre de la categoría
        $sql = "SELECT COUNT(*) FROM " . $db->prefix('mch_categories') . " WHERE name='{$name}' AND id_cat<>'{$id}'";
        list($num) = $db->fetchRow($db->query($sql));
        if ($num > 0) {
            redirectMsg('./categories.php?action=edit&id=' . $id, __('A category with same name already exists!', 'match'), 1);
            die;
        }
        if ($nameid) {
            $sql = "SELECT COUNT(*) FROM " . $db->prefix('mch_categories') . " WHERE nameid='{$nameid}' AND id_cat<>'" . $id . "'";
            list($num) = $db->fetchRow($db->queryF($sql));
            if ($num > 0) {
                redirectMsg('./categories.php?action=edit&id=' . $id, __('There are already a category with same name id!', 'match'), 1);
                die;
            }
        }
    } else {
        $cat = new MCHCategory();
    }
    //Genera $nameid Nombre identificador
    $found = false;
    $i = 0;
    if ($name != $cat->getVar('name') || empty($nameid)) {
        do {
            $nameid = TextCleaner::sweetstring($name) . ($found ? $i : '');
            $sql = "SELECT COUNT(*) FROM " . $db->prefix('mch_categories') . " WHERE nameid = '{$nameid}'";
            list($num) = $db->fetchRow($db->queryF($sql));
            if ($num > 0) {
                $found = true;
                $i++;
            } else {
                $found = false;
            }
        } while ($found == true);
    }
    $cat->setVar('name', $name);
    $cat->setVar('description', $desc);
    $cat->setVar('active', $active);
    $cat->setVar('nameid', $nameid);
    $cat->setVar('parent', $parent);
    $cat = RMEvents::get()->run_event('match.save.category', $cat);
    if (!$cat->save()) {
        redirectMsg('./categories.php', __('Errors ocurred while trying to update database!', 'match') . '<br />' . $cat->errors(), 1);
        die;
    } else {
        redirectMsg('./categories.php', __('Database updated successfully!', 'match'), 0);
        die;
    }
}