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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
<?php
/**
***********************************************************************************************
* Class manages access to database table adm_categories
*
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class TableCategory
* Diese Klasse dient dazu einen Kategorieobjekt zu erstellen.
* Eine Kategorieobjekt kann ueber diese Klasse in der Datenbank verwaltet werden
*
* Beside the methods of the parent class there are the following additional methods:
*
* getNewNameIntern($name, $index) - diese rekursive Methode ermittelt fuer den
* uebergebenen Namen einen eindeutigen Namen dieser bildet sich
* aus dem Namen in Grossbuchstaben und der naechsten freien Nummer
* getNumberElements() - number of child recordsets
* moveSequence($mode) - Kategorie wird um eine Position in der Reihenfolge verschoben
*/
class TableCategory extends TableAccess
{
protected $elementTable;
protected $elementColumn;
/**
* Constructor that will create an object of a recordset of the table adm_category.
* If the id is set than the specific category will be loaded.
* @param \Database $database Object of the class Database. This should be the default global object @b $gDb.
* @param int $cat_id The recordset of the category with this id will be loaded. If id isn't set than an empty object of the table is created.
*/
public function __construct(&$database, $cat_id = 0)
{
parent::__construct($database, TBL_CATEGORIES, 'cat', $cat_id);
}
/**
* Deletes the selected record of the table and all references in other tables.
* After that the class will be initialize. The method throws exceptions if
* the category couldn't be deleted.
* @throws AdmException SYS_DELETE_SYSTEM_CATEGORY
* CAT_DONT_DELETE_CATEGORY
* SYS_DELETE_LAST_CATEGORY
* @return bool @b true if no error occurred
*/
public function delete()
{
global $gCurrentSession;
// system-category couldn't be deleted
if($this->getValue('cat_system') == 1)
{
throw new AdmException('SYS_DELETE_SYSTEM_CATEGORY');
}
// checks if there exists another category of this type. Don't delete the last category of a type!
$sql = 'SELECT COUNT(*) AS count
FROM '.TBL_CATEGORIES.'
WHERE ( cat_org_id = '. $gCurrentSession->getValue('ses_org_id'). '
OR cat_org_id IS NULL )
AND cat_type = \''. $this->getValue('cat_type'). '\'';
$categoriesStatement = $this->db->query($sql);
if($categoriesStatement->fetchColumn() > 1)
{
$this->db->startTransaction();
// Luecke in der Reihenfolge schliessen
$sql = 'UPDATE '.TBL_CATEGORIES.' SET cat_sequence = cat_sequence - 1
WHERE ( cat_org_id = '. $gCurrentSession->getValue('ses_org_id'). '
OR cat_org_id IS NULL )
AND cat_sequence > '. $this->getValue('cat_sequence'). '
AND cat_type = \''. $this->getValue('cat_type'). '\'';
$this->db->query($sql);
// alle zugehoerigen abhaengigen Objekte suchen und mit weiteren Abhaengigkeiten loeschen
$sql = 'SELECT *
FROM '.$this->elementTable.'
WHERE '.$this->elementColumn.' = '. $this->getValue('cat_id');
$recordsetsStatement = $this->db->query($sql);
if($recordsetsStatement->rowCount() > 0)
{
throw new AdmException('CAT_DONT_DELETE_CATEGORY', $this->getValue('cat_name'), $this->getNumberElements());
}
$return = parent::delete();
$this->db->endTransaction();
return $return;
}
// Don't delete the last category of a type!
throw new AdmException('SYS_DELETE_LAST_CATEGORY');
}
/**
* diese rekursive Methode ermittelt fuer den uebergebenen Namen einen eindeutigen Namen
* dieser bildet sich aus dem Namen in Grossbuchstaben und der naechsten freien Nummer (index)
* Beispiel: 'Gruppen' => 'GRUPPEN_2'
* @param string $name
* @param int $index
* @return string
*/
private function getNewNameIntern($name, $index)
{
$newNameIntern = strtoupper(str_replace(' ', '_', $name));
if($index > 1)
{
$newNameIntern = $newNameIntern.'_'.$index;
}
$sql = 'SELECT cat_id
FROM '.TBL_CATEGORIES.'
WHERE cat_name_intern = \''.$newNameIntern.'\'';
$categoriesStatement = $this->db->query($sql);
if($categoriesStatement->rowCount() > 0)
{
++$index;
$newNameIntern = $this->getNewNameIntern($name, $index);
}
return $newNameIntern;
}
/**
* Read number of child recordsets of this category.
* @return int Returns the number of child elements of this category
*/
public function getNumberElements()
{
$sql = 'SELECT COUNT(*) AS count
FROM '.$this->elementTable.'
WHERE '.$this->elementColumn.' = '. $this->getValue('cat_id');
$elementsStatement = $this->db->query($sql);
return (int) $elementsStatement->fetchColumn();
}
/**
* Get the value of a column of the database table.
* If the value was manipulated before with @b setValue than the manipulated value is returned.
* @param string $columnName The name of the database column whose value should be read
* @param string $format For date or timestamp columns the format should be the date/time format e.g. @b d.m.Y = '02.04.2011'. @n
* For text columns the format can be @b database that would return the original database value without any transformations
* @return int|string|bool Returns the value of the database column.
* If the value was manipulated before with @b setValue than the manipulated value is returned.
*/
public function getValue($columnName, $format = '')
{
global $gL10n;
if($columnName === 'cat_name_intern')
{
// internal name should be read with no conversion
$value = parent::getValue($columnName, 'database');
}
else
{
$value = parent::getValue($columnName, $format);
}
// if text is a translation-id then translate it
if($columnName === 'cat_name' && $format !== 'database' && strpos($value, '_') === 3)
{
$value = $gL10n->get(admStrToUpper($value));
}
return $value;
}
/**
* Change the internal sequence of this category. It can be moved one place up or down
* @param string $mode This could be @b UP or @b DOWN.
*/
public function moveSequence($mode)
{
global $gCurrentOrganization;
// count all categories that are organization independent because these categories should not
// be mixed with the organization categories. Hidden categories are sidelined.
$sql = 'SELECT COUNT(*) AS count
FROM '.TBL_CATEGORIES.'
WHERE cat_type = \''. $this->getValue('cat_type'). '\'
AND cat_name_intern NOT LIKE \'CONFIRMATION_OF_PARTICIPATION\'
AND cat_org_id IS NULL ';
$countCategoriesStatement = $this->db->query($sql);
$rowCount = $countCategoriesStatement->fetchColumn();
// die Kategorie wird um eine Nummer gesenkt und wird somit in der Liste weiter nach oben geschoben
if(admStrToUpper($mode) === 'UP')
{
if($this->getValue('cat_org_id') == 0 || $this->getValue('cat_sequence') > $rowCount + 1)
{
$sql = 'UPDATE '.TBL_CATEGORIES.' SET cat_sequence = '.$this->getValue('cat_sequence').'
WHERE cat_type = \''. $this->getValue('cat_type'). '\'
AND ( cat_org_id = '. $gCurrentOrganization->getValue('org_id'). '
OR cat_org_id IS NULL )
AND cat_sequence = '.$this->getValue('cat_sequence').' - 1 ';
$this->db->query($sql);
$this->setValue('cat_sequence', $this->getValue('cat_sequence')-1);
$this->save();
}
}
// die Kategorie wird um eine Nummer erhoeht und wird somit in der Liste weiter nach unten geschoben
elseif(admStrToUpper($mode) === 'DOWN')
{
if($this->getValue('cat_org_id') > 0 || $this->getValue('cat_sequence') < $rowCount)
{
$sql = 'UPDATE '.TBL_CATEGORIES.' SET cat_sequence = '.$this->getValue('cat_sequence').'
WHERE cat_type = \''. $this->getValue('cat_type'). '\'
AND ( cat_org_id = '. $gCurrentOrganization->getValue('org_id'). '
OR cat_org_id IS NULL )
AND cat_sequence = '.$this->getValue('cat_sequence').' + 1 ';
$this->db->query($sql);
$this->setValue('cat_sequence', $this->getValue('cat_sequence')+1);
$this->save();
}
}
}
/**
* Reads a category out of the table in database selected by the unique category id in the table.
* Per default all columns of adm_categories will be read and stored in the object.
* @param int $cat_id Unique cat_id
* @return bool Returns @b true if one record is found
*/
public function readDataById($cat_id)
{
global $g_tbl_praefix;
$returnValue = parent::readDataById($cat_id);
if($returnValue)
{
switch ($this->getValue('cat_type'))
{
case 'ROL':
$this->elementTable = TBL_ROLES;
$this->elementColumn = 'rol_cat_id';
break;
case 'LNK':
$this->elementTable = TBL_LINKS;
$this->elementColumn = 'lnk_cat_id';
break;
case 'USF':
$this->elementTable = TBL_USER_FIELDS;
$this->elementColumn = 'usf_cat_id';
break;
case 'DAT':
$this->elementTable = TBL_DATES;
$this->elementColumn = 'dat_cat_id';
break;
case 'AWA':
$this->elementTable = $g_tbl_praefix.'_user_awards';
$this->elementColumn = 'awa_cat_id';
break;
}
}
return $returnValue;
}
/**
* Reads a category out of the table in database selected by different columns in the table.
* The columns are commited with an array where every element index is the column name and the value is the column value.
* The columns and values must be selected so that they identify only one record.
* If the sql will find more than one record the method returns @b false.
* Per default all columns of adm_categories will be read and stored in the object.
* @param array $columnArray An array where every element index is the column name and the value is the column value
* @return bool Returns @b true if one record is found
*/
public function readDataByColumns(array $columnArray)
{
$returnValue = parent::readDataByColumns($columnArray);
if($returnValue)
{
switch ($this->getValue('cat_type'))
{
case 'ROL':
$this->elementTable = TBL_ROLES;
$this->elementColumn = 'rol_cat_id';
break;
case 'LNK':
$this->elementTable = TBL_LINKS;
$this->elementColumn = 'lnk_cat_id';
break;
case 'USF':
$this->elementTable = TBL_USER_FIELDS;
$this->elementColumn = 'usf_cat_id';
break;
case 'DAT':
$this->elementTable = TBL_DATES;
$this->elementColumn = 'dat_cat_id';
break;
}
}
return $returnValue;
}
/**
* Save all changed columns of the recordset in table of database. Therefore the class remembers if it's
* a new record or if only an update is necessary. The update statement will only update
* the changed columns. If the table has columns for creator or editor than these column
* with their timestamp will be updated.
* If a new record is inserted than the next free sequence will be determined.
* @param bool $updateFingerPrint Default @b true. Will update the creator or editor of the recordset if table has columns like @b usr_id_create or @b usr_id_changed
* @return bool If an update or insert into the database was done then return true, otherwise false.
*/
public function save($updateFingerPrint = true)
{
global $gCurrentOrganization, $gCurrentSession;
$fields_changed = $this->columnsValueChanged;
$this->db->startTransaction();
if($this->new_record)
{
if($this->getValue('cat_org_id') > 0)
{
$org_condition = ' AND ( cat_org_id = '. $gCurrentOrganization->getValue('org_id'). '
OR cat_org_id IS NULL ) ';
}
else
{
$org_condition = ' AND cat_org_id IS NULL ';
}
// beim Insert die hoechste Reihenfolgennummer der Kategorie ermitteln
$sql = 'SELECT COUNT(*) AS count
FROM '.TBL_CATEGORIES.'
WHERE cat_type = \''. $this->getValue('cat_type'). '\'
'.$org_condition;
$countCategoriesStatement = $this->db->query($sql);
$this->setValue('cat_sequence', $countCategoriesStatement->fetchColumn() + 1);
if($this->getValue('cat_org_id') == 0)
{
// eine Orga-uebergreifende Kategorie ist immer am Anfang, also Kategorien anderer Orgas nach hinten schieben
$sql = 'UPDATE '.TBL_CATEGORIES.' SET cat_sequence = cat_sequence + 1
WHERE cat_type = \''. $this->getValue('cat_type'). '\'
AND cat_org_id IS NOT NULL ';
$this->db->query($sql);
}
}
// if new category than generate new name intern, otherwise no change will be made
if($this->new_record)
{
$this->setValue('cat_name_intern', $this->getNewNameIntern($this->getValue('cat_name'), 1));
}
$returnValue = parent::save($updateFingerPrint);
// Nach dem Speichern noch pruefen, ob Userobjekte neu eingelesen werden muessen,
if($fields_changed && $this->getValue('cat_type') === 'USF' && is_object($gCurrentSession))
{
// all active users must renew their user data because the user field structure has been changed
$gCurrentSession->renewUserObject();
}
$this->db->endTransaction();
return $returnValue;
}
/**
* Set a new value for a column of the database table.
* The value is only saved in the object. You must call the method @b save to store the new value to the database
* @param string $columnName The name of the database column whose value should get a new value
* @param mixed $newValue The new value that should be stored in the database field
* @param bool $checkValue The value will be checked if it's valid. If set to @b false than the value will not be checked.
* @return bool Returns @b true if the value is stored in the current object and @b false if a check failed
*/
public function setValue($columnName, $newValue, $checkValue = true)
{
global $gCurrentOrganization;
// Systemkategorien duerfen nicht umbenannt werden
if($columnName === 'cat_name' && $this->getValue('cat_system') == 1)
{
return false;
}
elseif($columnName === 'cat_default' && $newValue == '1')
{
// es darf immer nur eine Default-Kategorie je Bereich geben
$sql = 'UPDATE '.TBL_CATEGORIES.' SET cat_default = 0
WHERE cat_type = \''. $this->getValue('cat_type'). '\'
AND ( cat_org_id IS NOT NULL
OR cat_org_id = '.$gCurrentOrganization->getValue('org_id').')';
$this->db->query($sql);
}
return parent::setValue($columnName, $newValue, $checkValue);
}
}