Skip to content

Commit

Permalink
Fixed bug with control replication
Browse files Browse the repository at this point in the history
Fixed a bug not correctly replicating controls on pressing 'add input' button, plus tidied up code
  • Loading branch information
DBinaghi committed Mar 9, 2020
1 parent ead2167 commit 22ff366
Showing 1 changed file with 63 additions and 50 deletions.
113 changes: 63 additions & 50 deletions libraries/SimpleVocabPlus/Controller/Plugin/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,73 @@
*/
class SimpleVocabPlus_Controller_Plugin_Autosuggest extends Zend_Controller_Plugin_Abstract
{
/**
* Include all routes (route + controller + actions) that render an
* element form, including actions requested via AJAX.
*
* @var array
*/
protected $_defaultRoutes = array(
array(
'module' => 'default',
'controller' => 'items',
'actions' => array('add', 'edit', 'change-type')
),
array(
'module' => 'default',
'controller' => 'elements',
'actions' => array('element-form')
)
);

/**
* Cached vocab terms.
*/
protected $_svpTerms;
protected $_elementText;

/**
* Add autosuggest only during defined routes.
*/
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$db = get_db();

// Set NULL modules to default. Some routes do not have a default
// module, which resolves to NULL.
$module = $request->getModuleName();
if (is_null($module)) {
$module = 'default';
}
$controller = $request->getControllerName();
$action = $request->getActionName();
/**
* Add autosuggest only during defined routes.
*/
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$db = get_db();

// Include all routes (route + controller + actions) that render an
// element form, including actions requested via AJAX.
$routes = array(
array(
'module' => 'default',
'controller' => 'items',
'actions' => array('add', 'edit', 'element-form', 'change-type'),
),
);
// Set NULL modules to default. Some routes do not have a default
// module, which resolves to NULL.
$module = $request->getModuleName();
if (is_null($module)) {
$module = 'default';
}
$controller = $request->getControllerName();
$action = $request->getActionName();

$filterFiles = get_option('simple_vocab_plus_files');
if ($filterFiles) {
// Add the file add/edit route if configured to.
$routes[] = array(
$this->_defaultRoutes[] = array(
'module' => 'default',
'controller' => 'files',
'actions' => array('add', 'edit')
);
}

// Allow plugins to add routes that contain form inputs rendered by
// Omeka_View_Helper_ElementForm::_displayFormInput().
$routes = apply_filters('svp_suggest_routes', $routes);
// Allow plugins to add routes that contain form inputs rendered by
// Omeka_View_Helper_ElementForm::_displayFormInput().
$routes = apply_filters('svp_suggest_routes', $this->_defaultRoutes);

// Iterate the defined routes.
foreach ($routes as $route) {
// Set the autosuggest if the current action matches a defined route.
if ($route['module'] === $module
&& $route['controller'] === $controller
&& in_array($action, $route['actions'])
) {
// Iterate the elements that are assigned to a suggest endpoint.
$svpAssigns = $db->getTable('SvpAssign')->findAll();
// Iterate the defined routes.
foreach ($routes as $route) {
// Set the autosuggest if the current action matches a defined route.
if ($route['module'] === $module
&& $route['controller'] === $controller
&& in_array($action, $route['actions'])
) {
// Iterate the elements that are assigned to a suggest endpoint.
$svpAssigns = $db->getTable('SvpAssign')->findAll();
foreach ($svpAssigns as $svpAssign) {
$element = $db->getTable('Element')->find($svpAssign->element_id);
$elementSet = $db->getTable('ElementSet')->find($element->element_set_id);
$element = $db->getTable('Element')->find($svpAssign->element_id);
$elementSet = $db->getTable('ElementSet')->find($element->element_set_id);
$elementTextTable = $db->getTable('ElementText');
$svpTermTable = $db->getTable('SvpTerm');

Expand All @@ -81,10 +90,10 @@ public function preDispatch(Zend_Controller_Request_Abstract $request)
?>
// Add autosuggest to <?php echo $elementSet->name . ':' . $element->name; ?>. Used by the Simple Vocab Plus plugin.
jQuery(document).bind('omeka:elementformload', function(event) {
jQuery('#element-<?php echo $element->id; ?> textarea').autocomplete({
minLength: 2,
source: <?php echo json_encode($view->url('simple-vocab-plus/endpoint/suggest-proxy/element-id/' . $element->id)); ?>
});
jQuery('#element-<?php echo $element->id; ?> textarea').autocomplete({
minLength: 2,
source: <?php echo json_encode($view->url('simple-vocab-plus/endpoint/suggest-proxy/element-id/' . $element->id)); ?>
});
});
<?php
$view->headScript()->captureEnd();
Expand All @@ -108,12 +117,16 @@ public function preDispatch(Zend_Controller_Request_Abstract $request)
}
}

add_filter(array('ElementInput', 'Item', $elementSet->name, $element->name),
array($this, 'filterElementInput'));
add_filter(
array('ElementInput', 'Item', $elementSet->name, $element->name),
array($this, 'filterElementInput')
);
// Add the file filter if configured to.
if ($filterFiles) {
// Add the file filter if configured to.
add_filter(array('ElementInput', 'File', $elementSet->name, $element->name),
array($this, 'filterElementInput'));
add_filter(
array('ElementInput', 'File', $elementSet->name, $element->name),
array($this, 'filterElementInput')
);
}
}
}
Expand All @@ -129,10 +142,10 @@ public function preDispatch(Zend_Controller_Request_Abstract $request)
*/
public function filterElementInput($components, $args)
{
// Use the cached vocab terms
$hcolor = get_option('simple_vocab_plus_fields_highlight');
$hcolor = (preg_match('/#([a-f0-9]{3}){1,2}\b/i', $hcolor) ? 'background-color: ' . $hcolor : '');

// Use the cached vocab terms
if (empty($this->_svpTerms[$args['element']->id])) {
// case autosuggest, values not enforced
$components['input'] = get_view()->formTextarea(
Expand All @@ -158,4 +171,4 @@ public function filterElementInput($components, $args)

return $components;
}
}
}

0 comments on commit 22ff366

Please sign in to comment.