All pastes #986063 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-04-15 14:27 UTC
rendered paste body
		function getItems( $config, $alleVelden = false, $filter = array(), $sorteer = array()) {
			// Zoek uit welke velden getoond moeten worden
			if ( count( $config ) == 0 ) {
				return array();
			}
			
			$velden = array( $config[ 'primary_key' ] );
			
			// Bij gewone items is er ook een timestamp
			// Dan is de categorie_id ook 'id' en iig niet 'cat_id'

			// Timestamps in mysql worden teruggegeven als YmdHis
			// Die datum moet wordne omgezet naar normale timestamps
			if ( $config[ 'primary_key' ] != "cat_id" ) {
				$velden[] = " UNIX_TIMESTAMP( `_timestamp` ) as _timestamp ";
			}
			
			$velden[] = " _rang ";
			
			foreach( $config[ 'velden' ] as $veld => $info ) {
				if ( $alleVelden || !isset( $info[ 'tonen' ] ) || $info[ 'tonen' ] == 1 ) {
					$velden[] = $veld;
				}
			}

			// Bouw de SQL query op
			$sql = "SELECT " . implode( ", ", $velden ) . " FROM " . $config[ 'tabel' ];
			
			$filterArr = array( " 1 = 1 " );
			$velden = array();
			foreach( $filter as $veld => $waarde ) {
				$filterArr[] = " `$veld` = ? ";
				$velden[] = $waarde;
			}
			
			$sql .= " WHERE " . implode( " AND ", $filterArr );
			
			// Sorteer de gegevens
			if(is_array($sorteer) && count($sorteer) > 0) {
				$sql .= " ORDER BY ". implode(", ", $sorteer);
			}
			else {
				if ( isset( $config[ "zelf_sorteren" ] ) && $config[ "zelf_sorteren" ] != "" ) {
					$sql .= " ORDER BY _rang";
				} elseif ( isset( $config[ "sorteren" ] ) && trim( $config[ "sorteren" ] ) != "" && !is_numeric( $config[ "sorteren" ] ) ) {
					$sql .= " ORDER BY `" . $config[ "sorteren" ] . "`";
					
					if (isset($config["sort_type"])) {
						$sql .= " ".$config["sort_type"];
					}
				}
			}
			
			// Voer de query uit
			$q = $this->dba->db->query( $sql, $velden );
			
			if ( DB::isError( $q ) ) {
				raiseError( "De gegevens kunnen niet uit de database worden gelezen: " . $q->getMessage(), __FILE__, __LINE__, F_ERROR );
			}
			
			// Lees de gegevens uit
			$items = array();
			
			while( $item = $q->fetchRow() ) {
				$item = $this->prepareFieldValuesForShow($config, $item);
				$items[] = $item;
			}

			return $items;
		}

























function moveItem( $config, $item_id, $difference ) {
			// Zoek het huidige item op
			$current_item = $this->getItem( $config, $item_id );
			
			$categorie_id = $parent_id = -1;
			
			// Check of het item in een categorie zit
			if ( array_key_exists( "categorie", $current_item ) ) {
				$categorie_id = $current_item[ "categorie" ];
			} elseif ( array_key_exists( "parent", $current_item ) ) {
				// Het is van subcategorieen binnen een categorie
				$parent_id = $current_item[ 'parent' ];
			}
			
			//haal alle items op die nodig zijn om de sortering te kunnen doen
			$items = array();
			
			//order de items bij de huidige _rang
			$orderby = array(); 
			$orderby[] = "_rang ASC";
			
			if ( $categorie_id != -1 ) {
				$items = $this->getItems( $config, true, array("categorie" => $categorie_id), $orderby );
			} elseif( $parent_id != -1 )  {
				$items = $this->getItems( $config, true, array("parent" => $parent_id), $orderby );
			} else {
				$items = $this->getItems( $config, true, null, $orderby );
			}
			
			//voor het geval het eerste item omhoog word gedaan //dat willen we natuurlijk niet
			if($difference < 0 && $items[0][$config['primary_key']] == $current_item[$config['primary_key']]) {
				return false;
			}
			
			//voor het geval het laatste item omlaag word gedaan //dat willen we natuurlijk ook niet
			if($difference > 0 && $items[count($items) - 1][$config['primary_key']] == $current_item[$config['primary_key']]) {
				return false;
			}
			
			//voor goede ordering moeten we de huidige plek van het item weten 
			//(Voor het geval er gedelete items, gewijzigde cats of iets dergelijks tussen zit)
			$current_place = 0;
			for($i = 0; $i < count($items); $i++) {
				if($items[$i][$config['primary_key']] == $current_item[$config['primary_key']]) {
					$current_place = $i + 1;
					break;
				}
			}
			
			//corrigeer eventueel de _rang en voeg ze op de goede volgorde toe
			$correction = 0;
			while(list($cnt, $item) = each($items)) {

				//kijken of dit het huidige item is
				if($item[$config['primary_key']] == $current_item[$config['primary_key']]) {
					$correction--;
					continue;				
				}
			
				//kijken of dit de nieuwe plek word van het huidige item
				if($cnt + 1 == $current_place + $difference) {
					//zo ja voeg ze op de goede volgorde toe
					if($difference > 0) {
						//eerst normale item
						$sth = $this->dba->db->prepare("UPDATE " . $config[ 'tabel' ] . " SET _rang = ? WHERE " . $config[ "primary_key" ] . 

" = ?");
						$dbh1 = $this->dba->db->execute( $sth, array( $cnt + 1 + $correction, $item[$config["primary_key"]] ) );		

						

						$correction++;			
						
						//dan het huidige item
						$sth = $this->dba->db->prepare("UPDATE " . $config[ 'tabel' ] . " SET _rang = ? WHERE " . $config[ "primary_key" ] . 

" = ?");
						$dbh1 = $this->dba->db->execute( $sth, array( $cnt + 1 + $correction, $current_item[$config["primary_key"]] ) );	

					
					}
					else {
						//eerst het huidige item
						$sth = $this->dba->db->prepare("UPDATE " . $config[ 'tabel' ] . " SET _rang = ? WHERE " . $config[ "primary_key" ] . 

" = ?");
						$dbh1 = $this->dba->db->execute( $sth, array( $cnt + 1 + $correction, $current_item[$config["primary_key"]] ) );

						$correction++;		
											
						//dan normale item
						$sth = $this->dba->db->prepare("UPDATE " . $config[ 'tabel' ] . " SET _rang = ? WHERE " . $config[ "primary_key" ] . 

" = ?");
						$dbh1 = $this->dba->db->execute( $sth, array( $cnt + 1 + $correction, $item[$config["primary_key"]] ) );		

						
					}
					continue;
				}
				
				//als er nx speciaals gebeurt update het gewoon normaal (eventueel zelf correctie)
				$sth = $this->dba->db->prepare("UPDATE " . $config[ 'tabel' ] . " SET _rang = ? WHERE " . $config[ "primary_key" ] . " = ?");
				$dbh1 = $this->dba->db->execute( $sth, array( $cnt + 1 + $correction, $item[$config["primary_key"]] ) );
			}
			
			return true;
		}