ManyManyList.php 11.1 KB
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 404 405 406
<?php

/**
 * Subclass of {@link DataList} representing a many_many relation.
 *
 * @package framework
 * @subpackage model
 */
class ManyManyList extends RelationList {
	
	/**
	 * @var string $joinTable
	 */
	protected $joinTable;
	
	/**
	 * @var string $localKey
	 */
	protected $localKey;
	
	/**
	 * @var string $foreignKey
	 */
	protected $foreignKey;

	/**
	 * @var array $extraFields
	 */
	protected $extraFields;

	/**
	 * @var array $_compositeExtraFields
	 */
	protected $_compositeExtraFields = array();

	/**
	 * Create a new ManyManyList object.
	 * 
	 * A ManyManyList object represents a list of {@link DataObject} records
	 * that correspond to a many-many relationship.
	 * 
	 * Generation of the appropriate record set is left up to the caller, using
	 * the normal {@link DataList} methods. Addition arguments are used to
	 * support {@@link add()} and {@link remove()} methods.
	 * 
	 * @param string $dataClass The class of the DataObjects that this will list.
	 * @param string $joinTable The name of the table whose entries define the content of this many_many relation.
	 * @param string $localKey The key in the join table that maps to the dataClass' PK.
	 * @param string $foreignKey The key in the join table that maps to joined class' PK.
	 * @param string $extraFields A map of field => fieldtype of extra fields on the join table.
	 * 
	 * @example new ManyManyList('Group','Group_Members', 'GroupID', 'MemberID');
	 */
	public function __construct($dataClass, $joinTable, $localKey, $foreignKey, $extraFields = array()) {
		parent::__construct($dataClass);

		$this->joinTable = $joinTable;
		$this->localKey = $localKey;
		$this->foreignKey = $foreignKey;
		$this->extraFields = $extraFields;

		$baseClass = ClassInfo::baseDataClass($dataClass);

		// Join to the many-many join table
		$this->dataQuery->innerJoin($joinTable, "\"$joinTable\".\"$this->localKey\" = \"$baseClass\".\"ID\"");

		// Add the extra fields to the query
		if($this->extraFields) {
			$this->appendExtraFieldsToQuery();
		}
	}

	/**
	 * Adds the many_many_extraFields to the select of the underlying
	 * {@link DataQuery}.
	 *
	 * @return void
	 */
	protected function appendExtraFieldsToQuery() {
		$finalized = array();

		foreach($this->extraFields as $field => $spec) {
			$obj = Object::create_from_string($spec);

			if($obj instanceof CompositeDBField) {
				$this->_compositeExtraFields[$field] = array();

				// append the composite field names to the select
				foreach($obj->compositeDatabaseFields() as $k => $f) {
					$col = $field . $k;
					$finalized[] = $col;

					// cache
					$this->_compositeExtraFields[$field][] = $k;
				}
			} else {
				$finalized[] = $field;
			}
		}

		$this->dataQuery->selectFromTable($this->joinTable, $finalized);
	}

	/**
	 * Create a DataObject from the given SQL row.
	 *
	 * @param array $row
	 * @return DataObject
	 */
	protected function createDataObject($row) {
		// remove any composed fields
		$add = array();

		if($this->_compositeExtraFields) {
			foreach($this->_compositeExtraFields as $fieldName => $composed) {
				// convert joined extra fields into their composite field
				// types.
				$value = array();

				foreach($composed as $i => $k) {
					if(isset($row[$fieldName . $k])) {
						$value[$k] = $row[$fieldName . $k];

						// don't duplicate data in the record
						unset($row[$fieldName . $k]);
					}
				}

				$obj = Object::create_from_string($this->extraFields[$fieldName], $fieldName);
				$obj->setValue($value, null, false);

				$add[$fieldName] = $obj;
			}
		}

		$dataObject = parent::createDataObject($row);

		foreach($add as $fieldName => $obj) {
			$dataObject->$fieldName = $obj;
		}

		return $dataObject;
	}

	/**
	 * Return a filter expression for when getting the contents of the
	 * relationship for some foreign ID
	 *
	 * @param int $id
	 *
	 * @return string
	 */
	protected function foreignIDFilter($id = null) {
		if ($id === null) $id = $this->getForeignID();

		// Apply relation filter
		if(is_array($id)) {
			return "\"$this->joinTable\".\"$this->foreignKey\" IN ('" . 
				implode("', '", array_map('Convert::raw2sql', $id)) . "')";
		} else if($id !== null){
			return "\"$this->joinTable\".\"$this->foreignKey\" = '" . 
				Convert::raw2sql($id) . "'";
		}
	}

	/**
	 * Return a filter expression for the join table when writing to the join table
	 *
	 * When writing (add, remove, removeByID), we need to filter the join table to just the relevant
	 * entries. However some subclasses of ManyManyList (Member_GroupSet) modify foreignIDFilter to
	 * include additional calculated entries, so we need different filters when reading and when writing
	 *
	 * @return string
	 */
	protected function foreignIDWriteFilter($id = null) {
		return $this->foreignIDFilter($id);
	}

	/**
	 * Add an item to this many_many relationship. Does so by adding an entry
	 * to the joinTable.
	 *
	 * @param mixed $item
	 * @param array $extraFields A map of additional columns to insert into the
	 *								joinTable
	 */
	public function add($item, $extraFields = null) {
		if(is_numeric($item)) {
			$itemID = $item;
		} else if($item instanceof $this->dataClass) {
			$itemID = $item->ID;
		} else {
			throw new InvalidArgumentException(
				"ManyManyList::add() expecting a $this->dataClass object, or ID value",
				E_USER_ERROR
			);
		}

		$foreignIDs = $this->getForeignID();
		$foreignFilter = $this->foreignIDWriteFilter();

		// Validate foreignID
		if(!$foreignIDs) {
			throw new Exception("ManyManyList::add() can't be called until a foreign ID is set", E_USER_WARNING);
		}

		if($foreignFilter) {
			$query = new SQLQuery("*", array("\"$this->joinTable\""));
			$query->setWhere($foreignFilter);
			$hasExisting = ($query->count() > 0);
		} else {
			$hasExisting = false;	
		}

		// Insert or update
		foreach((array)$foreignIDs as $foreignID) {
			$manipulation = array();

			if($hasExisting) {
				$manipulation[$this->joinTable]['command'] = 'update';	
				$manipulation[$this->joinTable]['where'] = "\"$this->joinTable\".\"$this->foreignKey\" = " . 
					"'" . Convert::raw2sql($foreignID) . "'" .
					" AND \"$this->localKey\" = {$itemID}";
			} else {
				$manipulation[$this->joinTable]['command'] = 'insert';	
			}

			if($extraFields) {
				foreach($extraFields as $k => $v) {
					if(is_null($v)) {
						$manipulation[$this->joinTable]['fields'][$k] = 'NULL';
					}
					else {
						if(is_object($v) && $v instanceof DBField) {
							// rely on writeToManipulation to manage the changes
							// required for this field.
							$working = array('fields' => array());

							// create a new instance of the field so we can
							// modify the field name to the correct version.
							$field = DBField::create_field(get_class($v), $v);
							$field->setName($k);

							$field->writeToManipulation($working);

							foreach($working['fields'] as $extraK => $extraV) {
								$manipulation[$this->joinTable]['fields'][$extraK] = $extraV;
							}
						} else {
							$manipulation[$this->joinTable]['fields'][$k] =  "'" . Convert::raw2sql($v) . "'";
						}
					}
				}
			}

			$manipulation[$this->joinTable]['fields'][$this->localKey] = $itemID;
			$manipulation[$this->joinTable]['fields'][$this->foreignKey] = $foreignID;

			DB::manipulate($manipulation);
		}
	}

	/**
	 * Remove the given item from this list.
	 *
	 * Note that for a ManyManyList, the item is never actually deleted, only
	 * the join table is affected.
	 *
	 * @param DataObject $item
	 */
	public function remove($item) {
		if(!($item instanceof $this->dataClass)) {
			throw new InvalidArgumentException("ManyManyList::remove() expecting a $this->dataClass object");
		}
		
		return $this->removeByID($item->ID);
	}

	/**
	 * Remove the given item from this list.
	 *
	 * Note that for a ManyManyList, the item is never actually deleted, only
	 * the join table is affected
	 *
	 * @param int $itemID The item ID
	 */
	public function removeByID($itemID) {
		if(!is_numeric($itemID)) throw new InvalidArgumentException("ManyManyList::removeById() expecting an ID");

		$query = new SQLQuery("*", array("\"$this->joinTable\""));
		$query->setDelete(true);

		if($filter = $this->foreignIDWriteFilter($this->getForeignID())) {
			$query->setWhere($filter);
		} else {
			user_error("Can't call ManyManyList::remove() until a foreign ID is set", E_USER_WARNING);
		}
		
		$query->addWhere("\"$this->localKey\" = {$itemID}");
		$query->execute();
	}

	/**
	 * Remove all items from this many-many join.  To remove a subset of items,
	 * filter it first.
	 *
	 * @return void
	 */
	public function removeAll() {
		$base = ClassInfo::baseDataClass($this->dataClass());

		// Remove the join to the join table to avoid MySQL row locking issues.
		$query = $this->dataQuery();
		$query->removeFilterOn($query->getQueryParam('Foreign.Filter'));

		$query = $query->query();
		$query->setSelect("\"$base\".\"ID\"");

		$from = $query->getFrom();
		unset($from[$this->joinTable]);
		$query->setFrom($from);
		$query->setDistinct(false);

		 // ensure any default sorting is removed, ORDER BY can break DELETE clauses
		$query->setOrderBy(null, null);

		// Use a sub-query as SQLite does not support setting delete targets in
		// joined queries.
		$delete = new SQLQuery();
		$delete->setDelete(true);
		$delete->setFrom("\"$this->joinTable\"");
		$delete->addWhere($this->foreignIDFilter());
		$delete->addWhere("\"$this->joinTable\".\"$this->localKey\" IN ({$query->sql()})");
		$delete->execute();
	}

	/**
	 * Find the extra field data for a single row of the relationship join
	 * table, given the known child ID.
	 *	
	 * @param string $componentName The name of the component
	 * @param int $itemID The ID of the child for the relationship
	 *
	 * @return array Map of fieldName => fieldValue
	 */
	public function getExtraData($componentName, $itemID) {
		$result = array();

		if(!is_numeric($itemID)) {
			user_error('ComponentSet::getExtraData() passed a non-numeric child ID', E_USER_ERROR);
		}

		// @todo Optimize into a single query instead of one per extra field
		if($this->extraFields) {
			foreach($this->extraFields as $fieldName => $dbFieldSpec) {
				$query = new SQLQuery("\"$fieldName\"", array("\"$this->joinTable\""));
				if($filter = $this->foreignIDWriteFilter($this->getForeignID())) {
					$query->setWhere($filter);
				} else {
					user_error("Can't call ManyManyList::getExtraData() until a foreign ID is set", E_USER_WARNING);
				}
				$query->addWhere("\"$this->localKey\" = {$itemID}");
				$result[$fieldName] = $query->execute()->value();
			}
		}
		
		return $result;
	}

	/**
	 * Gets the join table used for the relationship.
	 *
	 * @return string the name of the table
	 */
	public function getJoinTable() {
		return $this->joinTable;
	}

	/**
	 * Gets the key used to store the ID of the local/parent object.
	 *
	 * @return string the field name
	 */
	public function getLocalKey() {
		return $this->localKey;
	}

	/**
	 * Gets the key used to store the ID of the foreign/child object.
	 *
	 * @return string the field name
	 */
	public function getForeignKey() {
		return $this->foreignKey;
	}

	/**
	 * Gets the extra fields included in the relationship.
	 *
	 * @return array a map of field names to types
	 */
	public function getExtraFields() {
		return $this->extraFields;
	}

}