/** * @param GenerateSchemaEventArgs $args */ public function postGenerateSchema(GenerateSchemaEventArgs $args) { $schema = $args->getSchema(); try { if (!$schema->hasTable(MAUTIC_TABLE_PREFIX . 'lead_fields')) { return; } $objects = ['lead' => 'leads', 'company' => 'companies']; foreach ($objects as $object => $tableName) { $table = $schema->getTable(MAUTIC_TABLE_PREFIX . $tableName); //get a list of fields $fields = $args->getEntityManager()->getConnection()->createQueryBuilder()->select('f.alias, f.is_unique_identifer as is_unique, f.type, f.object')->from(MAUTIC_TABLE_PREFIX . 'lead_fields', 'f')->where("f.object = '{$object}'")->orderBy('f.field_order', 'ASC')->execute()->fetchAll(); // Compile which ones are unique identifiers // Email will always be included first $uniqueFields = 'lead' === $object ? ['email' => 'email'] : ['companyemail' => 'companyemail']; foreach ($fields as $f) { if ($f['is_unique'] && $f['alias'] != 'email') { $uniqueFields[$f['alias']] = $f['alias']; } $columnDef = FieldModel::getSchemaDefinition($f['alias'], $f['type'], !empty($f['is_unique'])); $table->addColumn($columnDef['name'], $columnDef['type'], $columnDef['options']); if ('text' != $columnDef['type']) { $table->addIndex([$columnDef['name']], MAUTIC_TABLE_PREFIX . $f['alias'] . '_search'); } } // Only allow indexes for string types $columns = $table->getColumns(); /** @var \Doctrine\DBAL\Schema\Column $column */ foreach ($columns as $column) { $type = $column->getType(); $name = $column->getName(); if (!$type instanceof StringType) { unset($uniqueFields[$name]); } elseif (isset($uniqueFields[$name])) { $uniqueFields[$name] = $uniqueFields[$name]; } } if (count($uniqueFields) > 1) { // Only use three to prevent max key length errors $uniqueFields = array_slice($uniqueFields, 0, 3); $table->addIndex($uniqueFields, MAUTIC_TABLE_PREFIX . 'unique_identifier_search'); } switch ($object) { case 'lead': $table->addIndex(['attribution', 'attribution_date'], MAUTIC_TABLE_PREFIX . 'contact_attribution'); break; case 'company': $table->addIndex(['companyname', 'companyemail'], MAUTIC_TABLE_PREFIX . 'company_filter'); $table->addIndex(['companyname', 'companycity', 'companycountry', 'companystate'], MAUTIC_TABLE_PREFIX . 'company_match'); break; } } } catch (\Exception $e) { //table doesn't exist or something bad happened so oh well $this->logger->addError('SCHEMA ERROR: ' . $e->getMessage()); } }
/** * @param ObjectManager $manager */ public function load(ObjectManager $manager) { $fields = FieldModel::$coreFields; $translator = $this->container->get('translator'); $indexesToAdd = []; /** @var ColumnSchemaHelper $leadsSchema */ $leadsSchema = $this->container->get('mautic.schema.helper.factory')->getSchemaHelper('column', 'leads'); $order = 1; foreach ($fields as $alias => $field) { $type = isset($field['type']) ? $field['type'] : 'text'; $entity = new LeadField(); $entity->setLabel($translator->trans('mautic.lead.field.' . $alias, [], 'fixtures')); $entity->setGroup(isset($field['group']) ? $field['group'] : 'core'); $entity->setOrder($order); $entity->setAlias($alias); $entity->setType($type); $entity->setIsUniqueIdentifer(!empty($field['unique'])); $entity->setProperties(isset($field['properties']) ? $field['properties'] : []); $entity->setIsFixed(!empty($field['fixed'])); $entity->setIsListable(!empty($field['listable'])); $entity->setIsShortVisible(!empty($field['short'])); $manager->persist($entity); $manager->flush(); //add the column to the leads table $leadsSchema->addColumn(FieldModel::getSchemaDefinition($alias, $type, $entity->getIsUniqueIdentifier())); $indexesToAdd[] = $alias; $this->addReference('leadfield-' . $alias, $entity); $order++; } $leadsSchema->executeChanges(); /** @var IndexSchemaHelper $indexHelper */ $indexHelper = $this->container->get('mautic.schema.helper.factory')->getSchemaHelper('index', 'leads'); foreach ($indexesToAdd as $name) { $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : 'text'; if ('textarea' != $type) { $indexHelper->addIndex([$name], MAUTIC_TABLE_PREFIX . $name . '_search'); } } // Add an attribution index $indexHelper->addIndex(['attribution', 'attribution_date'], MAUTIC_TABLE_PREFIX . '_contact_attribution'); $indexHelper->executeChanges(); }
/** * @param ObjectManager $manager */ public function load(ObjectManager $manager) { $fieldGroups['lead'] = FieldModel::$coreFields; $fieldGroups['company'] = FieldModel::$coreCompanyFields; $translator = $this->container->get('translator'); $indexesToAdd = []; foreach ($fieldGroups as $object => $fields) { if ($object == 'company') { /** @var ColumnSchemaHelper $companiesSchema */ $schema = $this->container->get('mautic.schema.helper.factory')->getSchemaHelper('column', 'companies'); } else { /** @var ColumnSchemaHelper $companiesSchema */ $schema = $this->container->get('mautic.schema.helper.factory')->getSchemaHelper('column', 'leads'); } $order = 1; foreach ($fields as $alias => $field) { $type = isset($field['type']) ? $field['type'] : 'text'; $entity = new LeadField(); $entity->setLabel($translator->trans('mautic.lead.field.' . $alias, [], 'fixtures')); $entity->setGroup(isset($field['group']) ? $field['group'] : 'core'); $entity->setOrder($order); $entity->setAlias($alias); $entity->setIsRequired(isset($field['required']) ? $field['required'] : false); $entity->setType($type); $entity->setObject($field['object']); $entity->setIsUniqueIdentifer(!empty($field['unique'])); $entity->setProperties(isset($field['properties']) ? $field['properties'] : []); $entity->setIsFixed(!empty($field['fixed'])); $entity->setIsListable(!empty($field['listable'])); $entity->setIsShortVisible(!empty($field['short'])); $manager->persist($entity); $manager->flush(); $schema->addColumn(FieldModel::getSchemaDefinition($alias, $type, $entity->getIsUniqueIdentifier())); $indexesToAdd[$object][] = $alias; $this->addReference('leadfield-' . $alias, $entity); ++$order; } $schema->executeChanges(); } foreach ($indexesToAdd as $object => $indexes) { if ($object == 'company') { /** @var IndexSchemaHelper $indexHelper */ $indexHelper = $this->container->get('mautic.schema.helper.factory')->getSchemaHelper('index', 'companies'); } else { /** @var IndexSchemaHelper $indexHelper */ $indexHelper = $this->container->get('mautic.schema.helper.factory')->getSchemaHelper('index', 'leads'); } foreach ($indexes as $name) { $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : 'text'; if ('textarea' != $type) { $indexHelper->addIndex([$name], $name . '_search'); } } if ($object == 'lead') { // Add an attribution index $indexHelper->addIndex(['attribution', 'attribution_date'], 'contact_attribution'); } else { $indexHelper->addIndex(['companyname', 'companyemail'], 'company_filter'); $indexHelper->addIndex(['companyname', 'companycity', 'companycountry', 'companystate'], 'company_match'); } $indexHelper->executeChanges(); } }