class_mcPUIX_PDO.inc.php 13.4 KB
<?
class mcPUIX_PDO {

	/**
	 * @param string $param_tablename
	 * @param array $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]
	 * @param array $param_NewArray => $param_NewArray[mcPUIX::DBUIArray] = array()
	 * @return array 
	 */
	public static function prepareArrayForDBOperation($param_tablename, array $param_ConfigArray, array $param_NewArray=null /*, array $param_OriginalArray=null*/) {
		
		if(mcArray::checkKeyHasFullArray($param_ConfigArray, mcPUIX::DBUIArray, mcPUIX::DBUIConfig, $param_tablename)
			&& mcArray::checkKeyHasFullArray($param_NewArray, mcPUIX::DBUIArray) ) {
			
//			D::show(array($param_NewArray[mcPUIX::DBUIArray], $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]), $param_tablename. ' for ');
			
			$ConfigArray = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename];
			$result = array(mcPUIX::DBUIArray => array(mcPUIX::PDOToSaveValues => array($param_tablename => $result)));
			
			
			$DeleteRows = $NewRows = $UpdateRows = $ErrorRows = array();

			if(array_key_exists('TABLE', $ConfigArray)) {
				$BoolRequired = $BoolPrimary = $BoolActiveRow = false;

				if(mcArray::checkKeyHasFullArray($ConfigArray, mcPUIX::FORM_DO_ROW)) {
					$BoolActiveRow = true;
				}				

				if(array_key_exists('REQUIRED', $ConfigArray['TABLE']) && is_array($ConfigArray['TABLE']['REQUIRED'])) {
					$BoolRequired = true;
				}
				if(array_key_exists('PRIMARY', $ConfigArray['TABLE']) && is_array($ConfigArray['TABLE']['PRIMARY'])) {
					$BoolPrimary = true;
				}

				foreach($param_NewArray[mcPUIX::DBUIArray] as $NewKey =>  &$NewData) {
//					D::show($NewData, $NewKey. ' for ');
					if($BoolRequired === true) {
						foreach($ConfigArray['TABLE']['REQUIRED'] as $RequiredKey) {
							if(!array_key_exists($RequiredKey, $NewData) || !$NewData[$RequiredKey]) {
								$ErrorRows[] = $param_NewArray[mcPUIX::DBUIArray][$NewKey];
								unset($param_NewArray[mcPUIX::DBUIArray][$NewKey]);
								break;
							} 
						}
					}

//					if( 'NEW_ROW' === $NewKey) {
					if($NewData[mcPUIX::FORM_DO_ROW] === mcPUIX::FORM_DO_NEW) {
						$BoolErrorRow = false;
						foreach($ConfigArray['TABLE']['LISTID'] as $LISTID) {
							if(!array_key_exists($LISTID, $NewData)) {
								$ErrorRows[] = $param_NewArray[mcPUIX::DBUIArray][$NewKey];
								$BoolErrorRow = true;
								break;
							}
						}
						if($BoolErrorRow === false) {
							$NewRows[] = $param_NewArray[mcPUIX::DBUIArray][$NewKey];

						}						
//						D::show($NewData, 'NEW_ROW '.$NewKey);
						
					}
					
					if($BoolActiveRow === true) {
//						if(!array_key_exists(mcPUIX::FORM_DO_ROW, $NewData) || !$NewData[mcPUIX::FORM_DO_ROW]) {
						if($NewData[mcPUIX::FORM_DO_ROW] === mcPUIX::FORM_DO_DELETE) {
							$DeleteRows[] = $param_NewArray[mcPUIX::DBUIArray][$NewKey];
							unset($param_NewArray[mcPUIX::DBUIArray][$NewKey]);

//						} else if(array_key_exists($NewKey, $param_NewArray[mcPUIX::DBUIArray])) {
						} else if($NewData[mcPUIX::FORM_DO_ROW] === mcPUIX::FORM_DO_SAVE) {
							$BoolErrorRow = false;
							foreach($ConfigArray['TABLE']['PRIMARY'] as $PrimaryKey) {
								if(!array_key_exists($PrimaryKey, $NewData)) {
									$ErrorRows[] = $param_NewArray[mcPUIX::DBUIArray][$NewKey];
									$BoolErrorRow = true;
									break;
								}
							}
							if($BoolErrorRow == false) {
								$UpdateRows[] = $param_NewArray[mcPUIX::DBUIArray][$NewKey];

							}
							
							if(mcArray::checkKeyHasFullArray($NewData, mcPUIX::DBUIArray)) {
								$Tablename_LIST = mcPUIX_CONFIG::getReferenceTableName($param_tablename);
								
								/**
								 * 
								 * 
								 * QQQQQQQQQQQQQQ hier muss code hin
								 */
//								D::show($param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$Tablename_LIST], $Tablename_LIST.'  CONFIG');
//								D::show($NewData[mcPUIX::DBUIArray], '1 '.$Tablename_LIST);
//								D::cf('1 '.$Tablename_LIST);
								$result_List = mcPUIX_PDO::prepareArrayForDBOperation($Tablename_LIST, $param_ConfigArray, $NewData);
//								D::show($result_List, '2 '.$Tablename_LIST);
//								$result[mcPUIX::DBUIArray][mcPUIX::PDOToSaveValues][$Tablename_LIST] = $result_List[mcPUIX::DBUIArray][mcPUIX::PDOToSaveValues][$Tablename_LIST];
								$result = array_merge_recursive ($result, $result_List);
//								D::show($result_List, $Tablename_LIST. '##dddddddddddddddddddd ddddddd');
							}
						}
					} else if($NewData[mcPUIX::FORM_DO_ROW] === mcPUIX::FORM_DO_SAVE) {
						$UpdateRows[] = $param_NewArray[mcPUIX::DBUIArray][$NewKey];
//						break;
					}
				}

			}
			

			$result[mcPUIX::DBUIArray][mcPUIX::PDOToSaveValues][$param_tablename]['DeleteRows']	= $DeleteRows;
			$result[mcPUIX::DBUIArray][mcPUIX::PDOToSaveValues][$param_tablename]['UpdateRows']	= $UpdateRows;
			$result[mcPUIX::DBUIArray][mcPUIX::PDOToSaveValues][$param_tablename]['ErrorRows']	= $ErrorRows;
			$result[mcPUIX::DBUIArray][mcPUIX::PDOToSaveValues][$param_tablename]['NewRows']		= $NewRows;
//D::show($result, '$result');
			return $result;			
		}
		

	}	
	
	/**
	 *	return loaded PDO Table as DBUIArray
	 * 
	 * @param Account $param_HostAccount
	 * @param PDOLevel $param_iStorable
	 * @param string $param_tablename
	 * @param array $param_SelectArray
	 * @return array(
	 *			mcPUIX::DBUIArray => array(
	 *					mcPUIX::PDOValues => array(0=>val1, 1=>val2)
	 *				)
	 *			)
	 */
	public static function getTableAsDBUIArray(Account &$param_HostAccount, PDOLevel &$param_iStorable, $param_tablename, $param_SQLWhere='', $param_depth=0) {
		$ConfigArray = mcPUIX_CONFIG::getTableConfig($param_tablename);
		$ConfigArray = $ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename];

		if(mcArray::checkKeyHasFullArray($ConfigArray, 'TABLE')
			&& array_key_exists('TABLENAME', $ConfigArray['TABLE']) && $ConfigArray['TABLE']['TABLENAME']
		) {
			$var_query = 'select * from '.$ConfigArray['TABLE']['TABLENAME'].' '.$param_SQLWhere;
//				where SNIfProfileID = 0 or SNIfProfileID = :SNIfProfileID ';
//			$var_bindValueArray = array(':SNIfProfileID'	=>	$param_Object->getObjVar('Profile')->getObjVar('SNIfProfileID'),	);
//			D::cf(''.$var_query);
			$stmt = $param_iStorable->PDO_query($var_query, $var_bindValueArray);

			if($stmt){
				$PDOTable = $param_iStorable->PDO_fetchAll($stmt);
				
				if($param_depth>0) {
					if(mcArray::checkKeyHasFullArray($ConfigArray['TABLE'], 'LIST_TABLE')) {
						if($ConfigArray['TABLE']['LIST_TABLE']['TABLENAME'] && $ConfigArray['TABLE']['LIST_TABLE']['REFERENCE_KEY']) {

							foreach($PDOTable as &$PDORow) {
								if($PDORow[$ConfigArray['TABLE']['LIST_TABLE']['REFERENCE_KEY']]) {
	//								D::show($PDORow, '$PDORow');
			//						D::show($ConfigArray, '$ConfigArray');			
									$SQLWhere = ' where '.$ConfigArray['TABLE']['LIST_TABLE']['REFERENCE_KEY'].'='.$PDORow[$ConfigArray['TABLE']['LIST_TABLE']['REFERENCE_KEY']];
									$PDOTableList = mcPUIX_PDO::getTableAsDBUIArray($param_HostAccount, $param_iStorable, $ConfigArray['TABLE']['LIST_TABLE']['TABLENAME'], $SQLWhere, $param_depth-1);
									if(mcArray::checkKeyHasFullArray($PDOTableList, mcPUIX::DBUIArray, mcPUIX::PDOValues)) {
										$PDORow[mcPUIX::DBUIArray] = $PDOTableList[mcPUIX::DBUIArray];
									}
									
	//								D::show($PDOTableList, '$PDOTableList: '.$SQLWhere);								
								}
							}


						}
					}					
				}

				

				
				$result = array(
					mcPUIX::DBUIArray => array(
						mcPUIX::PDOValues => $PDOTable
					)
				);
//				D::show($result, $var_query);
				return $result;
			} else {
				return false;
			}			
		}
	}
	
	public static function getDatabaseTable_where($param_PrimaryKeys, $param_PrimaryValues, &$param_query, array &$param_bindValueArray) {
		$param_query .= ' where ';
		foreach($param_PrimaryKeys as $Pkey =>  $PrimaryKey ) {
			if(array_key_exists($PrimaryKey, $param_PrimaryValues)){
				if($Pkey>0) {
					$param_query .= ' and ';
				}
				$param_query .= ' '.$PrimaryKey.'=:'.$PrimaryKey.'_'.$param_PrimaryValues[$PrimaryKey];
				$param_bindValueArray[':'.$PrimaryKey.'_'.$param_PrimaryValues[$PrimaryKey]] = $param_PrimaryValues[$PrimaryKey];							
			} else {
				return Error::newError('DevError',"not enough primary values for keys ",$param_query);
			}
		}		
		return true;
	}
	
	public static function updateDatabaseTable(Account &$param_HostAccount, PDOLevel &$param_iStorable, array $param_ConfigArray, array $param_PreparedArray=null ) {
//		[mcPUIX::DBUIArray][mcPUIX::PDOToSaveValues][$param_tablename]
//		[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]
//		D::show($param_PreparedArray,'##########updateDatabaseTable:  $param_ConfigArray');		
//		D::show($param_ConfigArray, '$param_ConfigArray');
//		D::show($param_PreparedArray, '$param_PreparedArray: ');
		if(mcArray::checkKeyHasFullArray($param_PreparedArray, mcPUIX::DBUIArray, mcPUIX::PDOToSaveValues)) {
//			D::ulli($tablename.'   before');
			foreach($param_PreparedArray[mcPUIX::DBUIArray][mcPUIX::PDOToSaveValues] as $tablename => $preparedArray) {
//D::show($preparedArray, '$tablename: '.$tablename);

				if(mcArray::checkKeyHasFullArray($param_ConfigArray, mcPUIX::DBUIArray, mcPUIX::DBUIConfig, $tablename)) {
					
					$ConfigArray = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$tablename];
//					D::show(array($ConfigArray, $preparedArray), $i++.' updateDatabaseTable: '.$tablename);
				
				
				
					$TableConfig = null;
					$BoolActiveRow = false;


					if(array_key_exists(mcPUIX::FORM_DO_ROW, $ConfigArray) && is_array($ConfigArray[mcPUIX::FORM_DO_ROW])) {
						$BoolActiveRow = true;
					}

					if(array_key_exists('TABLE', $ConfigArray)
							&& array_key_exists('TABLENAME', $ConfigArray['TABLE']) && $ConfigArray['TABLE']['TABLENAME']
							&& array_key_exists('PRIMARY', $ConfigArray['TABLE']) && is_array($ConfigArray['TABLE']['PRIMARY'])
					) {
//D::cf($tablename.'  ');
						$TableConfig  = $ConfigArray['TABLE'];
//D::ulli($tablename. '   config');
						unset($TableConfig['TABLENAME']);

						/**
						 * delete the rows
						 */
						if(mcArray::checkKeyHasFullArray($preparedArray, 'DeleteRows')) {

							foreach($preparedArray['DeleteRows'] as $DeleteRow) {
								$var_bindValueArray = array();
								$var_query = 'delete from '.$ConfigArray['TABLE']['TABLENAME'].' ' ;

								if(mcPUIX_PDO::getDatabaseTable_where($TableConfig['PRIMARY'], $DeleteRow, $var_query, $var_bindValueArray)) {
//D::show($var_bindValueArray, '$var_query: '.$var_query);
									$stmt = $param_iStorable->PDO_query($var_query, $var_bindValueArray);
									if($stmt) {
									} else  {
										return Error::newError('DevUser','delete Dataset Error from Table '.$ConfigArray['TABLE']['TABLENAME'].' ',$query);
									}						
								} 					
							}
						}

						/**
						 * update the rows
						 */
						if(mcArray::checkKeyHasFullArray($preparedArray, 'UpdateRows')) {

							foreach($preparedArray['UpdateRows'] as $UpdateRow) {
//D::show(array($TableConfig,$UpdateRow),'UpdateRows UpdateRows');								
								$var_bindValueArray = array();
								$var_query = 'update '.$ConfigArray['TABLE']['TABLENAME'].' set ';
								$i=0;
								$bool_doUpdate = false;
								foreach($UpdateRow as $row_key => $row_value) {
									if($row_key!==mcPUIX::FORM_DO_ROW 
											&& $row_key!==mcPUIX::DBUIArray
											&& false===array_search($row_key, $TableConfig['PRIMARY'])) {
										if($i>0) {
											$var_query .= ', ';
										}
										$var_query .= ' '.$row_key.'=:'.$row_key.'_'.$i.'';
										$var_bindValueArray[':'.$row_key.'_'.$i] = $row_value;
										$i++;
										
										$bool_doUpdate = true;
									}
								}

								if($bool_doUpdate && mcPUIX_PDO::getDatabaseTable_where($TableConfig['PRIMARY'], $UpdateRow, $var_query, $var_bindValueArray)) {

									$stmt = $param_iStorable->PDO_query($var_query, $var_bindValueArray);
									if($stmt) {

									} else  {
										return Error::newError('UserError',"Fehler beim speichern von SocialNetInterfaceAddon ",$query);
									}						
								} else {
//									return Error::newError('DevError',"not enough primary values for keys ",$query);
								}
							}
						}
//D::show($preparedArray['NewRows'], 'NewRows1');
						/**
						 * insert the new rows
						 */
						if(mcArray::checkKeyHasFullArray($preparedArray, 'NewRows')) {
//D::show($preparedArray['NewRows'], 'NewRows2');
							foreach($preparedArray['NewRows'] as $NewRow) {
								unset($NewRow[mcPUIX::FORM_DO_ROW]);
								if($TableConfig['KINDOF'] === 'LIST')  {
									if($NewRow) {
//										D::show($NewRow,'NewRows');
										$param_iStorable->PDO_insertRow($ConfigArray['TABLE']['TABLENAME'], $ConfigArray['TABLE']['LISTID'], $ConfigArray['TABLE']['ELEMENTID'], $NewRow, true); 
									}

								} else if($TableConfig['KINDOF'] == 'DATASET') {
									D::show($preparedArray['NewRows'], 'FOR DATASET INSERT NOT IMPLEMENTED');
									
								} else if($TableConfig['KINDOF'] === 'RELATION') {
//									D::show($preparedArray['NewRows'], 'FOR RELATION INSERT NOT IMPLEMENTED');
									if($NewRow) {
//										D::show($NewRow,'NewRows');
										$param_iStorable->PDO_insertRow($ConfigArray['TABLE']['TABLENAME'], $ConfigArray['TABLE']['LISTID'], null, $NewRow, true); 
									}									
								}								
							}							
						}
//						$DataBaseArray = array_merge($preparedArray['NewRows'], $preparedArray['UpdateRows']);
			//			D::show($DataBaseArray);
//						return $DataBaseArray;
					} else {
						D::show($ConfigArray['TABLE']);
						return Error::newError('DevError','Table Config is wrong, missing data');
					}				
				}
			}
		}
	}
}
?>