Esempio n. 1
0
 /**
  * Notify the listener of any action on a record.
  *
  * This method is called by the framework for each action called on a
  * node. Depending on the actionfilter passed in the constructor, the
  * call is forwarded to the actionPerformed($action, $record) method.
  *
  * @param string $trigger The trigger being performed
  * @param array $record The record on which the trigger is performed
  * @param string $mode The mode (add/update)
  *
  * @return bool Result of operation.
  */
 public function notify($trigger, &$record, $mode = null)
 {
     if (method_exists($this, $trigger)) {
         Tools::atkdebug('Call listener ' . get_class($this) . " for trigger {$trigger} on " . $this->m_node->atkNodeUri() . ' (' . $this->m_node->primaryKey($record) . ')');
         return $this->{$trigger}($record, $mode);
     } else {
         return true;
     }
 }
Esempio n. 2
0
 /**
  * Check unique field combinations.
  * The function is called by the validate() method automatically. It is
  * not necessary to call this manually in a validation process.
  * Errors that are found are stored in the $record parameter.
  *
  * @param array $record The record to validate
  */
 public function validateUniqueFieldSets(&$record)
 {
     $db = $this->m_nodeObj->getDb();
     foreach ($this->m_nodeObj->m_uniqueFieldSets as $uniqueFieldSet) {
         $query = $db->createQuery();
         $query->addField('*');
         $query->addTable($this->m_nodeObj->m_table);
         $attribs = [];
         foreach ($uniqueFieldSet as $field) {
             $attrib = $this->m_nodeObj->m_attribList[$field];
             if ($attrib) {
                 $attribs[] = $attrib;
                 if (method_exists($attrib, 'createDestination') && isset($attrib->m_refKey) && is_array($attrib->m_refKey) && count($attrib->m_refKey) > 1) {
                     $attrib->createDestination();
                     foreach ($attrib->m_refKey as $refkey) {
                         $query->addCondition($query->quoteField($refkey) . " = '" . $db->escapeSQL($record[$attrib->fieldName()][$refkey]) . "'");
                     }
                 } else {
                     if (!$attrib->isNotNullInDb() && $attrib->isEmpty($record)) {
                         $query->addCondition($query->quoteField($field) . ' IS NULL');
                     } else {
                         $query->addCondition($query->quoteField($field) . " = '" . $attrib->value2db($record) . "'");
                     }
                 }
             } else {
                 Tools::atkerror("Field {$field} is mentioned in uniquefieldset but does not exist in " . $this->m_nodeObj->atkNodeUri());
             }
         }
         if ($this->m_mode != 'add') {
             $query->addCondition('NOT (' . $this->m_nodeObj->primaryKey($record) . ')');
         }
         if (count($db->getRows($query->buildSelect())) > 0) {
             Tools::atkTriggerError($record, $attribs, 'error_uniquefieldset');
         }
     }
 }
Esempio n. 3
0
 public function edit($record, $fieldprefix, $mode)
 {
     // There are 2 possibilities. Either we are going to search,
     // in which case we show a searchbox.
     // Or, a search has already been performed but multiple
     // matches have been found and an atkerror was set.
     // In this case, we show the selects.
     $select = false;
     if (isset($record['atkerror'])) {
         foreach ($record['atkerror'] as $error) {
             if ($error['attrib_name'] === $this->fieldName()) {
                 $select = true;
             }
         }
     }
     if ($select && $this->createSearchNodeInstance()) {
         $res = '';
         $notempty = false;
         // First lets get the results, which were lost during the redirect
         $this->m_matches = $this->getMatches($record[$this->fieldName()]);
         // Second check if we actually found anything
         if ($this->m_matches) {
             foreach ($this->m_matches as $match) {
                 if (!empty($match)) {
                     $notempty = true;
                     continue;
                 }
             }
             if (!$notempty) {
                 return Tools::atktext('no_results_found');
             }
         }
         if ($this->m_mode == 'multiselect' && count($this->m_matches) > 1) {
             $optionArray = $valueArray = [];
             foreach ($this->m_matches as $keyword => $matches) {
                 for ($i = 0, $_i = count($matches); $i < $_i; ++$i) {
                     $optionArray[] = $this->m_searchnodeInstance->descriptor($matches[$i]);
                     $valueArray[] = $this->m_searchnodeInstance->primaryKey($matches[$i]);
                 }
             }
             $attrib = new MultiSelectAttribute($this->m_name, $optionArray, $valueArray, 1, self::AF_NO_LABEL | MultiSelectAttribute::AF_CHECK_ALL | MultiSelectAttribute::AF_LINKS_BOTTOM);
             $res .= $attrib->edit($record, $fieldprefix, $mode);
         } else {
             if ($this->m_mode == 'select' || $this->m_mode == 'multiselect' && count($this->m_matches) == 1) {
                 // Select one record from all matches.
                 $res .= '<SELECT NAME="' . $this->getHtmlName($fieldprefix) . '[]" class="form-control select-standard">';
                 $res .= '<OPTION VALUE="">' . Tools::atktext('select_none');
                 $selects = [];
                 foreach ($this->m_matches as $keyword => $matches) {
                     for ($i = 0, $_i = count($matches); $i < $_i; ++$i) {
                         $item = '<OPTION VALUE="' . $this->m_searchnodeInstance->primaryKey($matches[$i]) . '">' . $this->m_searchnodeInstance->descriptor($matches[$i]);
                         if (!in_array($item, $selects)) {
                             $selects[] = $item;
                         }
                     }
                     $res .= implode("\n", $selects);
                 }
                 $res .= '</SELECT>';
             } else {
                 if ($this->m_mode == 'selectperkeyword') {
                     // Select one record per keyword.
                     $res = '<table border="0">';
                     foreach ($this->m_matches as $keyword => $matches) {
                         if (count($matches) > 0) {
                             $res .= '<tr><td>\'' . $keyword . '\': </td><td><SELECT NAME="' . $this->getHtmlName($fieldprefix) . '[]" class="form-control select-standard">';
                             $res .= '<OPTION VALUE="">' . Tools::atktext('select_none');
                             for ($i = 0, $_i = count($matches); $i < $_i; ++$i) {
                                 $res .= '<OPTION VALUE="' . $this->m_searchnodeInstance->primaryKey($matches[$i]) . '">' . $this->m_searchnodeInstance->descriptor($matches[$i]);
                             }
                             $res .= '</SELECT></td></tr>';
                         }
                     }
                     $res .= '</table>';
                 }
             }
         }
         return $res;
     } else {
         $record = '';
         // clear the record so we always start with an empty
         // searchbox.
         return parent::edit($record, $fieldprefix, $mode);
     }
 }