CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
paramsLogger.php259 linesDownload Raw Back to classes
1<?php2class paramsLogger3{4	/**5	 * @type String6	 */7	protected $paramsTableName = "";8 9	/**10	 * 1 - saved searches11	 * 2 - columns sizes12	 * 3 - show/hide fields13	 * 4 - reordering fields14	 *15	 * @type Number16	 */17	protected $type;18 19	/**20	 * @type String21	 */22	protected $userID = "";23 24	/**25	 * @type String26	 */27	protected $cookie = "";28 29	/**30	 * @type String31	 */32	protected $tableNameId;33 34	protected $dataSource;35	36 37	public function __construct( $tableNameId, $type ) {38		global $cman;39		40		$this->type = $type;41		$this->tableNameId = $tableNameId;42		43		$this->assignUserId();44		$this->assignCookieParams();45		46		$this->dataSource = getTableDataSource( $this->paramsTableName, $cman->getSavedSearchesConnId() );47	}48 49	/**50	 * Assign the userID prop with the currenly logged in user`s name51	 */52	protected function assignUserId() {53		if( Security::isLoggedIn() )54			$this->userID = Security::getUserName();55	}56 57	/**58	 * Set a COOKIE 'searchSaving' param If It isn`t set before.59	 * Assign the 'cookie' property with the COOKIE 'searchSaving' param60	 */61	protected function assignCookieParams() {62		if( !strlen($_COOKIE["paramsLogger"]) && !$this->userID )63			runner_setcookie("paramsLogger", generatePassword(24), time() + 5 * 365 * 86400, "", "", false, false);64		65		$this->cookie = $_COOKIE["paramsLogger"];66	}67 68	/**69	 *70	 */71	protected function getCommonFilter() {72		$userConditions = array();73		74		if( $this->userID )75			$userConditions[] = DataCondition::FieldEquals( "USERNAME", $this->userID );76		77		if( $this->cookie )78			$userConditions[] = DataCondition::FieldEquals( "COOKIE", $this->cookie );79		80		if( !$userConditions )81			return DataCondition::_False();82		83		$conditions = array(84			DataCondition::_Or( $userConditions ),85			DataCondition::FieldEquals( "TABLENAME", $this->tableNameId )86		);87		88		// if not searchParamsLogger89		if( $this->type !== SSEARCH_PARAMS_TYPE )90			$conditions[] = DataCondition::FieldEquals( "TYPE", $this->type );91		92		return DataCondition::_And( $conditions );93	}94 95	/**96	 * Save params97	 * @param array $data98	 * @param array values (optional)99	 */100	public function save( $data, $_values = array() ) {101		$issetData = strlen( $this->readData() ) != 0;102		if ( $issetData && $this->type != SSEARCH_PARAMS_TYPE ) {103			$dc = $this->getUpdateCommand( $data );104			$this->dataSource->updateSingle( $dc, false );105			return;106		}107		108		$dc = new DsCommand();109		110		$values = $_values;111		$values["SEARCH"] = my_json_encode( $data );112		$values["TABLENAME"] = $this->tableNameId;113		114		if ( $this->userID ) {115			$values["USERNAME"] = $this->userID;116		} else if ( $this->cookie ) {117			$values["COOKIE"] = $this->cookie;118		}119		120		if ( $this->type != SSEARCH_PARAMS_TYPE )121			$values["TYPE"] = $this->type;122		123		$dc->values = $values;124		125		$this->dataSource->insertSingle( $dc );126	}127 128 129	/**130	 * Save show/hide131	 * @param array $data132	 */133	public function saveShowHideData( $deviceClass, $data )134	{135		if( $this->getShowHideData( $deviceClass ) ) {136			$dc = $this->getUpdateCommand( $data );137			138			$dc->filter = DataCondition::_And( array( 139				$dc->filter, 140				DataCondition::FieldEquals( "NAME", $deviceClass )141			));142			143			$this->dataSource->updateSingle( $dc, false );	144			return;145		}146		147		$dc = new DsCommand();148		149		$values = array();150		$values["NAME"] = $deviceClass;151		$values["SEARCH"] = my_json_encode( $data );152		$values["TABLENAME"] = $this->tableNameId;153		$values["TYPE"] = $this->type;154		155		if ( $this->userID ) {156			$values["USERNAME"] = $this->userID;157		} else if ( $this->cookie ) {158			$values["COOKIE"] = $this->cookie;159		}160		161		$dc->values = $values;162		163		$this->dataSource->insertSingle( $dc );164	}165 166	/**167	 * Update the existing params168	 *169	 * @param array $data170	 * @param String $addWhere (optional)171	 * @deprecated ?172	 */173	protected function update( $data, $addWhere = '' ) {174		$dc = $this->getUpdateCommand( $data );175		$this->dataSource->updateSingle( $dc );176	}177	178	protected function getUpdateCommand( $data ) {179		$dc = $this->getDataCommand();180		$dc->values = array();181		$dc->values[ "SEARCH" ] = my_json_encode( $data );182		return $dc;183	}184 185	/**186	 * Delete param187	 * @param String $addWhere (optional)188	 */189	protected function delete() {190		$dc = $this->getDataCommand();191		$dataSource->deleteSingle( $dc, false );192	}193 194 195	/**196	 * @return array or null197	 */198	public function getData() {199		return $this->decode( $this->readData() );200	}201 202	/**203	 * @return array or null204	 */205	public function decode( $data ) {206		$parsed = my_json_decode( $data );207		if( !is_array( $parsed ) )208			return runner_unserialize_array( $data );209		return $parsed;210	}211 212 213	protected function getDataCommand() {214		$dc = new DsCommand();215		$dc->filter = $this->getCommonFilter();216		return $dc;217	}218 219 220	protected function readData() {221		$dc = $this->getDataCommand();222		223		$qResult = $this->dataSource->getSingle( $dc );224		if( !$qResult )225			return "";226		227		$data = $qResult->fetchAssoc();228		if( !isset( $data["SEARCH"] ) )229			return false;230		231		return $data["SEARCH"];232	}233 234	/**235	 * @return array236	 */237	public function getShowHideData( $deviceClass = -1 ) {238		$dc = $this->getDataCommand();239		240		if( $deviceClass >= 0 ) {241			$dc->filter = DataCondition::_And( array(242				$dc->filter, 243				DataCondition::FieldEquals( "NAME", $deviceClass ) 244			));245		}246		247		$qResult = $this->dataSource->getList( $dc );248		249		if( !$qResult )250			return array();	251		252		$ret = array();253		while( $data = $qResult->fetchAssoc() ) {254			$ret[ $data["NAME"] ] = $this->decode( $data["SEARCH"] );255		}256		return $ret;257	}258}259?>