protected function retrieve_fields() { if (is_array($this->fields)) { return $this->fields; } $fields = []; try { $fields = $this->get_data_from_cache(); } catch (\Exception $e) { $sql = "SELECT m.id as meta_id, s.id as structure_id\n FROM metadatas m, metadatas_structure s\n WHERE m.record_id = :record_id AND s.id = m.meta_struct_id\n ORDER BY s.sorter ASC"; $stmt = $this->databox->get_connection()->prepare($sql); $stmt->execute([':record_id' => $this->record->get_record_id()]); $fields = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); $this->set_data_to_cache($fields); } $rec_fields = []; foreach ($fields as $row) { $databox_meta_struct = databox_field::get_instance($this->app, $this->databox, $row['structure_id']); $metadata = new caption_field($this->app, $databox_meta_struct, $this->record); $rec_fields[$databox_meta_struct->get_id()] = $metadata; } $this->fields = $rec_fields; return $this->fields; }
public function setUp() { parent::setUp(); if (!self::$initialized) { $found = false; foreach (self::$DI['record_2']->get_databox()->get_meta_structure()->get_elements() as $field) { if (!$field->isBusiness()) { continue; } $found = true; } if (!$found) { $field = \databox_field::create(self::$DI['app'], self::$DI['record_2']->get_databox(), 'testBusiness' . mt_rand(), false); $field->set_business(true); $field->save(); } foreach (self::$DI['app']['phraseanet.appbox']->get_databoxes() as $databox) { break; } } $this->initialize(); if (!self::$searchEngine instanceof SearchEngineInterface) { $this->markTestSkipped('Unable to initialize search Engine'); } $options = new SearchEngineOptions(); $options->onCollections($databox->get_collections()); $this->options = $options; }
public function testRenameField() { $AddedValue = 'scalar value'; self::$DI['record_1']->set_metadatas([['meta_id' => null, 'meta_struct_id' => $this->object_mono->get_id(), 'value' => $AddedValue]]); $this->object_mono->set_name('Bonobo yoyo')->save(); $data = self::$DI['record_1']->get_caption()->get_field('Bonoboyoyo')->get_values(); $value = array_pop($data); $this->assertEquals($value->getValue(), $AddedValue); }
/** * Part of the cache_cacheableInterface * * @param string $option * @return caption_field */ public function delete_data_from_cache($option = null) { $this->value = $this->VocabularyId = $this->VocabularyType = null; $this->record->delete_data_from_cache(record_adapter::CACHE_TITLE); try { $this->record->get_caption()->get_field($this->databox_field->get_name())->delete_data_from_cache(); } catch (\Exception $e) { } unset(self::$localCache[$this->get_cache_key($option)]); }
/** * * @param string $name * @return databox_field */ public function get_element_by_name($name) { $name = databox_field::generateName($name); if (isset($this->cache_name_id[$name])) { return $this->elements[$this->cache_name_id[$name]]; } foreach ($this->elements as $id => $meta) { if ($meta->get_name() === $name) { $this->cache_name_id[$name] = $id; return $meta; } } return null; }
/** * {@inheritdoc} */ public function apply(base $databox, Application $app) { /** * Fail if upgrade has previously failed, no problem */ try { $sql = "ALTER TABLE `metadatas`\n ADD `updated` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '1',\n ADD INDEX ( `updated` )"; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $stmt->closeCursor(); $sql = 'UPDATE metadatas SET updated = "0" WHERE meta_struct_id IN ( SELECT id FROM metadatas_structure WHERE multi = "1" )'; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $stmt->closeCursor(); } catch (DBALException $e) { } try { $sql = 'ALTER TABLE `metadatas` DROP INDEX `unique`'; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $stmt->closeCursor(); } catch (DBALException $e) { } $sql = 'SELECT m . * FROM metadatas_structure s, metadatas m WHERE m.meta_struct_id = s.id AND s.multi = "1" AND updated="0"'; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $rowCount = $stmt->rowCount(); $stmt->closeCursor(); $n = 0; $perPage = 1000; while ($n < $rowCount) { $sql = 'SELECT m . * FROM metadatas_structure s, metadatas m WHERE m.meta_struct_id = s.id AND s.multi = "1" LIMIT ' . $n . ', ' . $perPage; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); $databox->get_connection()->beginTransaction(); $sql = 'INSERT INTO metadatas(id, record_id, meta_struct_id, value) VALUES (null, :record_id, :meta_struct_id, :value)'; $stmt = $databox->get_connection()->prepare($sql); $databox_fields = []; foreach ($rs as $row) { $meta_struct_id = $row['meta_struct_id']; if (!isset($databox_fields[$meta_struct_id])) { $databox_fields[$meta_struct_id] = \databox_field::get_instance($app, $databox, $meta_struct_id); } $values = \caption_field::get_multi_values($row['value'], $databox_fields[$meta_struct_id]->get_separator()); foreach ($values as $value) { $params = [':record_id' => $row['record_id'], ':meta_struct_id' => $row['meta_struct_id'], ':value' => $value]; $stmt->execute($params); } } $stmt->closeCursor(); $sql = 'DELETE FROM metadatas WHERE id = :id'; $stmt = $databox->get_connection()->prepare($sql); foreach ($rs as $row) { $params = [':id' => $row['id']]; $stmt->execute($params); } $stmt->closeCursor(); $databox->get_connection()->commit(); $n += $perPage; } /** * Remove the extra column */ try { $sql = "ALTER TABLE `metadatas` DROP `updated`"; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $stmt->closeCursor(); } catch (\Exception $e) { } return true; }
/** * @todo move this function to caption_record * * @param array $metadatas * @param boolean $force_readonly * * @return record_adapter */ public function set_metadatas(array $metadatas, $force_readonly = false) { foreach ($metadatas as $param) { if (!is_array($param)) { throw new Exception_InvalidArgument('Invalid metadatas argument'); } $db_field = \databox_field::get_instance($this->app, $this->get_databox(), $param['meta_struct_id']); if ($db_field->is_readonly() === true && !$force_readonly) { continue; } $this->set_metadata($param, $this->databox); } $this->xml = null; $this->caption_record = null; $xml = new DOMDocument(); $xml->loadXML($this->app['serializer.caption']->serialize($this->get_caption(), CaptionSerializer::SERIALIZE_XML, true)); $this->set_xml($xml); $this->reindex(); unset($xml); return $this; }
public function tearDown() { ACLProvider::purge(); \collection::purge(); \databox::purge(); \caption_field::purge(); \caption_Field_Value::purge(); \databox_field::purge(); \databox_status::purge(); \thesaurus_xpath::purge(); /** * Kris Wallsmith pro-tip * @see http://kriswallsmith.net/post/18029585104/faster-phpunit */ $refl = new ReflectionObject($this); foreach ($refl->getProperties() as $prop) { if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_') && 0 !== strpos($prop->getDeclaringClass()->getName(), 'Phraseanet')) { $prop->setAccessible(true); $prop->setValue($this, null); } } $refl = null; parent::tearDown(); //In case some executed script modify 'max_execution_time' ini var //Initialize set_time_limit(0) which is the default value for PHP CLI set_time_limit(0); }
public function testDeleteField() { $databoxes = self::$DI['app']['phraseanet.appbox']->get_databoxes(); $databox = array_shift($databoxes); $field = \databox_field::create(self::$DI['app'], $databox, 'testfield' . mt_rand(), false); $fieldId = $field->get_id(); $data = $field->toArray(); $data['business'] = true; $data['vocabulary-type'] = 'User'; self::$DI['client']->request("DELETE", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()), [], [], [], json_encode($data)); $response = self::$DI['client']->getResponse()->getContent(); $this->assertEquals('', $response); $this->assertEquals(204, self::$DI['client']->getResponse()->getStatusCode()); try { \databox_field::get_instance(self::$DI['app'], $databox, $fieldId); $this->fail('Should have raise an exception'); } catch (\Exception $e) { } }
/** * {@inheritdoc} */ public function asString() { return serialize(['id' => $this->databox_field->get_id(), 'sbas_id' => $this->databox_field->get_databox()->get_sbas_id(), 'value' => $this->value]); }
private function validateTagField(array $field) { try { \databox_field::loadClassFromTagName($field['tag'], true); } catch (\Exception_Databox_metadataDescriptionNotFound $e) { throw new BadRequestHttpException(sprintf('Provided tag %s is unknown.', $field['tag'])); } }
/** * Retrieve informations about one \databox metadata field * * @param \databox_field $databox_field * * @return array */ private function list_databox_metadata_field_properties(\databox_field $databox_field) { return ['id' => $databox_field->get_id(), 'namespace' => $databox_field->get_tag()->getGroupName(), 'source' => $databox_field->get_tag()->getTagname(), 'tagname' => $databox_field->get_tag()->getName(), 'name' => $databox_field->get_name(), 'labels' => ['fr' => $databox_field->get_label('fr'), 'en' => $databox_field->get_label('en'), 'de' => $databox_field->get_label('de'), 'nl' => $databox_field->get_label('nl')], 'separator' => $databox_field->get_separator(), 'thesaurus_branch' => $databox_field->get_tbranch(), 'type' => $databox_field->get_type(), 'indexable' => $databox_field->is_indexable(), 'multivalue' => $databox_field->is_multi(), 'readonly' => $databox_field->is_readonly(), 'required' => $databox_field->is_required()]; }
public function displayMetaRestrictions(Application $app, $sbas_id, $field_id) { $databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $field = \databox_field::get_instance($app, $databox, $field_id); return $app['twig']->render('prod/Tooltip/DataboxFieldRestrictions.html.twig', ['field' => $field]); }
/** * * @param Application $app * @param string $serialized * * @return SearchEngineOptions * * @throws \InvalidArgumentException * @throws \RuntimeException */ public static function hydrate(Application $app, $serialized) { $serialized = json_decode($serialized, true); if (!is_array($serialized)) { throw new \InvalidArgumentException('SearchEngineOptions data are corrupted'); } $options = new static(); $options->disallowBusinessFields(); foreach ($serialized as $key => $value) { switch (true) { case is_null($value): $value = null; break; case in_array($key, ['date_min', 'date_max']): $value = \DateTime::createFromFormat(DATE_ATOM, $value); break; case $value instanceof stdClass: $tmpvalue = (array) $value; $value = []; foreach ($tmpvalue as $k => $data) { $k = ctype_digit($k) ? (int) $k : $k; $value[$k] = $data; } break; case in_array($key, ['date_fields', 'fields']): $value = array_map(function ($serialized) use($app) { $data = explode('_', $serialized); return \databox_field::get_instance($app, $app['phraseanet.appbox']->get_databox($data[0]), $data[1]); return \collection::get_from_base_id($app, $base_id); }, $value); break; case in_array($key, ['collections', 'business_fields']): $value = array_map(function ($base_id) use($app) { return \collection::get_from_base_id($app, $base_id); }, $value); break; } $sort_by = $sort_ord = null; switch ($key) { case 'record_type': $options->setRecordType($value); break; case 'search_type': $options->setSearchType($value); break; case 'status': $options->setStatus($value); break; case 'date_min': $options->setMinDate($value); break; case 'date_max': $options->setMaxDate($value); break; case 'i18n': if ($value) { $options->setLocale($value); } break; case 'stemming': $options->setStemming($value); break; case 'sort_by': $sort_by = $value; break; case 'sort_ord': $sort_ord = $value; break; case 'date_fields': $options->setDateFields($value); break; case 'fields': $options->setFields($value); break; case 'collections': $options->onCollections($value); break; case 'business_fields': $options->allowBusinessFieldsOn($value); break; default: throw new \RuntimeException(sprintf('Unable to handle key `%s`', $key)); break; } } if ($sort_by) { if ($sort_ord) { $options->setSort($sort_by, $sort_ord); } else { $options->setSort($sort_by); } } return $options; }
public function testDeleteField() { $app = $this->getApplication(); $databox = $this->getFirstDatabox($app); $field = \databox_field::create($app, $databox, 'testfield' . mt_rand(), false); $fieldId = $field->get_id(); $data = $field->toArray(); $data['business'] = true; $data['vocabulary-type'] = 'User'; /** @var Client $client */ $client = self::$DI['client']; $client->request("DELETE", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()), [], [], [], json_encode($data)); $response = $client->getResponse()->getContent(); $this->assertEquals('', $response); $this->assertEquals(204, $client->getResponse()->getStatusCode()); try { $databox->get_meta_structure()->get_element($fieldId); $this->fail('Should have raise an exception'); } catch (\Exception $e) { } }
public function feed_meta_fields() { $sxe = $this->get_sxml_structure(); foreach ($sxe->description->children() as $fname => $field) { $dom_struct = $this->get_dom_structure(); $xp_struct = $this->get_xpath_structure(); $fname = (string) $fname; $src = trim(isset($field['src']) ? str_replace('/rdf:RDF/rdf:Description/', '', $field['src']) : ''); $meta_id = isset($field['meta_id']) ? $field['meta_id'] : null; if (!is_null($meta_id)) { continue; } $nodes = $xp_struct->query('/record/description/' . $fname); if ($nodes->length > 0) { $nodes->item(0)->parentNode->removeChild($nodes->item(0)); } $this->saveStructure($dom_struct); $type = isset($field['type']) ? $field['type'] : 'string'; $type = in_array($type, [databox_field::TYPE_DATE, databox_field::TYPE_NUMBER, databox_field::TYPE_STRING, databox_field::TYPE_TEXT]) ? $type : databox_field::TYPE_STRING; $multi = isset($field['multi']) ? (bool) (string) $field['multi'] : false; $meta_struct_field = databox_field::create($this->app, $this, $fname, $multi); $meta_struct_field->set_readonly(isset($field['readonly']) ? (string) $field['readonly'] : 0)->set_indexable(isset($field['index']) ? (string) $field['index'] : '1')->set_separator(isset($field['separator']) ? (string) $field['separator'] : '')->set_required(isset($field['required']) && (string) $field['required'] == 1)->set_business(isset($field['business']) && (string) $field['business'] == 1)->set_type($type)->set_tbranch(isset($field['tbranch']) ? (string) $field['tbranch'] : '')->set_thumbtitle(isset($field['thumbtitle']) ? (string) $field['thumbtitle'] : (isset($field['thumbTitle']) ? $field['thumbTitle'] : '0'))->set_report(isset($field['report']) ? (string) $field['report'] : '1')->save(); try { $meta_struct_field->set_tag(\databox_field::loadClassFromTagName($src))->save(); } catch (\Exception $e) { } } return $this; }
public static function purge() { self::$_instance = []; }
/** * Part of the cache_cacheableInterface * * @param string $option * @return string */ public function get_cache_key($option = null) { return 'caption_field_' . $this->databox_field->get_id() . '_' . $this->record->get_serialize_key() . ($option ? '_' . $option : ''); }
public function testSet_record_metadatas() { $databox = self::$DI['record_1']->get_databox(); $request = new Request(["salut" => "salut c'est la fete"], [], [], [], [], ['HTTP_Accept' => 'application/json']); $result = $this->object->set_record_metadatas($request, self::$DI['record_1']->get_sbas_id(), self::$DI['record_1']->get_record_id()); $this->assertEquals(400, $result->get_http_code()); $request = new Request(["metadatas" => "salut c'est la fete"], [], [], [], [], ['HTTP_Accept' => 'application/json']); $this->object->set_record_metadatas($request, self::$DI['record_1']->get_sbas_id(), self::$DI['record_1']->get_record_id()); $this->assertEquals(400, $result->get_http_code()); if (sizeof(self::$DI['record_1']->get_caption()->get_fields()) == 0) { $caption_field_value = caption_Field_Value::create(self::$DI['app'], databox_field::get_instance(self::$DI['app'], $databox, 1), self::$DI['record_1'], 'my value'); } $metadatas = []; foreach (self::$DI['record_1']->get_databox()->get_meta_structure()->get_elements() as $field) { try { $values = self::$DI['record_1']->get_caption()->get_field($field->get_name())->get_values(); $value = array_pop($values); $meta_id = $value->getId(); } catch (\Exception $e) { $meta_id = null; } $metadatas[] = ['meta_id' => $meta_id, 'meta_struct_id' => $field->get_id(), 'value' => 'poOM POOM TCHOK ' . $field->get_id()]; } $request = new Request(["metadatas" => $metadatas], [], [], [], [], ['HTTP_Accept' => 'application/json']); $result = $this->object->set_record_metadatas($request, self::$DI['record_1']->get_sbas_id(), self::$DI['record_1']->get_record_id()); $response = json_decode($result->format(), true); $this->assertEquals($response['meta']['http_code'], 200); $this->checkResponseField($result, "record_metadatas", 'array'); }