/** * Hooked Function to check if a field is unique or unique restricted to a certain field * @param I2CE_FormField $field_obj * @returns boolean */ public function validate_formfield($field_obj) { if (!$field_obj->hasOption('unique') || !$field_obj->getOption('unique') || !$field_obj->isValid() || !$field_obj->issetValue() || $field_obj->getDBValue() == "") { return; } $where = array('operator' => 'FIELD_LIMIT', 'style' => 'equals', 'field' => $field_obj->getName(), 'data' => array('value' => $field_obj->getDBValue())); $form_obj = $field_obj->getContainer(); if (!$form_obj instanceof I2CE_Form) { I2CE::raiseError(get_class($form_obj)); return; } $names = array(); $search_parent = false; if ($field_obj->hasOption('unique_field')) { $unique = $field_obj->getOption('unique_field'); if (strpos($unique, ':') !== false) { //the value is a mapped thing. this is handled by hooked mehtod defined in I2CE_Module_List return; } $unique_fields = explode(',', $unique); $unique_where = array($where); foreach ($unique_fields as $unique_field) { if ($unique_field == "parent") { $search_parent = $form_obj->getParent(); if ($search_parent == "") { // If there is no parent then we can't validate // by parent so ignore this return; } continue; } if (!($unique_field_obj = $form_obj->getField($unique_field)) instanceof I2CE_FormField) { I2CE::raiseError("Invalid field {$unique_field}"); return; } if ($unique_field_obj->hasHeader('default')) { $names[] = $unique_field_obj->getHeader('default'); } else { $names[] = $unique_field_obj->getName(); } if ($unique_field_obj->isValid()) { $unique_where[] = array('operator' => 'FIELD_LIMIT', 'style' => 'equals', 'field' => $unique_field_obj->getName(), 'data' => array('value' => $unique_field_obj->getDBValue())); } else { $unique_where[] = array('operator' => 'OR', 'operand' => array(0 => array('operator' => 'FIELD_LIMIT', 'style' => 'equals', 'field' => $unique_field_obj->getName(), 'data' => array('value' => $unique_field_obj->getDBValue())), 1 => array('operator' => 'FIELD_LIMIT', 'style' => 'null', 'field' => $unique_field_obj->getName()))); } } if (count($unique_where) > 1) { //we added somehitng $where = array('operator' => 'AND', 'operand' => $unique_where); } } $found = I2CE_FormStorage::search($form_obj->getName(), $search_parent, $where, array(), 1); if ($found !== false && '' . $found != '' . $form_obj->getId()) { I2CE::raiseMessage("found is {$found} and id is " . $form_obj->getId() . " and names are " . print_r($names, true)); if (count($names) > 1) { $field_obj->setInvalidMessage('unique_fields', null, ' ' . implode(', ', $names)); //$field_obj->setInvalid("This must be unique and another record has this value for the given value of the fields " .implode(',',$names) ); } else { if (count($names) == 1) { $field_obj->setInvalidMessage('unique_field', null, ' ' . implode(', ', $names)); //$field_obj->setInvalid("This must be unique and another record has this value for the given value of the field " .implode(',',$names) ); } else { $field_obj->setInvalidMessage('unique'); //$field_obj->setInvalid("This must be unique and another record has this value." ); } } } }