1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class Tracker_Field_Icon extends Tracker_Field_Abstract 9{ 10 public static function getTypes() 11 { 12 return [ 13 'icon' => [ 14 'name' => tr('Icon'), 15 'description' => tr('Provide the ability to select an image as an icon attached to the tracker item from the file galleries.'), 16 'prefs' => ['trackerfield_icon', 'feature_file_galleries', 'feature_search'], 17 'tags' => ['advanced'], 18 'help' => 'Icon Tracker Field', 19 'default' => 'y', 20 'params' => [ 21 'galleryId' => [ 22 'name' => tr('Gallery ID'), 23 'description' => tr('File gallery to upload new files into.'), 24 'filter' => 'int', 25 'legacy_index' => 0, 26 'profile_reference' => 'file_gallery', 27 ], 28 'default' => [ 29 'name' => tr('Default image'), 30 'description' => tr('Path to the default icon used.'), 31 'filter' => 'url', 32 'legacy_index' => 1, 33 ], 34 'maxIcons' => [ 35 'name' => tr('Maximum Icons'), 36 'description' => tr('Number of icons to display in each gallery (default 120).'), 37 'filter' => 'int', 38 'default' => 120, 39 'legacy_index' => 2, 40 ], 41 'update' => [ 42 'name' => tr('Update icon event'), 43 'type' => 'list', 44 'description' => tr('Allow update during re-indexing. Selection of indexing is useful for changing the default icon for all items.'), 45 'filter' => 'word', 46 'options' => [ 47 'save' => tr('Save'), 48 'index' => tr('Indexing'), 49 ], 50 ], 51 ], 52 ], 53 ]; 54 } 55 56 function getFieldData(array $requestData = []) 57 { 58 $insertId = $this->getInsertId(); 59 60 if (isset($requestData[$insertId])) { 61 $value = $requestData[$insertId]; 62 } else { 63 $value = $this->getValue(); 64 } 65 66 if (! $value) { 67 $value = $this->getOption('default'); 68 } 69 70 if (! $value) { 71 $value = 'img/icons/plugin.png'; 72 } 73 74 return [ 75 'value' => $value, 76 ]; 77 } 78 79 private function getSearchLink($galleryId) 80 { 81 return 'tiki-searchindex.php?' . http_build_query( 82 [ 83 'filter~type' => 'file', 84 'filter~gallery_id' => $galleryId, 85 'filter~filetype' => 'image', 86 'maxRecords' => $this->getOption('maxIcons', 120), 87 'sort_mode' => 'title_asc', 88 ], 89 '', 90 '&' 91 ); 92 } 93 94 function renderInput($context = []) 95 { 96 $filegallib = TikiLib::lib('filegal'); 97 98 $galleryId = (int) $this->getOption('galleryId'); 99 $info = $filegallib->get_file_gallery_info($galleryId); 100 101 $galleries = [ 102 ['label' => $info['name'], 'url' => $this->getSearchLink($galleryId)], 103 ]; 104 105 $children = $filegallib->table('tiki_file_galleries')->fetchMap( 106 'galleryId', 107 'name', 108 ['parentId' => $galleryId], 109 -1, 110 -1, 111 ['name' => 'ASC'] 112 ); 113 foreach ($children as $galleryId => $name) { 114 $galleries[] = [ 115 'label' => $name, 116 'url' => $this->getSearchLink($galleryId), 117 ]; 118 } 119 120 return $this->renderTemplate( 121 'trackerinput/icon.tpl', 122 $context, 123 [ 124 'galleries' => $galleries, 125 ] 126 ); 127 } 128 129 function renderInnerOutput($context = []) 130 { 131 if ($context['list_mode'] === 'csv') { 132 return $this->getValue(); 133 } else { 134 return $this->renderTemplate('trackeroutput/icon.tpl', $context); 135 } 136 } 137 138 function handleSave($value, $oldValue) 139 { 140 $value = TikiLib::makeAbsoluteLinkRelative($value); 141 return [ 142 'value' => $value, 143 ]; 144 } 145 146 public static function updateIcon($args) 147 { 148 $definition = Tracker_Definition::get($args['trackerId']); 149 150 if ($definition && $fieldId = $definition->getIconField()) { 151 $value = isset($args['values'][$fieldId]) ? $args['values'][$fieldId] : null; 152 153 if (! empty($value) && isset($_SERVER['REQUEST_METHOD'])) { // leave URLs alone when run from a shell command 154 $value = TikiLib::lib('tiki')->tikiUrl($value); 155 $value = TikiLib::makeAbsoluteLinkRelative($value); 156 } 157 158 $attributelib = TikiLib::lib('attribute'); 159 $attributelib->set_attribute($args['type'], $args['object'], 'tiki.icon.src', $value); 160 } 161 } 162 163 function getDocumentPart(Search_Type_Factory_Interface $typeFactory) 164 { 165 if ('index' == $this->getOption('update')) { 166 $value = $this->getValue(); 167 if (empty($value)) { 168 $value = $this->getOption('default'); // value is often "" but default in getValue checks for isset 169 } 170 self::updateIcon([ 171 'trackerId' => $this->getConfiguration('trackerId'), 172 'type' => 'trackeritem', 173 'object' => $this->getItemId(), 174 'values' => [ 175 $this->getConfiguration('fieldId') => $value, 176 ], 177 ]); 178 } 179 180 $baseKey = $this->getBaseKey(); 181 return [ 182 $baseKey => $typeFactory->identifier($this->getValue()), 183 ]; 184 } 185} 186