mambax7 /
pedigree
| 1 | <?php |
||
| 2 | // ------------------------------------------------------------------------- |
||
| 3 | |||
| 4 | use Xmf\Request; |
||
| 5 | use XoopsModules\Pedigree; |
||
| 6 | |||
| 7 | //require_once dirname(dirname(__DIR__)) . '/mainfile.php'; |
||
| 8 | require_once __DIR__ . '/header.php'; |
||
| 9 | |||
| 10 | $moduleDirName = basename(__DIR__); |
||
| 11 | xoops_loadLanguage('main', $moduleDirName); |
||
| 12 | |||
| 13 | // Include any common code for this module. |
||
| 14 | require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/common.php'; |
||
| 15 | |||
| 16 | $GLOBALS['xoopsOption']['template_main'] = 'pedigree_result.tpl'; |
||
| 17 | include $GLOBALS['xoops']->path('/header.php'); |
||
| 18 | |||
| 19 | //get module configuration |
||
| 20 | /** @var XoopsModuleHandler $moduleHandler */ |
||
| 21 | $moduleHandler = xoops_getHandler('module'); |
||
| 22 | $module = $moduleHandler->getByDirname($moduleDirName); |
||
| 23 | $configHandler = xoops_getHandler('config'); |
||
| 24 | $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid')); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 25 | |||
| 26 | $f = Request::getString('f', 'naam', 'GET'); |
||
| 27 | $q = Request::getString('query', '', 'POST'); |
||
| 28 | /* |
||
| 29 | if (!isset($_GET['f'])) { |
||
| 30 | $f = "naam"; |
||
| 31 | } else { |
||
| 32 | $f = $_GET['f']; |
||
| 33 | } |
||
| 34 | |||
| 35 | if (isset($_POST['query'])) { |
||
| 36 | $q = $_POST['query']; |
||
| 37 | } else { |
||
| 38 | $q = ''; |
||
| 39 | } |
||
| 40 | */ |
||
| 41 | if ('' === $q < 1 && isset($_POST['query'])) { |
||
|
0 ignored issues
–
show
|
|||
| 42 | redirect_header('index.php', 3, _MA_PEDIGREE_SEARCH_SHORT); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (!isset($_GET['w'])) { |
||
| 46 | $w = '%' . $q . '%'; |
||
| 47 | } |
||
| 48 | |||
| 49 | if (isset($_GET['p'])) { |
||
| 50 | $p = $_GET['p']; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (isset($p)) { |
||
| 54 | $w = $q; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (isset($_GET['w'])) { |
||
| 58 | if ('zero' === $_GET['w'] || '' === $_GET['w'] || '0' === $_GET['w']) { |
||
| 59 | $w = '0'; |
||
| 60 | } else { |
||
| 61 | $w = $_GET['w']; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | if (isset($_GET['l'])) { |
||
| 65 | if ('1' == $_GET['l'] || 'LIKE' === $_GET['l']) { |
||
| 66 | $l = 'LIKE'; |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | $l = '='; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (!$_GET['o']) { |
||
| 73 | $o = 'naam'; |
||
| 74 | } else { |
||
| 75 | $o = $_GET['o']; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (!isset($_GET['d'])) { |
||
| 79 | $d = 'ASC'; |
||
| 80 | } else { |
||
| 81 | $d = $_GET['d']; |
||
| 82 | } |
||
| 83 | |||
| 84 | if (!isset($_GET['st'])) { |
||
| 85 | $st = 0; |
||
| 86 | } else { |
||
| 87 | $st = $_GET['st']; |
||
| 88 | } |
||
| 89 | |||
| 90 | $perPage = $moduleConfig['perpage']; |
||
| 91 | |||
| 92 | //is current user a module admin? |
||
| 93 | $modadmin = false; |
||
| 94 | $xoopsModule = XoopsModule::getByDirname($moduleDirName); |
||
| 95 | if (!empty($GLOBALS['xoopsUser']) && ($GLOBALS['xoopsUser'] instanceof \XoopsUser) |
||
| 96 | && $GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) { |
||
| 97 | $modadmin = true; |
||
| 98 | } |
||
| 99 | |||
| 100 | //count total number of dogs |
||
| 101 | $numDog = 'SELECT COUNT(id) FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' WHERE ' . $f . ' ' . $l . " '" . $w . "'"; |
||
| 102 | $numRes = $GLOBALS['xoopsDB']->query($numDog); |
||
| 103 | //total number of dogs the query will find |
||
| 104 | list($numResults) = $GLOBALS['xoopsDB']->fetchRow($numRes); |
||
| 105 | //if nothing is found |
||
| 106 | if (0 == $numResults) { |
||
| 107 | //just for debug information |
||
| 108 | //echo $numDog; |
||
| 109 | redirect_header('index.php', 15, strtr(_MA_PEDIGREE_SEARCH_NO, ['[animalTypes]' => $moduleConfig['animalTypes']])); |
||
| 110 | } |
||
| 111 | //total number of pages |
||
| 112 | $numPages = floor($numResults / $perPage) + 1; |
||
| 113 | if (($numPages * $perPage) == ($numResults + $perPage)) { |
||
| 114 | --$numPages; |
||
| 115 | } |
||
| 116 | //find current page |
||
| 117 | $currentPage = floor($st / $perPage) + 1; |
||
| 118 | //create empty pages variable |
||
| 119 | $pages = ''; |
||
| 120 | |||
| 121 | $length = strlen($w); |
||
| 122 | if ('%' === substr($w, $length - 1, $length)) { |
||
| 123 | $whe = substr($w, 0, $length - 1) . '%25'; |
||
| 124 | if (0 === strncmp($whe, '%', 1)) { |
||
| 125 | $length = strlen($whe); |
||
| 126 | $whe = '%25' . substr($whe, 1, $length); |
||
| 127 | } |
||
| 128 | } else { |
||
| 129 | $whe = $w; |
||
| 130 | } |
||
| 131 | /* @todo: replace this with standard XOOPS Page Navigation */ |
||
| 132 | //create previous button |
||
| 133 | if ($numPages > 1) { |
||
| 134 | if ($currentPage > 1) { |
||
| 135 | $pages .= '<a href="result.php?f=' . $f . '&l=' . $l . '&w=' . $whe . '&o=' . $o . '&d=' . $d . '&st=' . ($st - $perPage) . '">' . _MA_PEDIGREE_PREVIOUS . '</a> '; |
||
| 136 | } |
||
| 137 | |||
| 138 | //create numbers |
||
| 139 | for ($x = 1; $x < ($numPages + 1); ++$x) { |
||
| 140 | //create line break after 20 number |
||
| 141 | if (0 == ($x % 20)) { |
||
| 142 | $pages .= '<br>'; |
||
| 143 | } |
||
| 144 | if ($x != $currentPage) { |
||
| 145 | $pages .= '<a href="result.php?f=' . $f . '&l=' . $l . '&w=' . $whe . '&o=' . $o . '&d=' . $d . '&st=' . ($perPage * ($x - 1)) . '">' . $x . '</a> '; |
||
| 146 | } else { |
||
| 147 | $pages .= '<b>' . $x . '</b>  '; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | //create next button |
||
| 152 | if ($numPages > 1) { |
||
| 153 | if ($currentPage < $numPages) { |
||
| 154 | $pages .= '<a href="result.php?f=' . $f . '&l=' . $l . '&w=' . $whe . '&o=' . $o . '&d=' . $d . '&st=' . ($st + $perPage) . '">' . _MA_PEDIGREE_NEXT . '</a>  '; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | //query |
||
| 159 | $queryString = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' WHERE ' . $f . ' ' . $l . " '" . $w . "' ORDER BY " . $o . ' ' . $d . ' LIMIT ' . $st . ', ' . $perPage; |
||
| 160 | $result = $GLOBALS['xoopsDB']->query($queryString); |
||
| 161 | |||
| 162 | $animal = new Pedigree\Animal(); |
||
| 163 | //test to find out how many user fields there are... |
||
| 164 | $fields = $animal->getNumOfFields(); |
||
| 165 | $fieldsCount = count($fields); |
||
| 166 | $numofcolumns = 1; |
||
| 167 | $columns = [['columnname' => 'Name']]; |
||
| 168 | foreach ($fields as $i => $iValue) { |
||
| 169 | $userField = new Pedigree\Field($fields[$i], $animal->getConfig()); |
||
| 170 | $fieldType = $userField->getSetting('FieldType'); |
||
| 171 | $fieldObject = new $fieldType($userField, $animal); |
||
| 172 | //create empty string |
||
| 173 | if ($userField->isActive() && $userField->inList()) { |
||
| 174 | if ($userField->hasLookup()) { |
||
| 175 | $lookupValues = $userField->lookupField($fields[$i]); |
||
| 176 | } else { |
||
| 177 | $lookupValues = ''; |
||
| 178 | } |
||
| 179 | /* print_r($lookupValues); //debug information */ |
||
| 180 | $columns[] = [ |
||
| 181 | 'columnname' => $fieldObject->fieldname, |
||
| 182 | 'columnnumber' => $userField->getId(), |
||
| 183 | 'lookupval' => $lookupValues |
||
| 184 | ]; |
||
| 185 | ++$numofcolumns; |
||
| 186 | unset($lookupValues); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $pathIcon16 = \Xmf\Module\Admin::iconUrl('', 16); |
||
| 191 | |||
| 192 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
| 193 | //reset $gender |
||
| 194 | $gender = ''; |
||
| 195 | if ((!empty($GLOBALS['xoopsUser']) && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) |
||
| 196 | && (($row['user'] == $xoopsUser->getVar('uid')) || (true === $modadmin))) { |
||
| 197 | $gender = "<a href='dog.php?id={$row['id']}'><img src=" . $pathIcon16 . '/edit.png alt=' . _EDIT . "'></a> |
||
| 198 | <a href='delete.php?id={$row['id']}'><img src=" . $pathIcon16 . '/delete.png alt=' . _DELETE . "'></a>"; |
||
| 199 | } |
||
| 200 | if (0 == $row['roft']) { |
||
| 201 | $gender .= "<img src='assets/images/male.gif'>"; |
||
| 202 | } else { |
||
| 203 | $gender .= "<img src='assets/images/female.gif'>"; |
||
| 204 | } |
||
| 205 | |||
| 206 | // $camera = ('' != $row['foto']) ? " <img src='" . PEDIGREE_UPLOAD_URL . "/images/dog-icon25.png'>" : ''; |
||
| 207 | if ('' != $row['foto']) { |
||
| 208 | $camera = ' <img src="assets/images/camera.png">'; |
||
| 209 | } else { |
||
| 210 | $camera = ''; |
||
| 211 | } |
||
| 212 | |||
| 213 | $name = stripslashes($row['naam']) . $camera; |
||
| 214 | //empty array |
||
| 215 | unset($columnvalue); |
||
| 216 | //fill array |
||
| 217 | for ($i = 1; $i < $numofcolumns; ++$i) { |
||
| 218 | $x = 'user' . $columns[$i]['columnnumber']; |
||
| 219 | //echo $x."columnnumber"; |
||
| 220 | if (is_array($columns[$i]['lookupval'])) { |
||
| 221 | foreach ($columns[$i]['lookupval'] as $key => $keyValue) { |
||
| 222 | if ($keyValue['id'] == $row[$x]) { |
||
| 223 | //echo "key:".$row['user5']."<br>"; |
||
| 224 | $value = $keyValue['value']; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | //debug information |
||
| 228 | ///echo $columns[$i]['columnname']."is an array !"; |
||
| 229 | } //format value - cant use object because of query count |
||
| 230 | elseif (0 === strncmp($row[$x], 'http://', 7)) { |
||
| 231 | $value = "<a href='{$row[$x]}'>{$row[$x]}</a>"; |
||
| 232 | } else { |
||
| 233 | $value = $row[$x]; |
||
| 234 | } |
||
| 235 | if (isset($value)) { |
||
| 236 | $columnvalue[] = ['value' => $value]; |
||
| 237 | unset($value); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | $animals[] = [ |
||
| 241 | 'id' => $row['id'], |
||
| 242 | 'name' => $name, |
||
| 243 | 'gender' => $gender, |
||
| 244 | 'link' => "<a href='pedigree.php?pedid={$row['id']}'>{$name}</a>", |
||
| 245 | 'colour' => '', |
||
| 246 | 'number' => '', |
||
| 247 | 'usercolumns' => isset($columnvalue) ? $columnvalue : 0 |
||
| 248 | ]; |
||
| 249 | } |
||
| 250 | |||
| 251 | //add data to smarty template |
||
| 252 | //assign dog |
||
| 253 | $GLOBALS['xoopsTpl']->assign([ |
||
| 254 | 'dogs' => $animals, |
||
| 255 | 'columns' => $columns, |
||
| 256 | 'numofcolumns' => $numofcolumns, |
||
| 257 | 'tsarray' => Pedigree\Utility::sortTable($numofcolumns) |
||
| 258 | ]); |
||
| 259 | //assign links |
||
| 260 | |||
| 261 | //find last shown number |
||
| 262 | if (($st + $perPage) > $numResults) { |
||
| 263 | $lastshown = $numResults; |
||
| 264 | } else { |
||
| 265 | $lastshown = $st + $perPage; |
||
| 266 | } |
||
| 267 | //create string |
||
| 268 | $matches = strtr(_MA_PEDIGREE_MATCHES, ['[animalTypes]' => $moduleConfig['animalTypes']]); |
||
| 269 | $nummatchstr = "{$numResults}{$matches}" . ($st + 1) . " - {$lastshown} ({$numPages} pages)"; |
||
| 270 | $GLOBALS['xoopsTpl']->assign('nummatch', $nummatchstr); |
||
| 271 | $GLOBALS['xoopsTpl']->assign('pages', $pages); |
||
| 272 | |||
| 273 | //comments and footer |
||
| 274 | include $GLOBALS['xoops']->path('footer.php'); |
||
| 275 |