Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: cda01a6052
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 233 lines (197 sloc) 7.878 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
<?php

/**
* SQL.PHP
* This file is include from view.php and print.php
* @version $Id$
* @copyright 2003
**/

/// Creating the SQL statements

/// Pivot is the field that set the break by groups (category, initial, author name, etc)

/// fullpivot indicate if the whole pivot should be compared agasint the db or just the first letter
/// printpivot indicate if the pivot should be printed or not
    switch ($CFG->dbtype) {
    case 'postgres7':
        $as = 'as';
    break;
    case 'mysql':
        $as = '';
    break;
    }

    switch ( $sortkey ) {
    case "CREATION":
        $sqlsortkey = "timecreated";
    break;
    
    case "UPDATE":
        $sqlsortkey = "timemodified";
    break;
    }
    $sqlsortorder = $sortorder;

    $fullpivot = 1;

    $userid = '';
    if ( isset($USER->id) ) {
        $userid = "OR ge.userid = $USER->id";
    }
    switch ($tab) {
    case GLOSSARY_CATEGORY_VIEW:
        if ($hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {

            $sqlselect = "SELECT gec.id, ge.*, gec.entryid, gc.name $as pivot";
            $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge,
{$CFG->prefix}glossary_entries_categories gec,
{$CFG->prefix}glossary_categories gc";
            $sqlwhere = "WHERE (ge.glossaryid = '$glossary->id' OR ge.sourceglossaryid = '$glossary->id') AND
ge.id = gec.entryid AND gc.id = gec.categoryid AND
(ge.approved != 0 $userid)";

            $sqlorderby = ' ORDER BY gc.name, ge.concept';

        } elseif ($hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {

            $printpivot = 0;
            $sqlselect = "SELECT ge.*, concept $as pivot";
            $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge";
            $sqlwhere = "WHERE (glossaryid = '$glossary->id' OR sourceglossaryid = '$glossary->id') AND
(ge.approved != 0 $userid)";


            $sqlorderby = ' ORDER BY concept';

        } else {

            $printpivot = 0;
            $sqlselect = "SELECT ge.*, ce.entryid, c.name $as pivot";
            $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories ce, {$CFG->prefix}glossary_categories c";
            $sqlwhere = "WHERE ge.id = ce.entryid AND ce.categoryid = $hook AND
ce.categoryid = c.id AND ge.approved != 0 AND
(ge.glossaryid = $glossary->id OR ge.sourceglossaryid = $glossary->id) AND
(ge.approved != 0 $userid)";

            $sqlorderby = ' ORDER BY c.name, ge.concept';

        }
    break;
    case GLOSSARY_AUTHOR_VIEW:

        if (!isset($sqlsortkey)) {
            $sqlsortkey = NULL;
        }

        $where = '';
        switch ($CFG->dbtype) {
        case 'postgres7':
            $usernametoshow = "u.firstname || ' ' || u.lastname";
            if ( $sqlsortkey == 'FIRSTNAME' ) {
                $usernamefield = "u.firstname || ' ' || u.lastname";
            } else {
                $usernamefield = "u.lastname || ' ' || u.firstname";
            }
            $where = "AND substr(ucase($usernamefield),1," . strlen($hook) . ") = '" . strtoupper($hook) . "'";
        break;
        case 'mysql':
            if ( $sqlsortkey == 'FIRSTNAME' ) {
                $usernamefield = "CONCAT(CONCAT(u.firstname,' '), u.lastname)";
            } else {
                $usernamefield = "CONCAT(CONCAT(u.lastname,' '), u.firstname)";
            }
            $where = "AND left(ucase($usernamefield)," . strlen($hook) . ") = '$hook'";
        break;
        }
        if ( $hook == 'ALL' ) {
            $where = '';
        }

        $sqlselect = "SELECT ge.id, $usernamefield $as pivot, u.id uid, ge.*";
        $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}user u";
        $sqlwhere = "WHERE ge.userid = u.id AND
(ge.approved != 0 $userid)
$where AND
(ge.glossaryid = $glossary->id OR ge.sourceglossaryid = $glossary->id)";
        $sqlorderby = "ORDER BY $usernamefield $sqlsortorder, ge.concept";
    break;
    case GLOSSARY_APPROVAL_VIEW:
        $fullpivot = 0;
        $printpivot = 0;

        if (!isset($sqlsortkey)) {
            $sqlsortkey = NULL;
        }

        $where = '';
        if ($hook != 'ALL' and $hook != 'SPECIAL') {
            switch ($CFG->dbtype) {
            case 'postgres7':
                $where = 'AND substr(ucase(concept),1,' . strlen($hook) . ') = \'' . strtoupper($hook) . '\'';
            break;
            case 'mysql':
                $where = 'AND left(ucase(concept),' . strlen($hook) . ") = '$hook'";
            break;
            }
        }

        $sqlselect = "SELECT ge.*, ge.concept $as pivot";
        $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge";
        $sqlwhere = "WHERE (ge.glossaryid = $glossary->id OR ge.sourceglossaryid = $glossary->id) AND
ge.approved = 0 $where";
                             
        if ( $sqlsortkey ) {
            $sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
        } else {
            $sqlorderby = "ORDER BY ge.concept";
        }
    break;
    case GLOSSARY_DATE_VIEW:
    case GLOSSARY_STANDARD_VIEW:
    default:
        $sqlselect = "SELECT ge.*, ge.concept $as pivot";
        $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge";

        $where = '';
        $fullpivot = 0;
        if ($CFG->dbtype == "postgres7") {
            $LIKE = "ILIKE"; // case-insensitive
        } else {
            $LIKE = "LIKE";
        }

        switch ( $mode ) {
        case 'search':
            $printpivot = 0;
            $where = "AND ( ge.concept $LIKE '%$hook%'";
            if ( $fullsearch ) {
                $where .= "OR ge.definition $LIKE '%$hook%')";
            } else {
                $where .= ")";
            }
        break;
        
        case 'term':
            $printpivot = 0;
            $sqlfrom .= " left join {$CFG->prefix}glossary_alias ga on ge.id = ga.entryid ";
            $where = "AND (ge.concept = '$hook' OR ga.alias = '$hook' )
";
        break;

        case 'entry':
            $printpivot = 0;
            $where = "AND ge.id = $hook";
        break;

        case 'letter':
            if ($hook != 'ALL' and $hook != 'SPECIAL') {
                switch ($CFG->dbtype) {
                case 'postgres7':
                    $where = 'AND substr(ucase(concept),1,' . strlen($hook) . ') = \'' . strtoupper($hook) . '\'';
                break;
                case 'mysql':
                    $where = 'AND left(ucase(concept),' . strlen($hook) . ") = '" . strtoupper($hook) . "'";
                break;
                }
            }
        break;
        }
        
        $sqlwhere = "WHERE (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) AND
(ge.approved != 0 $userid)
$where";
        switch ( $tab ) {
        case GLOSSARY_DATE_VIEW:
            $sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
        break;
        
        case GLOSSARY_STANDARD_VIEW:
            $sqlorderby = "ORDER BY ge.concept";
        default:
        break;
        }
    break;
    }
    $count = count_records_sql("select count(*) $sqlfrom $sqlwhere");

    $sqllimit = '';
    
    if ( $offset >= 0 ) {
        switch ($CFG->dbtype) {
        case 'postgres7':
            $sqllimit = " LIMIT $entriesbypage OFFSET $offset";
        break;
        case 'mysql':
            $sqllimit = " LIMIT $offset, $entriesbypage";
        break;
        }
    }

    $allentries = get_records_sql("$sqlselect $sqlfrom $sqlwhere $sqlorderby $sqllimit");
?>
Something went wrong with that request. Please try again.