1<?php 2/* 3 * vim:set softtabstop=4 shiftwidth=4 expandtab: 4 * 5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later) 6 * Copyright 2001 - 2020 Ampache.org 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Affero General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Affero General Public License for more details. 17 * 18 * You should have received a copy of the GNU Affero General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 */ 21 22namespace Ampache\Repository\Model\Metadata; 23 24use Ampache\Config\AmpConfig; 25use Ampache\Repository\Model\Metadata\Model\MetadataField; 26use ReflectionException; 27use Ampache\Module\Util\ObjectTypeToClassNameMapper; 28 29trait Metadata 30{ 31 /** 32 * 33 * @var Repository\Metadata 34 */ 35 protected $metadataRepository; 36 37 /** 38 * 39 * @var Repository\MetadataField 40 */ 41 protected $metadataFieldRepository; 42 43 /** 44 * Determines if the functionality is enabled or not. 45 * @var boolean 46 */ 47 protected $enableCustomMetadata; 48 49 /** 50 * Cache variable for disabled metadata field names 51 * @var array 52 */ 53 protected $disabledMetadataFields = array(); 54 55 /** 56 * Initialize the repository variables. Needs to be called first if the trait should do something. 57 */ 58 protected function initializeMetadata() 59 { 60 $this->metadataRepository = new Repository\Metadata(); 61 $this->metadataFieldRepository = new Repository\MetadataField(); 62 } 63 64 /** 65 * 66 * @return Model\Metadata 67 */ 68 public function getMetadata() 69 { 70 return $this->metadataRepository->findByObjectIdAndType($this->id, ucfirst(ObjectTypeToClassNameMapper::reverseMap(get_class($this)))); 71 } 72 73 /** 74 * 75 * @param Model\Metadata $metadata 76 */ 77 public function deleteMetadata(Model\Metadata $metadata) 78 { 79 $this->metadataRepository->remove($metadata); 80 } 81 82 /** 83 * 84 * @param MetadataField $field 85 * @param string $data 86 * @throws ReflectionException 87 */ 88 public function addMetadata(MetadataField $field, $data) 89 { 90 $metadata = new Model\Metadata(); 91 $metadata->setField($field); 92 $metadata->setObjectId($this->id); 93 $metadata->setType(ucfirst(ObjectTypeToClassNameMapper::reverseMap(get_class($this)))); 94 $metadata->setData($data); 95 $this->metadataRepository->add($metadata); 96 } 97 98 /** 99 * @param MetadataField $field 100 * @param $data 101 * @throws ReflectionException 102 */ 103 public function updateOrInsertMetadata(MetadataField $field, $data) 104 { 105 /* @var Model\Metadata $metadata */ 106 $metadata = $this->metadataRepository->findByObjectIdAndFieldAndType($this->id, $field, ucfirst(ObjectTypeToClassNameMapper::reverseMap(get_class($this)))); 107 if ($metadata) { 108 $object = reset($metadata); 109 $object->setData($data); 110 $this->metadataRepository->update($object); 111 } else { 112 $this->addMetadata($field, $data); 113 } 114 } 115 116 /** 117 * 118 * @param string $name 119 * @param boolean $public 120 * @return MetadataField 121 * @throws ReflectionException 122 */ 123 protected function createField($name, $public) 124 { 125 $field = new MetadataField(); 126 $field->setName($name); 127 if (!$public) { 128 $field->hide(); 129 } 130 $this->metadataFieldRepository->add($field); 131 132 return $field; 133 } 134 135 /** 136 * 137 * @param string $propertie 138 * @param boolean $public 139 * @return MetadataField 140 * @throws ReflectionException 141 */ 142 public function getField($propertie, $public = true) 143 { 144 $fields = $this->metadataFieldRepository->findByName($propertie); 145 if (count($fields)) { 146 $field = reset($fields); 147 } else { 148 $field = $this->createField($propertie, $public); 149 } 150 151 return $field; 152 } 153 154 /** 155 * 156 * @return boolean 157 */ 158 public static function isCustomMetadataEnabled() 159 { 160 return (boolean) AmpConfig::get('enable_custom_metadata'); 161 } 162 163 /** 164 * Get all disabled Metadata field names 165 * @return array 166 */ 167 public function getDisabledMetadataFields() 168 { 169 if (empty($this->disabledMetadataFields)) { 170 $fields = array(); 171 $ids = explode(',', AmpConfig::get('disabled_custom_metadata_fields')); 172 foreach ($ids as $metaid) { 173 $field = $this->metadataFieldRepository->findById($metaid); 174 if ($field) { 175 $fields[] = $field->getName(); 176 } 177 } 178 $this->disabledMetadataFields = array_merge( 179 $fields, explode(',', AmpConfig::get('disabled_custom_metadata_fields_input')) 180 ); 181 } 182 183 return $this->disabledMetadataFields; 184 } 185} 186