/** * Validates the attribute of the object. * If there is any error, the error message is added to the object. * @param \CModel $object the object being validated * @param string $attribute the attribute being validated */ protected function validateAttribute($object, $attribute) { $value = $object->{$attribute}; if ($this->allowEmpty && $this->isEmpty($value)) { return; } if ($this->integerOnly) { if (!preg_match($this->integerPattern, "{$value}")) { $message = $this->message !== null ? $this->message : Craft::t('“{attribute}” must be an integer.'); $this->addError($object, $attribute, $message); } } else { // For decimals, convert from a locale specific format to a normalized one before we check validity. $value = LocalizationHelper::normalizeNumber($value); if (!preg_match($this->numberPattern, "{$value}")) { $message = $this->message !== null ? $this->message : Craft::t('“{attribute}” must be a number.'); $this->addError($object, $attribute, $message); } } if (is_numeric($value)) { if ($this->min !== null && $value < $this->min) { $message = $this->tooSmall !== null ? $this->tooSmall : Craft::t('“{attribute}” is too small (minimum is {min}).'); $this->addError($object, $attribute, $message, array('{min}' => $this->min)); } if ($this->max !== null && $value > $this->max) { $message = $this->tooBig !== null ? $this->tooBig : Craft::t('“{attribute}” is too big (maximum is {max}).'); $this->addError($object, $attribute, $message, array('{max}' => $this->max)); } } }
/** * @inheritDoc IFieldType::prepValueFromPost() * * @param mixed $data * * @return mixed */ public function prepValueFromPost($data) { if ($data === '') { return 0; } else { return LocalizationHelper::normalizeNumber($data); } }
/** * Prepare fields for fieldtypes. * * @param string &$data * @param string $handle * * @return mixed */ public function prepForFieldType(&$data, $handle) { // Fresh up $data $data = StringHelper::convertToUTF8($data); $data = trim($data); // Get field info $field = craft()->fields->getFieldByHandle($handle); // If it's a field ofcourse if (!is_null($field)) { // For some fieldtypes the're special rules switch ($field->type) { case ImportModel::FieldTypeEntries: // No newlines allowed $data = str_replace("\n", '', $data); $data = str_replace("\r", '', $data); // Don't connect empty fields if (!empty($data)) { // Get field settings $settings = $field->getFieldType()->getSettings(); // Get source id's for connecting $sectionIds = array(); $sources = $settings->sources; if (is_array($sources)) { foreach ($sources as $source) { list($type, $id) = explode(':', $source); $sectionIds[] = $id; } } // Find matching element in sections $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->sectionId = $sectionIds; $criteria->limit = $settings->limit; // Get search strings $search = ArrayHelper::stringToArray($data); // Ability to import multiple Assets at once $data = array(); // Loop through keywords foreach ($search as $query) { // Search $criteria->search = $query; // Add to data $data = array_merge($data, $criteria->ids()); } } else { // Return empty array $data = array(); } break; case ImportModel::FieldTypeCategories: // Don't connect empty fields if (!empty($data)) { // Get field settings $settings = $field->getFieldType()->getSettings(); // Get source id $source = $settings->source; list($type, $id) = explode(':', $source); // Get category data $category = new CategoryModel(); $category->groupId = $id; // This we append before the slugified path $categoryUrl = str_replace('{slug}', '', $category->getUrlFormat()); // Find matching elements in categories $criteria = craft()->elements->getCriteria(ElementType::Category); $criteria->groupId = $id; $criteria->limit = $settings->limit; // Get search strings $search = ArrayHelper::stringToArray($data); // Ability to import multiple Categories at once $data = array(); // Loop through keywords foreach ($search as $query) { // Find matching element by URI (dirty, not all categories have URI's) $criteria->uri = $categoryUrl . $this->slugify($query); // Add to data $data = array_merge($data, $criteria->ids()); } } else { // Return empty array $data = array(); } break; case ImportModel::FieldTypeAssets: // Don't connect empty fields if (!empty($data)) { // Get field settings $settings = $field->getFieldType()->getSettings(); // Get folder id's for connecting $folderIds = array(); $folders = $settings->sources; if (is_array($folders)) { foreach ($folders as $folder) { list($type, $id) = explode(':', $folder); $folderIds[] = $id; } } // Find matching element in folders $criteria = craft()->elements->getCriteria(ElementType::Asset); $criteria->folderId = $folderIds; $criteria->limit = $settings->limit; // Get search strings $search = ArrayHelper::stringToArray($data); // Ability to import multiple Assets at once $data = array(); // Loop through keywords foreach ($search as $query) { // Search $criteria->search = $query; // Add to data $data = array_merge($data, $criteria->ids()); } } else { // Return empty array $data = array(); } break; case ImportModel::FieldTypeUsers: // Don't connect empty fields if (!empty($data)) { // Get field settings $settings = $field->getFieldType()->getSettings(); // Get group id's for connecting $groupIds = array(); $sources = $settings->sources; if (is_array($sources)) { foreach ($sources as $source) { list($type, $id) = explode(':', $source); $groupIds[] = $id; } } // Find matching element in sources $criteria = craft()->elements->getCriteria(ElementType::User); $criteria->groupId = $groupIds; $criteria->limit = $settings->limit; // Get search strings $search = ArrayHelper::stringToArray($data); // Ability to import multiple Users at once $data = array(); // Loop through keywords foreach ($search as $query) { // Search $criteria->search = $query; // Add to data $data = array_merge($data, $criteria->ids()); } } else { // Return empty array $data = array(); } break; case ImportModel::FieldTypeTags: // Get field settings $settings = $field->getFieldType()->getSettings(); // Get tag group id $source = $settings->getAttribute('source'); list($type, $groupId) = explode(':', $source); $tags = ArrayHelper::stringToArray($data); $data = array(); foreach ($tags as $tag) { // Find existing tag $criteria = craft()->elements->getCriteria(ElementType::Tag); $criteria->title = $tag; $criteria->groupId = $groupId; if (!$criteria->total()) { // Create tag if one doesn't already exist $newtag = new TagModel(); $newtag->getContent()->title = $tag; $newtag->groupId = $groupId; // Save tag if (craft()->tags->saveTag($newtag)) { $tagArray = array($newtag->id); } } else { $tagArray = $criteria->ids(); } // Add tags to data array $data = array_merge($data, $tagArray); } break; case ImportModel::FieldTypeNumber: // Parse as number $data = LocalizationHelper::normalizeNumber($data); // Parse as float $data = floatval($data); break; case ImportModel::FieldTypeDate: // Parse date from string $data = DateTimeHelper::formatTimeForDb(DateTimeHelper::fromString($data, craft()->timezone)); break; case ImportModel::FieldTypeRadioButtons: case ImportModel::FieldTypeDropdown: //get field settings $settings = $field->getFieldType()->getSettings(); //get field options $options = $settings->getAttribute('options'); // find matching option label $labelSelected = false; foreach ($options as $option) { if ($labelSelected) { continue; } if ($data == $option['label']) { $data = $option['value']; //stop looking after first match $labelSelected = true; } } break; case ImportModel::FieldTypeCheckboxes: case ImportModel::FieldTypeMultiSelect: // Convert to array $data = ArrayHelper::stringToArray($data); break; case ImportModel::FieldTypeLightSwitch: // Convert yes/no values to boolean switch ($data) { case Craft::t('Yes'): $data = true; break; case Craft::t('No'): $data = false; break; } break; } } return $data; }
public function prepNumber($data, $field) { return floatval(LocalizationHelper::normalizeNumber($data)); }
/** * Prepare fields for fieldtypes. * * @param string &$data * @param string $handle * * @return mixed */ public function prepForFieldType(&$data, $handle) { // Fresh up $data $data = StringHelper::convertToUTF8($data); $data = trim($data); // Get field info $field = craft()->fields->getFieldByHandle($handle); // If it's a field ofcourse if (!is_null($field)) { // For some fieldtypes the're special rules switch ($field->type) { case ImportModel::FieldTypeEntries: // No newlines allowed $data = str_replace("\n", '', $data); $data = str_replace("\r", '', $data); // Don't connect empty fields if (!empty($data)) { $data = $this->prepEntriesFieldType($data, $field); } else { $data = array(); } break; case ImportModel::FieldTypeCategories: if (!empty($data)) { $data = $this->prepCategoriesFieldType($data, $field); } else { $data = array(); } break; case ImportModel::FieldTypeAssets: if (!empty($data)) { $data = $this->prepAssetsFieldType($data, $field); } else { $data = array(); } break; case ImportModel::FieldTypeUsers: if (!empty($data)) { $data = $this->prepUsersFieldType($data, $field); } else { $data = array(); } break; case ImportModel::FieldTypeTags: $data = $this->prepTagsFieldType($data, $field); break; case ImportModel::FieldTypeNumber: // Parse as numberx $data = LocalizationHelper::normalizeNumber($data); // Parse as float $data = floatval($data); break; case ImportModel::FieldTypeDate: // Parse date from string $data = DateTimeHelper::formatTimeForDb(DateTimeHelper::fromString($data, craft()->timezone)); break; case ImportModel::FieldTypeRadioButtons: case ImportModel::FieldTypeDropdown: //get field settings $data = $this->prepDropDownFieldType($data, $field); break; case ImportModel::FieldTypeCheckboxes: case ImportModel::FieldTypeMultiSelect: // Convert to array $data = ArrayHelper::stringToArray($data); break; case ImportModel::FieldTypeLightSwitch: // Convert yes/no values to boolean switch ($data) { case Craft::t('Yes'): $data = true; break; case Craft::t('No'): $data = false; break; } break; } } return $data; }