Determines whether the model data makes it a sluggable candidate
and returns analysis result

@param array $model  model data
@return array|false      false if not a candidate
765348
protected function analyzeModelForSluggable(array $model)
    {
        $analysis = [
            'translated'              => null,
            'slug_column'             => null,
            'slug_sources_normal'     => [],
            'slug_sources_translated' => [],
        ];

        // find out whether the model has a 'slug' column, preferring the first match we find
        $slugColumns = config('pxlcms.generator.models.slugs.slug_columns', []);

        foreach ($slugColumns as $slugColumn) {

            foreach ($model['normal_attributes'] as $attribute) {
                if ($attribute == $slugColumn) {
                    $analysis['slug_column'] = $attribute;
                    $analysis['translated']  = false;
                    break 2;
                }
            }

            foreach ($model['translated_attributes'] as $attribute) {
                if ($attribute == $slugColumn) {
                    $analysis['slug_column'] = $attribute;
                    $analysis['translated']  = true;
                    break 2;
                }
            }
        }

        // discard any matches that don't have a slug column on the model
        // if there is no slug structure present (because that could never work)
        if ( ! $this->context->slugStructurePresent && empty($analysis['slug_column'])) return false;

        // find out whether the model has slug source candidate columns
        $slugSources = config('pxlcms.generator.models.slugs.slug_source_columns', []);

        // make sure we keep slug and source column in the same model (translated or parent)
        $analysis['slug_sources_translated'] = array_values(array_intersect($slugSources, $model['translated_attributes']));
        $analysis['slug_sources_normal']     = array_values(array_intersect($slugSources, $model['normal_attributes']));

        if ($analysis['translated'] === true) {
            $analysis['slug_sources_normal'] = [];
        } elseif ($analysis === false) {
            $analysis['slug_sources_translated'] = [];
        }


        // any matches? return as candidate
        if (    ! empty($analysis['slug_column'])
            ||  count($analysis['slug_sources_normal'])
            ||  count($analysis['slug_sources_translated'])
        ) {
            return $analysis;
        }

        return false;
    }
44591
public function backup(array $data, $model)
    {
        $directory = gplcart_file_private_module('backup');

        if (!file_exists($directory) && !mkdir($directory, 0775, true)) {
            return false;
        }

        $data['type'] = 'module';
        $data['name'] = $this->translation->text('Module @name', array('@name' => $data['name']));

        $time = date('d-m-Y--G-i');
        $destination = gplcart_file_unique("$directory/module-{$data['id']}-$time.zip");
        $data['path'] = gplcart_file_relative($destination);

        $success = $this->zip->directory($data['directory'], $destination, $data['id']);

        if ($success) {
            return (bool) $model->add($data);
        }

        return false;
    }
359676
public function getModelArray(Model $model)
    {
        $result = [];
        foreach ($model->getAttributes() as $key => $value) {
            $result[] = ['type' => 'text', 'name' => $key, 'key' => $key.' :', 'value' => $value];
        }
        unset($result[0]);

        return $result;
    }
309288
public function getDefinition($model)
    {
        if(!($model instanceof Model)) return false;

        $fields = array();
        $keys = array();
        $unique = array();
        $definition = $model->getDefinition();
        if (isset($definition['fields'])) {
            $definitionFields = $definition['fields'];
            if (is_array($definition['index'])) {
                $definitionFields = array($definition['index']['field'] => $definition['index']) + $definitionFields;
                $definitionFields[$definition['index']['field']]['primary'] = true;
            } else {
                $definitionFields = array(
                        $definition['index'] => array(
                            'type' => 'int(11)',
                            'autoincrement' => true
                        )
                    ) + $definitionFields;
            }
            foreach ($definitionFields as $field => $props) {
                if (is_array($props)) {
                    $fields[$field] = array(
                        'field' => $field,
                    );

                    if (isset($props['type'])) {
                        $fields[$field]['type'] = $props['type'];
                    }
                    if (isset($props['default'])) {
                        $fields[$field]['default'] = $props['default'];
                    } else {
                        $fields[$field]['default'] = '';
                    }
                    if (isset($props['autoincrement'])) {
                        $fields[$field]['autoincrement'] = $props['autoincrement'];
                    }
                    if (isset($props['primary'])) {
                        $fields[$field]['primary'] = $props['primary'];
                    }
                } else {
                    $fields[$field] = array(
                        'field' => $field,
                        'type' => $props,
                        'default' => '',
                        //                'null' => 1
                    );
                }
            }
        }
        if (isset($definition['keys'])) {
            foreach ($definition['keys'] as $field => $value) {
                if (isset($fields[$field])) {
                   $model1 = key($value); // Para extrar datos del tipo key => value
                   $column = current($value);
                   $modelObj = Model::newInstance($model1);
                   if ($modelObj and $modelObj instanceof Model) {
                       $keys[] = array(
                           'field' => $field,
                           'table' => $modelObj->getTableName(),
                           'target' => $column

                       );
//                       $fields[$field]['index'] = true;
                   }

                }
            }
        }

        if (isset($definition['unique'])) {
            foreach ($definition['unique'] as $field) {
                $unique[] = array('field' => $field);
            }
        }

        $result = array();
        $result['table'] = $model->getTableName();
        $result['fields'] = $fields;
        $result['keys'] = $keys;
        $result['unique'] = $unique;
//        $result['engine'] = $model->getEngine();

        return $result;
    }
1232149
protected function checkModelType($model)
    {
        if (is_string($model)) {
            return $this->getTable($model);
        } elseif (is_object($model)) {
            return $this->getTable(get_class($model));
        }
    }
293808
private function isTranslationActive($model)
    {
        if (!$model) {
            return false;
        }
        if (is_array($this->requiredAttributes) && !empty($this->requiredAttributes)) {
            foreach ($this->requiredAttributes as $attribute) {
                if ($model->{$attribute} && !empty($model->{$attribute})) {
                    return true;
                }
            }
        } else {
            return true;
        }
        return false;
    }
1049495
public function checkModel(ActiveRecord $model)
    {
        /** @var TrackableBehavior $behavior */
        if (($behavior = $this->getBehavior($model)) === null) {
            return null;
        }

        $auditTableName = $behavior->auditTableName;

        if (($pos=strpos($auditTableName, '.')) !== false) {
            $auditSchema = substr($auditTableName, 0, $pos);
        } else {
            $auditSchema = null;
        }
        $triggerTemplate = $this->triggerTemplate($model->getTableSchema()->name, $auditSchema);

        return [
            'enabled' => true,
            'valid' => $triggerTemplate['exists'],
        ];
    }
1161842
public function validate($model)
    {
        $validator = Validation::createValidator();
        $errors = $validator->validate($model);
        if (($errors->count() > 0)) {
            $this->errors = array_merge($this->errors, $errors);

            return false;
        }

        return true;
    }
884892
protected function validateModelFoundFromData($model)
    {
        $return = parent::validateModelFoundFromData($model);
        if ($return !== true) {
            return $return;
        }
        $data = $this->getData();

        return $model->checkSaltedPassword($data['password']);
    }
460301
protected function isApplicableRoute(array $route)
    {
        return (isset($route['contenttype']) && $route['contenttype'] === $this->contenttype['singular_slug'])
            || (isset($route['contenttype']) && $route['contenttype'] === $this->contenttype['slug'])
            || (isset($route['recordslug']) && $route['recordslug'] === $this->getReference());
    }
230975
protected function _getValidationRules(Model $model) {
		$cachePath = 'validation_rules_' . $model->alias;
		$cached = Cache::read($cachePath, CAKE_LDAP_CACHE_KEY_CONFIG);
		if ($cached !== false) {
			return $cached;
		}

		$ldapFields = $this->_modelConfigSync->getLdapFieldsInfo();
		if (empty($ldapFields)) {
			return false;
		}

		$ldapField = $this->_getLdapField($model);
		if (empty($ldapField)) {
			return false;
		}

		$result = Hash::get($ldapFields, $ldapField . '.rules');
		if (empty($result)) {
			$result = true;
		}
		Cache::write($cachePath, $result, CAKE_LDAP_CACHE_KEY_CONFIG);

		return $result;
	}
784835
public function isEncodable($model)
    {
        if (!method_exists($model, 'getDirtyEncodableAttributes')) {
            return false;
        }

        if (is_a($model, 'Bkwld\Decoy\Models\Element') && $model->getAttribute('type') != 'video-encoder') {
            return false;
        }

        return true;
    }
622325
private function mustBeFiltered(Model $model): bool
    {
        return
            ($this->withHash !== null && $model->hash !== $this->withHash) ||
            ($this->thatNotMigrated && $model->migrated()) ||
            ($this->onlyMigrated && ! $model->migrated())
        ;
    }
609405
public function check($model)
    {
        $moderated = false;

        foreach ($model->getModerateList() as $id => $moderation) {
            $rules = explode('|', $moderation);

            foreach ($rules as $rule) {
                $action = explode(':', $rule);
                $options = isset($action[1]) ? $action[1] : null;

                $method = $action[0];
                if ($this->$method($model->$id, $options)) {
                    return $moderated = true;
                }
            }

            // No reason to keep going
            if ($moderated === true) {
                return $moderated;
            }
        }

        return $moderated;
    }
181483
public function slug(Model $model, bool $force = false): bool
    {
        $this->setModel($model);

        $attributes = [];

        foreach ($this->model->sluggable() as $attribute => $config) {
            if (is_numeric($attribute)) {
                $attribute = $config;
                $config = $this->getConfiguration();
            } else {
                $config = $this->getConfiguration($config);
            }

            $slug = $this->buildSlug($attribute, $config, $force);

            if ($slug !== null) {
                $this->model->setAttribute($attribute, $slug);
                $attributes[] = $attribute;
            }
        }

        return $this->model->isDirty($attributes);
    }
982479
public function dynamic($model, &$attributes = null)
    {
        // dynamic migration is not allowed when attributes are locked explicitly
        $table_schema = Cache::get($model->getTable());

        if (isset($table_schema['lock_attributes']) && $table_schema['lock_attributes']) {
            return true;
        }

        // Cached attributes
        $cached_attributes = array_map(function($item) { return $item['name']; }, isset($table_schema['attributes']) ? $table_schema['attributes'] : array());

        $config = array(
            'lock_attributes' => false,
            'attributes' => array()
        );

        foreach ($attributes as $field => $value) {
            // extract datatype from field value to migrate
            $datatype = strtolower(gettype($value));

            // ignore 'null' data-types
            if ($datatype == 'null') {
                unset($attributes[$field]);
                continue;
            }

            // Detect large text blocks to declare 'text' datatype.
            if ($datatype == 'string' && strlen($value) > 255) {
                $datatype = 'text';
            }

            if ($datatype !== 'array' && !in_array($field, $cached_attributes)) {
                $config['attributes'][] = array('name' => $field, 'type' => $datatype);
            }
        }

        if (count($config['attributes']) > 0) {
            return $this->migrate($model, $config, true);
        } else {
            return false;
        }
    }
454523
public function checkTableExists($model)
    {
        if (!in_array($model->table, $this->getAuditTables())) {
            $model->addError('table', Yii::t('app', "You've entered an invalid table name."));
            return false;
        }
        return true;
    }