/** * Return the translatable label. * * @param string $size * @return null|string */ protected function translatableLabel($size = 'sm') { if ($this->object->isTranslatable()) { return '<span class="label label-info label-' . $size . '">' . trans('streams::assignment.translatable.name') . '</span>'; } return null; }
/** * Run after a record has been deleted. * * @param AssignmentInterface $model */ public function deleted(AssignmentInterface $model) { $model->flushCache(); $model->compileStream(); $this->dispatch(new DropAssignmentColumn($model)); $this->dispatch(new DeleteAssignmentTranslations($model)); $this->events->fire(new AssignmentWasDeleted($model)); }
/** * Handle the command. * * @param AssignmentSchema $schema */ public function handle(AssignmentSchema $schema) { $stream = $this->assignment->getStream(); $type = $this->assignment->getFieldType(); if (!$this->assignment->isTranslatable()) { $table = $stream->getEntryTableName(); } else { $table = $stream->getEntryTranslationsTableName(); } $schema->updateColumn($table, $type, $this->assignment); }
/** * Rename a column. * * @param $table * @param FieldType $type * @param AssignmentInterface $assignment */ public function renameColumn($table, FieldType $type, AssignmentInterface $assignment) { $schema = $type->getSchema(); $from = $assignment->getFieldType(true); if ($from->getColumnName() === $type->getColumnName()) { return; } $this->schema->table($table, function (Blueprint $table) use($schema, $from) { $schema->renameColumn($table, $from); }); }
/** * Parse an assignment relation. * * @param AssignmentInterface $assignment * @param $string */ protected function parseAssignment(AssignmentInterface $assignment, &$string) { $fieldSlug = $assignment->getFieldSlug(); $method = camel_case($fieldSlug); $relationString = ''; $relationString .= "\npublic function {$method}()"; $relationString .= "\n{"; $relationString .= "\n\nreturn \$this->getFieldType('{$fieldSlug}')->getRelation();"; $relationString .= "\n}"; $relationString .= "\n"; $string .= $relationString; }
/** * Add the field type column. * * @param Blueprint $table * @param AssignmentInterface $assignment */ public function addColumn(Blueprint $table, AssignmentInterface $assignment) { // Skip if the column already exists. if ($this->schema->hasColumn($table->getTable(), $this->fieldType->getColumnName() . '_id')) { return; } $table->string($this->fieldType->getColumnName() . '_type')->nullable(!$assignment->isRequired()); $table->integer($this->fieldType->getColumnName() . '_id')->nullable(!$assignment->isRequired()); if ($assignment->isUnique() && $assignment->isTranslatable()) { $table->unique([$this->fieldType->getColumnName() . '_type', $this->fieldType->getColumnName() . '_id']); } }
/** * Parse the assignment rules. * * @param StreamInterface $stream * @param AssignmentInterface $assignment * @param $string */ protected function parseAssignmentRules(StreamInterface $stream, AssignmentInterface $assignment, &$string) { $rules = []; if ($assignment->isRequired()) { $rules[] = 'required'; } if ($assignment->isUnique()) { $rules[] = 'unique:' . $stream->getEntryTableName() . ',' . $assignment->getColumnName(); } if (is_array($rules)) { $rules = implode('|', array_filter($rules)); $string .= "\n'{$assignment->getFieldSlug()}' => '{$rules}',"; } }
/** * Handle the command. * * @param AssignmentSchema $schema * @param AssignmentRepositoryInterface $assignments */ public function handle(AssignmentSchema $schema, AssignmentRepositoryInterface $assignments) { $stream = $this->assignment->getStream(); $type = $this->assignment->getFieldType(true); if (!$this->assignment->isTranslatable()) { $table = $stream->getEntryTableName(); } else { $table = $stream->getEntryTranslationsTableName(); } /* @var AssignmentInterface $assignment */ $assignment = $assignments->find($this->assignment->getId()); $assignment = clone $assignment; $schema->renameColumn($table, $type, $assignment); }
/** * Parse an assignment. * * @param AssignmentInterface $assignment * @param $string */ protected function parseAssignment(AssignmentInterface $assignment, &$string) { $string .= "\n["; foreach ($assignment->getAttributes() as $key => $value) { $value = $assignment->getAttribute($key); if (is_string($value)) { $value = addslashes($value); } if (is_array($value)) { $value = serialize($value); } $string .= "\n'{$key}' => '{$value}',"; } // Parse this assignment field. $this->parseField($assignment->getField(), $string); // Parse assignment translations. $this->parseTranslations($assignment, $string); $string .= "\n],"; }
/** * @param Blueprint $table * @param AssignmentInterface $assignment */ public function addColumn(Blueprint $table, AssignmentInterface $assignment) { // Skip if the column already exists. if ($this->schema->hasColumn($table->getTable(), $this->fieldType->getColumnName())) { return; } /** * Add the column to the table. * * @var Blueprint|Fluent $column */ $column = $table->{$this->fieldType->getColumnType()}($this->fieldType->getColumnName(), 11, array_get($this->fieldType->getConfig(), 'decimals', 2))->nullable(!$assignment->isTranslatable() ? !$assignment->isRequired() : true); if (!str_contains($this->fieldType->getColumnType(), ['text', 'blob'])) { $column->default(array_get($this->fieldType->getConfig(), 'default_value')); } // Mark the column unique if desired and not translatable. if ($assignment->isUnique() && !$assignment->isTranslatable()) { $table->unique($this->fieldType->getColumnName()); } }
/** * Handle the command. * * @param AssignmentSchema $schema */ public function handle(AssignmentSchema $schema, AssignmentRepositoryInterface $assignments) { $stream = $this->assignment->getStream(); /* @var AssignmentInterface $assignment */ $assignment = $assignments->find($this->assignment->getId()); // If nothing is different then skip it. if ($assignment->isTranslatable() == $this->assignment->isTranslatable()) { return; } /* * If it's NOW translatable then move it from * the main table to the translations table. */ if ($this->assignment->isTranslatable()) { $schema->dropColumn($stream->getEntryTableName(), $assignment->getFieldType(true)); $schema->addColumn($stream->getEntryTranslationsTableName(), $assignment->getFieldType(true), $assignment); } /* * If it's NOT translatable then move it from * the translations table to the main table. */ if (!$this->assignment->isTranslatable()) { $schema->dropColumn($stream->getEntryTranslationsTableName(), $assignment->getFieldType(true)); $schema->addColumn($stream->getEntryTableName(), $assignment->getFieldType(true), $assignment); } }
/** * Update the field type column to the table. * * @param Blueprint $table * @param AssignmentInterface $assignment */ public function updateColumn(Blueprint $table, AssignmentInterface $assignment) { // Skip if no column type. if (!$this->fieldType->getColumnType()) { return; } // Skip if the column doesn't exists. if (!$this->schema->hasColumn($table->getTable(), $this->fieldType->getColumnName())) { return; } /** * Update the column to the table. * * @var Blueprint|Fluent $column */ $column = call_user_func_array([$table, $this->fieldType->getColumnType()], array_filter([$this->fieldType->getColumnName(), $this->fieldType->getColumnLength()])); $column->nullable(!$assignment->isTranslatable() ? !$assignment->isRequired() : true)->change(); if (!str_contains($this->fieldType->getColumnType(), ['text', 'blob'])) { $column->default(array_get($this->fieldType->getConfig(), 'default_value')); } /** * Mark the column unique if desired and not translatable. * Otherwise, drop the unique index. */ $connection = $this->schema->getConnection(); $manager = $connection->getDoctrineSchemaManager(); $doctrine = $manager->listTableDetails($connection->getTablePrefix() . $table->getTable()); // The unique index name. $unique = md5('unique_' . $this->fieldType->getColumnName()); /** * If the assignment is unique and not translatable * and the table does not already have the given the * given table index then go ahead and add it. */ if ($assignment->isUnique() && !$assignment->isTranslatable() && !$doctrine->hasIndex($unique)) { $table->unique($this->fieldType->getColumnName(), $unique); } /** * If the assignment is NOT unique and not translatable * and the table DOES have the given table index * then we need to remove. */ if (!$assignment->isUnique() && !$assignment->isTranslatable() && $doctrine->hasIndex($unique)) { $column->dropIndex(md5('unique_' . $this->fieldType->getColumnName())); } }
/** * Parse an assignment relation. * * @param AssignmentInterface $assignment * @param $string */ protected function parseAssignment(AssignmentInterface $assignment, &$string) { $fieldType = $assignment->getFieldType(); $parser = $fieldType->getParser(); $string .= $parser->relation($assignment); }
/** * Handle the command. */ public function handle() { foreach ($this->assignment->getTranslations() as $translation) { $translation->delete(); } }