class_mcActiveRecort.inc.php 8.08 KB
<?php
/** 
 * @copyright (c) 2014, netz.coop eG
 */
class mcActiveRecord extends ActiveRecord {
	
	public static function PDO_query($param_query, array $param_bindValueArray=null) {
		$pdo = ActiveDB::getConnection();  
	
		try {

			$stmt = $pdo->prepare($param_query);

			if(!$stmt) {
				D::backtrace('PDO SQL ERROR');
				D::ulli("PDO_query(".$param_query.")");
				D::show($pdo->errorInfo(), $pdo->errorCode());
				D::show(PDO::getAvailableDrivers  ( void  ), 'PDO::getAvailableDrivers  ( void  )');
//								handle($param_errno, $param_errstr, $param_errfile, $param_errline, $param_errcontext)
				mcErrorEvent::handle('PDOSql', "error(".$pdo->errorCode().")", null, null, $param_query);
				return false;
			}

			if($param_bindValueArray!==null) {
				foreach($param_bindValueArray as $key => $value) {
					$stmt->bindValue($key,$value);
				}
			}

			$stmt->execute();
		} catch (PDOException $e) {
			D::backtrace();
			D::printPDOQuery($param_query, $param_bindValueArray);
			mcException::handleException($e);
		}
		
		if($stmt->errorCode() == "42000") {
			D::backtrace('PDO SQL ERROR');
			D::ulli("PDO_query(".$param_query.")");
			D::show($stmt->errorInfo(), $stmt->errorCode());
			mcErrorEvent::handle($param_typ='PDOSql', "error(".$stmt->errorCode().")", null, null, $param_query);
			return false;
		}
		return $stmt;
	}	

	public static function PDO_fetchAll(PDOStatement $param_stmt) {
		try {
			$var_result = $param_stmt->fetchAll(PDO::FETCH_ASSOC);
			$param_stmt->closeCursor();
			return $var_result;
		} catch (PDOException $e) {
			mcException::handleException($e);
		}
	}	
	
	/**
	 *	fnc sort relations
	 * 
	 * @todo überprüfen ob sort alg stabil ist
	 * 
	 * 
	 * @since 20120125 @author neumann@netz.coop	
	 * 
	 * @arphp -- fnc zum commiten
	 * 
	 * @param type $param_RelationName
	 * @param type $param_column
	 * @return boolean 
	 */
	public function sortManyRel($param_RelationName, $param_column) {
		
		$relationobjs =  mcSort::sortObjs($this->$param_RelationName, $param_column);
		if(is_array($relationobjs)) {
			$this->$param_RelationName = $relationobjs;
			return true;
		} else {
			return false;
		}
		
	}

	public function existColumn($varName){
//D::li($varName,1,1);
		if(array_key_exists($varName,$this->dbFields)){
			return true;
		}
		if(array_key_exists($varName,$this->hasOneObjects)){
			return true;
		}
		//1:n beziehungen werden erst beim 1.aufruf geladen
		if(array_key_exists($varName,$this->hasManyObjects)){
			return true;
		}
		//n:m beziehungen werden erst beim 1.aufruf geladen
		if(array_key_exists($varName,$this->hasManyBelongsToObjects)){
			return true;
		}

		return false;

	}	
	
	/**
	 *	show ActiveRecord Array in html table
	 * 
	 * @param array $praram_ActiveRecords
	 * @param array $param_ShowColumns -- array(
	 *									'col1'=>null, 
	 *									'col2'=>array('link' => array(action=>'do',ID=1), 
	 *									'col3'=>array('type'=>'date||euro'),
	 *									'col4'=>array('hasOneRel' => 'activerecortclassname'),
	 *								)
	 * @param string $param_subject
	 * @param array $param_headrow=null
	 * @param type $param_SortColumn
	 * @param array $param_marked 
	 * @param 
	 */
	public static function showArray(array $praram_ActiveRecords, array $param_ShowColumns, $param_subject="", array $param_headrow=null, $param_SortColumn=null, array $param_marked=null, array $param_summaryline=null ) {

		if(count($praram_ActiveRecords) && count($param_ShowColumns)) {
			$Display_sortRealtions = array();
			
			if(!is_null($param_headrow)) {
				$Display_sortRealtions[] = $param_headrow;
			}
			
			if(!is_null($param_SortColumn)) {
				$praram_ActiveRecords=  mcSort::sortObjs($praram_ActiveRecords, $param_SortColumn);
			}

			if(is_array($param_summaryline) && count($praram_ActiveRecords)>1) {
				$summaryline = array();
				$lastline = array();
			}
			
			foreach($praram_ActiveRecords as $relationobj) {

				if(!is_null($param_marked)) {
					$m_bool =array();
					foreach($param_marked as $mkey => $mvalue) {
						if($relationobj->$mkey == $mvalue) {
							$m_bool[$mkey] = $mvalue;
						} 
					}

					if($m_bool == $param_marked) {
						$b1 = '<u>';
						$b2 = '</u>';
					} else {
						$b1 = $b2 = '';
					}						
				}

				$Display_Line = array();
				foreach($param_ShowColumns as $columnname => $columnoptions) {
//					D::li($columnname,1,1);
					if(is_array($columnoptions)) {
						if(array_key_exists('hasOneRel', $columnoptions) && $columnoptions['hasOneRel']) {
							if($relationobj->$columnoptions['hasOneRel']->existColumn($columnname)) {
								$Display_value = $relationobj->$columnoptions['hasOneRel']->$columnname;
								
							} else if(array_key_exists('valueIsFnc', $columnoptions) && $columnoptions['valueIsFnc']) {
								$return = $relationobj->$columnoptions['hasOneRel']->$columnname();
								if(is_array($return) && array_key_exists('returnKey', $columnoptions['valueIsFnc'])) {
									$Display_value = $return[$columnoptions['valueIsFnc']['returnKey']];
								} else if(!is_array($return)) {
									$Display_value = $return;
								} else {
									D::li('config Error');
								}
								
							} else {
								$Display_value = mcLanguage::getWord($columnname);
							}
							
						} else {
							if($relationobj->existColumn($columnname)) {
								$Display_value = $relationobj->$columnname;
							} else if(array_key_exists('valueIsFnc', $columnoptions) && $columnoptions['valueIsFnc']) {
								
								$return = $relationobj->$columnname();
								if(is_array($return) && array_key_exists('returnKey', $columnoptions['valueIsFnc'])) {
									$Display_value = $return[$columnoptions['valueIsFnc']['returnKey']];
								} else if(!is_array($return)) {
									$Display_value = $return;
								} else {
									D::li('config Error');
								}
								
								
								
							} else {
								$Display_value = mcLanguage::getWord($columnname);
							}							
						}
					} else {
						if($relationobj->existColumn($columnname)) {
							$Display_value = $relationobj->$columnname;
						} else {
							$Display_value = mcLanguage::getWord($columnname);
						}							
					}
					
					if(is_array($param_summaryline) && array_key_exists($columnname, $param_summaryline)) {
						if($param_summaryline[$columnname] == 'summation') {
							$summaryline[$columnname] += $Display_value;
						}
						
					}
							
					
					
					if(empty($columnoptions)) {
						$Line = $b1.$Display_value.$b2;
					} else if($columnoptions['type']=='euro') {
						$Line = $b1.mcNumber::form2($Display_value).' €'.$b2;
					} else if($columnoptions['type']=='date') {
						$Line = $b1.mcDate::format8601Date($Display_value).''.$b2;						
					} else {
						$Line = $b1.$Display_value.$b2;
					}

					if(!empty($columnoptions) && array_key_exists('link', $columnoptions) && is_array($columnoptions['link'])) {
						if(array_key_exists('hasOneRel', $columnoptions) && $columnoptions['hasOneRel']) {
							$Display_Line[] = mcInputHandler::getHrefActionMenuLink($Line, $columnoptions['link']['action'], $relationobj->$columnoptions['hasOneRel']->$columnoptions['link']['ID']);
						} else {
							$Display_Line[] = mcInputHandler::getHrefActionMenuLink($Line, $columnoptions['link']['action'], $relationobj->$columnoptions['link']['ID']);
						}

					} else {
						$Display_Line[] = $Line;
					}						
				
				}
				$Display_sortRealtions[] = $Display_Line;
			}	
			
			if(is_array($lastline)) {
				foreach($param_ShowColumns as $columnname => $columnoptions) {
					if(array_key_exists($columnname, $summaryline)) {
						if(empty($columnoptions)) {
							$lastline[] = $summaryline[$columnname];
						} else if($columnoptions['type']=='euro') {
							$lastline[] = mcNumber::form2($summaryline[$columnname]).' €';
						} else if($columnoptions['type']=='date') {
							$lastline[] = mcDate::format8601Date($summaryline[$columnname]).'';						
						} else {
							$lastline[] = $summaryline[$columnname];
						}						
					} else {
						$lastline[] = '';
					}
				}				
				$Display_sortRealtions[] = $lastline;
			}
			
			mcTableHtml::showTable($Display_sortRealtions, $param_subject);
		}	
		
		if(is_array($summaryline)) {
			return $summaryline;
		}
	}
	
}
?>