/** * Short description of method unReferenceResource * * @access public * @author Joel Bout, <*****@*****.**> * @param Resource resource * @return boolean */ public function unReferenceResource(\core_kernel_classes_Resource $resource) { $returnValue = (bool) false; if ($this->isResourceReferenced($resource)) { $dbWrapper = \core_kernel_classes_DbWrapper::singleton(); //select id to be removed: $resourceId = Utils::getResourceIdByTable($resource, 'resource_to_table'); if ($resourceId) { $queries[] = 'DELETE FROM "resource_has_class" WHERE "resource_has_class"."resource_id" = \'' . $resourceId . '\';'; $queries[] = 'DELETE FROM "resource_to_table" WHERE "resource_to_table"."id" = \'' . $resourceId . '\';'; $returnValue = true; foreach ($queries as $query) { $result = $dbWrapper->exec($query); if ($result === false) { $returnValue = false; } } if ($returnValue !== false) { if (array_key_exists($resource->getUri(), self::$_resources)) { unset(self::$_resources[$resource->getUri()]); } } } } return (bool) $returnValue; }
/** * Short description of method delete * * @access public * @author Joel Bout, <*****@*****.**> * @param Resource resource * @param boolean deleteReference * @return boolean */ public function delete(\core_kernel_classes_Resource $resource, $deleteReference = false) { $returnValue = (bool) false; $dbWrapper = \core_kernel_classes_DbWrapper::singleton(); $tableName = ResourceReferencer::singleton()->resourceLocation($resource); if (empty($tableName)) { return $returnValue; } $uri = $resource->getUri(); $resourceId = HardapiUtils::getResourceIdByTable($resource, $tableName); if ($resourceId) { /* * Delete all the references of the resource first, before the resource is delete of course, * if the parameter $deleteReference is true */ if ($deleteReference) { $properties = array(); //get the resource classes (type) $types = ''; foreach ($resource->getTypes() as $type) { $properties[$type->getUri()] = array(); $types = "'" . $type->getUri() . "',"; } $types = substr($types, 0, strlen($types) - 1); if (!empty($types)) { //get all the properties that have one of the resource class as range $sqlQuery = 'SELECT "subject", "object" FROM "statements" WHERE "predicate" = \'' . RDFS_RANGE . '\' AND object IN (' . $types . ')'; $result = $dbWrapper->query($sqlQuery); while ($row = $result->fetch()) { //fill the properties range: propertyUri => domains: $propertyUri = $row['subject']; $rangeUri = $row['object']; $properties[$rangeUri][$propertyUri] = array(); //get the domain of the property: $property = new \core_kernel_classes_Property($propertyUri); foreach ($property->getDomain()->getIterator() as $domain) { if ($domain instanceof \core_kernel_classes_Class) { $properties[$rangeUri][$propertyUri][] = $domain->getUri(); } } } //delete the references $referencer = ResourceReferencer::singleton(); foreach ($properties as $rangeUri => $propertyUris) { foreach ($propertyUris as $propertyUri => $domains) { //property -> column $property = new \core_kernel_classes_Property($propertyUri); $isMulti = $property->isMultiple() || $property->isLgDependent(); $columnName = ''; if (!$isMulti) { $columnName = HardapiUtils::getShortName($property); if (empty($columnName)) { continue; } } foreach ($domains as $domainUri) { //classLocations -> table $classLocations = $referencer->classLocations(new \core_kernel_classes_Class($domainUri)); foreach ($classLocations as $classLocation) { if ($property->isMultiple()) { //delete the row in the props table $query = 'DELETE FROM "' . $classLocation['table'] . 'props" WHERE "property_uri" = ? AND ("property_value" = ? OR "property_foreign_uri" = ?)'; $dbWrapper->exec($query, array($propertyUri, $uri, $uri)); } else { //set the col value to NULL $query = 'UPDATE "' . $classLocation['table'] . '" SET "' . $columnName . '" = NULL WHERE "' . $columnName . '" = ?'; $dbWrapper->exec($query, array($uri)); } } } } } } } $queries = array(); // Delete records in the main table $queries[] = 'DELETE FROM "' . $tableName . '" WHERE "id" = \'' . $resourceId . '\''; // Delete records in the properties table $queries[] = 'DELETE FROM "' . $tableName . 'props" WHERE "instance_id" = \'' . $resourceId . '\''; foreach ($queries as $query) { try { $result = $dbWrapper->exec($query); if ($result === false) { $returnValue = false; break; } else { $returnValue = true; } } catch (\PDOException $e) { throw new Exception("Unable to delete resource ({$resource->getUri()}) ;" . $e->getMessage()); } } // Unreference the resource ResourceReferencer::singleton()->unReferenceResource($resource); } return (bool) $returnValue; }