1 <?php
  2 
  3 /**
  4  * @package redaxo\core\form
  5  */
  6 class rex_form_prio_element extends rex_form_select_element
  7 {
  8     private $labelField;
  9     private $labelCallback;
 10     private $whereCondition;
 11     private $primaryKey;
 12     private $firstOptionMsg;
 13     private $optionMsg;
 14 
 15     // 1. Parameter nicht genutzt, muss aber hier stehen,
 16     // wg einheitlicher Konstrukturparameter
 17     public function __construct($tag = '', rex_form_base $table = null, array $attributes = [])
 18     {
 19         parent::__construct('', $table, $attributes);
 20 
 21         $this->labelField = '';
 22         $this->whereCondition = '';
 23         $this->primaryKey = 'id';
 24         $this->firstOptionMsg = 'form_field_first_priority';
 25         $this->optionMsg = 'form_field_after_priority';
 26         $this->select->setSize(1);
 27 
 28         rex_extension::register('REX_FORM_SAVED', [$this, 'organizePriorities']);
 29     }
 30 
 31     /**
 32      * Setzt die Datenbankspalte, die das Label für die zu priorisierenden Elemente darstellt.
 33      *
 34      * @param string $labelField
 35      */
 36     public function setLabelField($labelField)
 37     {
 38         $this->labelField = $labelField;
 39     }
 40 
 41     public function setLabelCallback(callable $labelCallback)
 42     {
 43         $this->labelCallback = $labelCallback;
 44     }
 45 
 46     public function setWhereCondition($whereCondition)
 47     {
 48         $this->whereCondition = $whereCondition;
 49     }
 50 
 51     public function setPrimaryKey($primaryKey)
 52     {
 53         $this->primaryKey = $primaryKey;
 54     }
 55 
 56     public function formatElement()
 57     {
 58         $name = $this->getFieldName();
 59 
 60         $qry = 'SELECT ' . $this->labelField . ',' . $name . ' FROM ' . $this->table->getTableName() . ' WHERE 1=1';
 61         if ($this->whereCondition != '') {
 62             $qry .= ' AND (' . $this->whereCondition . ')';
 63         }
 64 
 65         // Im Edit Mode das Feld selbst nicht als Position einfügen
 66         if ($this->table->isEditMode()) {
 67             $qry .= ' AND (' . $name . '!=' . $this->getValue() . ')';
 68         }
 69 
 70         $qry .= ' ORDER BY ' . $name;
 71         $sql = rex_sql::factory();
 72         $sql->setQuery($qry);
 73 
 74         $this->select->addOption(rex_i18n::msg($this->firstOptionMsg), 1);
 75         $value = 1;
 76         foreach ($sql as $opt) {
 77             $value = $opt->getValue($name) + 1;
 78             $label = $opt->getValue($this->labelField);
 79 
 80             if ($this->labelCallback) {
 81                 $label = call_user_func($this->labelCallback, $label);
 82             }
 83 
 84             $this->select->addOption(rex_i18n::rawMsg($this->optionMsg, $label), $value);
 85         }
 86         if (!$this->table->isEditMode()) {
 87             $this->select->setSelected($value);
 88         }
 89 
 90         return parent::formatElement();
 91     }
 92 
 93     public function organizePriorities(rex_extension_point $ep)
 94     {
 95         if ($this->table->equals($ep->getParam('form'))) {
 96             $name = $this->getFieldName();
 97 
 98             rex_sql_util::organizePriorities(
 99                 $this->table->getTableName(),
100                 $name,
101                 $this->whereCondition,
102                 $name . ', updatedate desc'
103             );
104         }
105     }
106 }
107