/**
  * Create dummies.
  *
  * @param AmSeed_GeneratorModel $generator
  * @param int                   $totalDummies [Optional] Amount of dummies to generate.
  *
  * @return bool
  */
 public function createDummies(AmSeed_GeneratorModel $generator, $totalDummies = 100)
 {
     // Remember the generator
     $this->_generator = $generator;
     // Get the service and its save method
     $this->_service = craft()->amSeed_settings->getSettingsValueByHandleAndType(StringHelper::toCamelCase($generator->elementType . ' Service'), AmSeedModel::SettingElementTypes, false);
     $this->_saveMethod = craft()->amSeed_settings->getSettingsValueByHandleAndType(StringHelper::toCamelCase($generator->elementType . ' Method'), AmSeedModel::SettingElementTypes, false);
     if (!$this->_service || !$this->_saveMethod) {
         $this->_log['errors'][] = array('Service and save method not set in settings.');
         return false;
     }
     // Get locales to add
     $this->_getLocales();
     // Get element criteria
     $criteria = craft()->elements->getCriteria($generator->elementType);
     // Get element type
     $this->_elementType = $criteria->getElementType();
     // Set element source
     if ($this->_getGeneratorSetting('source')) {
         $source = $this->_elementType->getSource($this->_getGeneratorSetting('source'));
         // Does the source specify any criteria attributes?
         if ($source && !empty($source['criteria'])) {
             $criteria->setAttributes($source['criteria']);
         }
     }
     // Set locale
     $criteria->locale = $this->_locales[0];
     // Get element model
     $elementModel = $this->_elementType->populateElementModel((array) $criteria->getAttributes());
     if (!$elementModel) {
         $this->_log['errors'][] = array('Could not populate an Element Model for this Element Type.');
         return false;
     }
     // Get field layout
     $this->_getFieldLayoutFields($elementModel->getFieldLayout());
     // Generate dummies!
     $this->_randomTexts = array();
     // Reset for new texts
     craft()->config->maxPowerCaptain();
     craft()->config->set('cacheElementQueries', false);
     for ($i = 0; $i < (int) $totalDummies; $i++) {
         // Update counter
         $this->_counter = $i + 1;
         // Create new model
         $elementModel = $this->_elementType->populateElementModel((array) $criteria->getAttributes());
         // Create dummy!
         if ($this->_createDummy($elementModel)) {
             $this->_log['success']++;
         } else {
             $this->_log['failed']++;
             $this->_log['errors'] = $elementModel->getErrors();
         }
     }
     return true;
 }
 public function actionGetGroupFieldHtml()
 {
     $this->requirePostRequest();
     $this->requireAjaxRequest();
     $groupId = craft()->request->getRequiredPost('groupId');
     $template = craft()->request->getRequiredPost('template');
     $group = craft()->fields->getGroupById($groupId);
     $variables = array('group' => $group, 'prefix' => StringHelper::toCamelCase($group->name) . '_');
     $returnData['html'] = $this->renderTemplate('fieldmanager/_group/' . $template, $variables, true);
     $this->returnJson($returnData);
 }
 /**
  * Get available element types.
  *
  * @param bool $checkSettings [Optional] Check whether the settings have been set.
  *
  * @return array
  */
 public function getElementTypes($checkSettings = false)
 {
     $elementTypes = array();
     if ($checkSettings) {
         $typeSettings = craft()->amSeed_settings->getAllSettingsByType(AmSeedModel::SettingElementTypes);
     }
     foreach ($this->_allElementTypes as $type => $elementType) {
         // Ignore some
         if (in_array($type, $this->_ignoreElementTypes)) {
             continue;
         }
         // Check for settings?
         if ($checkSettings) {
             // Settings for this Element Type are available?
             $typeCamel = StringHelper::toCamelCase($type);
             if (!isset($typeSettings[$typeCamel . 'Service']) || !isset($typeSettings[$typeCamel . 'Method']) || empty($typeSettings[$typeCamel . 'Service']->value) || empty($typeSettings[$typeCamel . 'Method']->value)) {
                 continue;
             }
         }
         $elementTypes[$type] = $elementType->name;
     }
     return $elementTypes;
 }
 /**
  * Hook for formatting the HTML for embedded asset attributes in the Assets manager.
  *
  * @param $element
  * @param $attribute
  * @return null|string
  */
 public function getAssetTableAttributeHtml($element, $attribute)
 {
     if ($element instanceof AssetFileModel) {
         $embed = craft()->embeddedAssets->getEmbeddedAsset($element);
         if ($embed) {
             switch ($attribute) {
                 case 'filename':
                     return HtmlHelper::encodeParams('<a href="{url}" target="_blank" style="word-break: break-word;">{name}</a>', array('url' => $embed->url, 'name' => mb_strimwidth($embed->url, 0, 50, '...')));
                 case 'provider':
                     return HtmlHelper::encodeParams('<a href="{url}" target="_blank" data-provider="{data}">{name}</a>', array('url' => $embed->providerUrl, 'data' => StringHelper::toCamelCase($embed->providerName), 'name' => $embed->providerName));
                 case 'size':
                     return '';
                 case 'kind':
                     switch ($embed->type) {
                         case 'photo':
                             return Craft::t("Embedded Image");
                         case 'video':
                             return Craft::t("Embedded Video");
                         case 'link':
                             return Craft::t("Embedded Link");
                     }
                     return Craft::t("Embedded Media");
                 case 'imageSize':
                     if ($embed->type == 'image') {
                         $width = $embed->width;
                         $height = $embed->height;
                         if ($width && $height) {
                             return $width . ' &times; ' . $height;
                         } else {
                             return null;
                         }
                     } else {
                         return '';
                     }
                 case 'width':
                 case 'height':
                     if ($embed->type == 'image') {
                         $size = $embed->{$attribute};
                         return $size ? $size . 'px' : null;
                     } else {
                         return '';
                     }
             }
         }
     }
     if ($attribute === 'provider') {
         return '';
     }
     return null;
 }
 /**
  * camelCases a string.
  *
  * @param string $string The string
  *
  * @return string
  */
 public function camelFilter($string)
 {
     return StringHelper::toCamelCase($string);
 }
 /**
  * Get unique name and handle for a form.
  *
  * @param AmForms_FormModel $form
  */
 public function getUniqueNameAndHandle(AmForms_FormModel $form)
 {
     $slugWordSeparator = craft()->config->get('slugWordSeparator');
     $maxSlugIncrement = craft()->config->get('maxSlugIncrement');
     for ($i = 0; $i < $maxSlugIncrement; $i++) {
         $testName = $form->name;
         if ($i > 0) {
             $testName .= $slugWordSeparator . $i;
         }
         $originalName = $form->name;
         $originalHandle = $form->handle;
         $form->name = $testName;
         $form->handle = StringHelper::toCamelCase($form->name);
         $totalForms = craft()->db->createCommand()->select('count(id)')->from('amforms_forms')->where('name=:name AND handle=:handle', array(':name' => $form->name, ':handle' => $form->handle))->queryScalar();
         if ($totalForms == 0) {
             return;
         } else {
             $form->name = $originalName;
             $form->handle = $originalHandle;
         }
     }
     throw new Exception(Craft::t('Could not find a unique name and handle for this form.'));
 }