Skip to content

Commit

Permalink
Temporary deactivate updateFeatureDescription function since it is no…
Browse files Browse the repository at this point in the history
…t yet validated
  • Loading branch information
jjrom committed Jun 6, 2024
1 parent 158ee7e commit 0428bcc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
32 changes: 30 additions & 2 deletions app/resto/core/dbfunctions/CatalogsFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,42 @@ public function updateCatalog($catalog)

}

/**
* Increment input catalogs count
*
* @param array $catalogs
* @param string $collectionId
* @param integer $increment
*/
public function updateCatalogsCounters($catalogs, $collectionId, $increment)
{

$catalogIds = [];
for ($i = count($catalogs); $i--;) {
$catalogIds[] = $catalogs[$i]['id'];
}

$query = join(' ', array(
'UPDATE ' . $this->dbDriver->targetSchema . '.catalog SET counters=public.increment_counters(counters,' . $increment . ',' . (isset($collectionId) ? '\'' . $collectionId . '\'': 'NULL') . ')',
'FROM ' . $this->dbDriver->targetSchema . '.catalog',
'WHERE id IN (\'' . join('\',\'', $catalogIds) . '\') RETURNING id'
));

$results = $this->dbDriver->fetch($this->dbDriver->query($query));

return count($results);

}


/**
* Increment all catalogs relied to feature
*
* @param string $featureId
* @param string $collectionId
* @param integer $increment
*/
public function updateCatalogsCounts($featureId, $collectionId, $increment)
public function updateFeatureCatalogsCounters($featureId, $collectionId, $increment)
{

$query = join(' ', array(
Expand Down Expand Up @@ -351,7 +379,7 @@ public function getSummaries($types, $collectionId)
$pivots = array();

$catalogs = $this->getCatalogs(array(
'where' => !empty($types) ? 'rtype IN(\'' . join('\',\'', $types) . '\')' : 'rtype NOT IN (\'' . join('\',\'', CatalogsFunctions::TOPONYM_TYPES) . '\')'
'where' => !empty($types) ? 'rtype IN (\'' . join('\',\'', $types) . '\')' : 'rtype NOT IN (\'' . join('\',\'', CatalogsFunctions::TOPONYM_TYPES) . '\')'
));

$counter = 0;
Expand Down
52 changes: 26 additions & 26 deletions app/resto/core/dbfunctions/FeaturesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function removeFeature($feature)
/*
* Update statistics counter for featureId - i.e. remove 1 per catalogs containing this feature
*/
$catalogsUpdated = (new CatalogsFunctions($this->dbDriver))->updateCatalogsCounts($feature->id, $feature->collection->id, -1);
$catalogsUpdated = (new CatalogsFunctions($this->dbDriver))->updateFeatureCatalogsCounters($feature->id, $feature->collection->id, -1);

/*
* Next remove
Expand Down Expand Up @@ -504,13 +504,13 @@ public function updateFeature($feature, $collection, $newFeatureArray)
*/
if ( !empty($diffCatalogs['removed']) ) {
// [IMPORTANT] First run updateCatalogCounts !!
(new CatalogsFunctions($this->dbDriver))->updateCatalogsCounts($feature->id, $feature->collection->id, -1);
(new CatalogsFunctions($this->dbDriver))->updateCatalogsCounters($diffCatalogs['removed'], $feature->collection->id, -1);
$this->dbDriver->pQuery('DELETE FROM ' . $this->dbDriver->targetSchema . '.catalog_feature WHERE featureid=$1', array(
$feature->id
));
}
if ( !empty($diffCatalogs['added']) ) {
(new CatalogsFunctions($this->dbDriver))->storeCatalogs($keysAndValues['catalogs'], $collection->user->profile['id'], $collection->id, $feature->id);
(new CatalogsFunctions($this->dbDriver))->storeCatalogs($diffCatalogs['added'], $collection->user->profile['id'], $collection->id, $feature->id);
}

/*
Expand Down Expand Up @@ -571,19 +571,19 @@ public function updateFeatureProperty($feature, $property, $value)
public function updateFeatureDescription($feature, $description)
{

return 'TODO';

// Get hashtags to remove from feature before update
$hashtagsToRemove = $this->extractHashtagsFromText($feature->toArray()['properties']['description'], true);
$hashtagsToAdd = $this->extractHashtagsFromText($description, true);
$hashtags = array_merge(array_diff($feature->toArray()['properties']['hashtags'], $hashtagsToRemove), $hashtagsToAdd);
return RestoLogUtil::httpError(400, 'TODO - update feature description not yet implemented');

$cataloger = new Cataloger($feature->collection->context, $feature->collection->user);
$catalogsFunctions = new CatalogsFunctions($this->dbDriver);

// Get diff for catalogs
$diffCatalogs = $catalogsFunctions->diff($cataloger->catalogsFromText($feature->toArray()['properties']['description']), $cataloger->catalogsFromText($description));

$model = isset($feature->collection) ? $feature->collection->model : new DefaultModel();

/*
* Transaction
*/
try {

/*
* Update description, hashtags and normalized_hashtags
*/
Expand All @@ -592,27 +592,27 @@ public function updateFeatureDescription($feature, $description)
'{' . join(',', $hashtags) . '}',
$feature->id
));
} catch (Exception $e) {
RestoLogUtil::httpError(500, 'Cannot update feature ' . $feature->id);
}

/*
* Update facets i.e. remove old facets and add new ones
* This is non blocking i.e. if error just indicated in the result but feature is updated
*/
$facetsUpdated = true;
try {
$facetsFunctions = new FacetsFunctions($this->dbDriver);
$facetsFunctions->removeFacetsFromHashtags($hashtagsToRemove, $feature->collection->id);
if ($feature->context->core['storeFacets'] && $model->dbParams['storeFacets']) {
$facetsFunctions->storeFacets($hashtagsToAdd, $feature->user->profile['id'], $feature->collection->id);
/*
* Remove/add catalogs
*/
if ( !empty($diffCatalogs['removed']) ) {
// [IMPORTANT] First run updateCatalogsCounters !!
(new CatalogsFunctions($this->dbDriver))->updateCatalogsCounters($diffCatalogs['removed'], $feature->collection->id, -1);
$this->dbDriver->pQuery('DELETE FROM ' . $this->dbDriver->targetSchema . '.catalog_feature WHERE featureid=$1', array(
$feature->id
));
}
if ( !empty($diffCatalogs['added']) ) {
(new CatalogsFunctions($this->dbDriver))->storeCatalogs($diffCatalogs['added'], $feature->collection->user->profile['id'], $feature->collection->id, $feature->id);
}

} catch (Exception $e) {
$facetsUpdated = false;
RestoLogUtil::httpError(500, 'Cannot update feature ' . $feature->id);
}

return RestoLogUtil::success('Property description updated for feature ' . $feature->id, array(
'facetsUpdated' => $facetsUpdated
'id' => $feature->id
));
}

Expand Down

0 comments on commit 0428bcc

Please sign in to comment.