1: <?php
2:
3: 4: 5:
6: class Quform_Validator_Duplicate extends Quform_Validator_Abstract
7: {
8: const IS_DUPLICATE = 'isDuplicate';
9:
10: 11: 12: 13: 14:
15: protected $element;
16:
17: 18: 19: 20: 21:
22: protected $repository;
23:
24: 25: 26:
27: public function __construct(array $options = array())
28: {
29: if ( ! array_key_exists('element', $options) || ! $options['element'] instanceof Quform_Element_Field) {
30: throw new InvalidArgumentException("Option 'element' is required and must be an instance of Quform_Element_Field");
31: }
32:
33: if ( ! array_key_exists('repository', $options) || ! $options['repository'] instanceof Quform_Repository) {
34: throw new InvalidArgumentException("Option 'repository' is required and must be an instance of Quform_Repository");
35: }
36:
37: $this->element = $options['element'];
38: $this->repository = $options['repository'];
39: unset($options['element'], $options['repository']);
40:
41: parent::__construct($options);
42: }
43:
44: 45: 46: 47: 48: 49: 50:
51: public function isValid($value)
52: {
53: $this->reset();
54:
55: if ($this->repository->hasDuplicateEntry($this->element)) {
56: $this->error(self::IS_DUPLICATE);
57: return false;
58: }
59:
60: return true;
61: }
62:
63: 64: 65: 66: 67: 68:
69: public static function getMessageTemplates($key = null)
70: {
71: $messageTemplates = array(
72: self::IS_DUPLICATE => __('This value is a duplicate of a previously submitted form', 'quform')
73: );
74:
75: if (is_string($key)) {
76: return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
77: }
78:
79: return $messageTemplates;
80: }
81:
82: 83: 84: 85: 86: 87:
88: public static function getDefaultConfig($key = null)
89: {
90: $config = apply_filters('quform_default_config_validator_duplicate', array(
91: 'messages' => array(
92: self::IS_DUPLICATE => ''
93: )
94: ));
95:
96: $config['type'] = 'duplicate';
97:
98: if (Quform::isNonEmptyString($key)) {
99: return Quform::get($config, $key);
100: }
101:
102: return $config;
103: }
104: }
105: