Files
Phraseanet/lib/classes/caption/Field/Value.class.php
2012-02-08 16:15:48 +01:00

359 lines
9.4 KiB
PHP

<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2010 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use \Alchemy\Phrasea\Vocabulary;
/**
*
* @package
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class caption_Field_Value
{
/**
*
* @var int
*/
protected $id;
/**
*
* @var string
*/
protected $value;
/**
*
* @var type \Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface
*/
protected $VocabularyType;
/**
*
* @var int
*/
protected $VocabularyId;
/**
*
* @var databox_field
*/
protected $databox_field;
/**
*
* @var record_adapter
*/
protected $record;
/**
*
* @param databox_field $databox_field
* @param record_adapter $record
* @param type $id
* @return \caption_Field_Value
*/
public function __construct(databox_field $databox_field, record_adapter $record, $id)
{
$this->id = (int) $id;
$this->databox_field = $databox_field;
$this->record = $record;
$connbas = $databox_field->get_databox()->get_connection();
$sql = 'SELECT record_id, value, VocabularyType, VocabularyId
FROM metadatas WHERE id = :id';
$stmt = $connbas->prepare($sql);
$stmt->execute(array(':id' => $id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$this->value = $row ? $row['value'] : null;
try
{
$this->VocabularyType = $row['VocabularyType'] ? Vocabulary\Controller::get($row['VocabularyType']) : null;
$this->VocabularyId = $row['VocabularyId'];
}
catch (\Exception $e)
{
}
if ($this->VocabularyType)
{
/**
* Vocabulary Control has been deactivated
*/
if (!$this->databox_field->getVocabularyControl())
{
$this->removeVocabulary();
}
/**
* Vocabulary Control has changed
*/
elseif ($this->databox_field->getVocabularyControl()->getType() !== $this->VocabularyType->getType())
{
$this->removeVocabulary();
}
/**
* Current Id is not available anymore
*/
elseif (!$this->VocabularyType->validate($this->VocabularyId))
{
$this->removeVocabulary();
}
/**
* String equivalence has changed
*/
elseif ($this->VocabularyType->getValue($this->VocabularyId) !== $this->value)
{
$this->set_value($this->VocabularyType->getValue($this->VocabularyId));
}
}
return $this;
}
public function getVocabularyType()
{
return $this->VocabularyType;
}
public function getVocabularyId()
{
return $this->VocabularyId;
}
public function getId()
{
return $this->id;
}
public function getValue()
{
return $this->value;
}
public function getDatabox_field()
{
return $this->databox_field;
}
public function getRecord()
{
return $this->record;
}
public function delete()
{
$connbas = $this->databox_field->get_connection();
$sql = 'DELETE FROM metadatas WHERE id = :id';
$stmt = $connbas->prepare($sql);
$stmt->execute(array(':id' => $this->id));
$stmt->closeCursor();
$sbas_id = $this->record->get_sbas_id();
$this->record->get_caption()->delete_data_from_cache();
try
{
$registry = registry::get_instance();
$sphinx_rt = sphinxrt::get_instance($registry);
$sbas_params = phrasea::sbas_params();
if (isset($sbas_params[$sbas_id]))
{
$params = $sbas_params[$sbas_id];
$sbas_crc = crc32(str_replace(array('.', '%'), '_', sprintf('%s_%s_%s_%s', $params['host'], $params['port'], $params['user'], $params['dbname'])));
$sphinx_rt->delete(array("metadatas" . $sbas_crc, "metadatas" . $sbas_crc . "_stemmed_fr", "metadatas" . $sbas_crc . "_stemmed_en"), "metas_realtime" . $sbas_crc, $this->id);
$sphinx_rt->delete(array("documents" . $sbas_crc, "documents" . $sbas_crc . "_stemmed_fr", "documents" . $sbas_crc . "_stemmed_en"), "docs_realtime" . $sbas_crc, $this->record->get_record_id());
}
}
catch (Exception $e)
{
unset($e);
}
return $this;
}
public function removeVocabulary()
{
$connbas = $this->databox_field->get_connection();
$params = array(
':VocabType' => null
, ':VocabularyId' => null
, ':meta_id' => $this->getId()
);
$sql_up = 'UPDATE metadatas
SET VocabularyType = :VocabType, VocabularyId = :VocabularyId
WHERE id = :meta_id';
$stmt_up = $connbas->prepare($sql_up);
$stmt_up->execute($params);
$stmt_up->closeCursor();
return $this;
}
public function setVocab(Vocabulary\ControlProvider\ControlProviderInterface $vocabulary, $vocab_id)
{
$connbas = $this->databox_field->get_connection();
$params = array(
':VocabType' => $vocabulary->getType()
, ':VocabularyId' => $vocab_id
, ':meta_id' => $this->getId()
);
$sql_up = 'UPDATE metadatas
SET VocabularyType = :VocabType, VocabularyId = :VocabularyId
WHERE id = :meta_id';
$stmt_up = $connbas->prepare($sql_up);
$stmt_up->execute($params);
$stmt_up->closeCursor();
$this->set_value($vocabulary->getValue($vocab_id));
return $this;
}
public function set_value($value)
{
$this->value = $value;
$sbas_id = $this->databox_field->get_databox()->get_sbas_id();
$connbas = $this->databox_field->get_connection();
$params = array(
':meta_id' => $this->id
, ':value' => $value
);
$sql_up = 'UPDATE metadatas SET value = :value WHERE id = :meta_id';
$stmt_up = $connbas->prepare($sql_up);
$stmt_up->execute($params);
$stmt_up->closeCursor();
try
{
$registry = registry::get_instance();
$sphinx_rt = sphinxrt::get_instance($registry);
$sbas_params = phrasea::sbas_params();
if (isset($sbas_params[$sbas_id]))
{
$params = $sbas_params[$sbas_id];
$sbas_crc = crc32(str_replace(array('.', '%'), '_', sprintf('%s_%s_%s_%s', $params['host'], $params['port'], $params['user'], $params['dbname'])));
$sphinx_rt->delete(array("metadatas" . $sbas_crc, "metadatas" . $sbas_crc . "_stemmed_fr", "metadatas" . $sbas_crc . "_stemmed_en"), "", $this->id);
$sphinx_rt->delete(array("documents" . $sbas_crc, "documents" . $sbas_crc . "_stemmed_fr", "documents" . $sbas_crc . "_stemmed_en"), "", $this->record->get_record_id());
}
}
catch (Exception $e)
{
}
$this->update_cache_value($value);
return $this;
}
/**
*
* @param array $value
* @return caption_field
*/
public function update_cache_value($value)
{
$this->record->get_caption()->delete_data_from_cache();
$sbas_id = $this->databox_field->get_databox()->get_sbas_id();
try
{
$registry = registry::get_instance();
$sbas_params = phrasea::sbas_params();
if (isset($sbas_params[$sbas_id]))
{
$params = $sbas_params[$sbas_id];
$sbas_crc = crc32(str_replace(array('.', '%'), '_', sprintf('%s_%s_%s_%s', $params['host'], $params['port'], $params['user'], $params['dbname'])));
$sphinx_rt = sphinxrt::get_instance($registry);
$sphinx_rt->replace_in_metas(
"metas_realtime" . $sbas_crc, $this->id, $this->databox_field->get_id(), $this->record->get_record_id(), $sbas_id, phrasea::collFromBas($this->record->get_base_id()), ($this->record->is_grouping() ? '1' : '0'), $this->record->get_type(), $value, $this->record->get_creation_date()
);
$all_datas = array();
foreach ($this->record->get_caption()->get_fields() as $field)
{
if (!$field->is_indexable())
continue;
$all_datas[] = $field->get_serialized_values();
}
$all_datas = implode(' ', $all_datas);
$sphinx_rt->replace_in_documents(
"docs_realtime" . $sbas_crc, //$this->id,
$this->record->get_record_id(), $all_datas, $sbas_id, phrasea::collFromBas($this->record->get_base_id()), ($this->record->is_grouping() ? '1' : '0'), $this->record->get_type(), $this->record->get_creation_date()
);
}
}
catch (Exception $e)
{
unset($e);
}
return $this;
}
public static function create(databox_field &$databox_field, record_Interface $record, $value, Vocabulary\ControlProvider\ControlProviderInterface $vocabulary = null, $vocabularyId = null)
{
$connbas = $databox_field->get_connection();
$sql_ins = 'INSERT INTO metadatas
(id, record_id, meta_struct_id, value, VocabularyType, VocabularyId)
VALUES
(null, :record_id, :field, :value, :VocabType, :VocabId)';
$params = array(
':record_id' => $record->get_record_id(),
':field' => $databox_field->get_id(),
':value' => $value,
':VocabType' => $vocabulary ? $vocabulary->getType() : null,
':VocabId' => $vocabulary ? $vocabularyId : null,
);
$stmt_ins = $connbas->prepare($sql_ins);
$stmt_ins->execute($params);
$stmt_ins->closeCursor();
$meta_id = $connbas->lastInsertId();
$caption_field_value = new self($databox_field, $record, $meta_id);
$caption_field_value->update_cache_value($value);
$record->get_caption()->delete_data_from_cache();
return $caption_field_value;
}
}