+++ /dev/null
-<?php
-namespace App\Model\Table;
-
-use App\Model\Entity\Software;
-use Cake\ORM\Query;
-use Cake\ORM\RulesChecker;
-use Cake\ORM\Table;
-use Cake\Validation\Validator;
-use Search\Manager;
-/**
- * Softwares Model
- *
- * @property \Cake\ORM\Association\BelongsTo $Licenses
- * @property \Cake\ORM\Association\HasMany $RelationshipsSoftwaresUsers
- * @property \Cake\ORM\Association\HasMany $Reviews
- * @property \Cake\ORM\Association\HasMany $Screenshots
- * @property \Cake\ORM\Association\HasMany $SoftwaresStatistics
- * @property \Cake\ORM\Association\BelongsToMany $Relationships
- */
-class SoftwaresTable extends Table
-{
-
- /**
- * Initialize method
- *
- * @param array $config The configuration for the Table.
- * @return void
- */
- public function initialize(array $config)
- {
- parent::initialize($config);
-
- $this->table('softwares');
- $this->displayField('softwarename');
- $this->primaryKey('id');
-
- $this->addBehavior('Timestamp');
- //For upload file -> avatar
- $this->addBehavior('Josegonzalez/Upload.Upload', [
- 'photo' => [
- 'fields' => [
- 'dir' => 'logo_directory',
- ],
- 'path' => 'files{DS}{model}{DS}{primaryKey}{DS}{field}{DS}avatar',
- ],
- ]);
-
- $this->belongsTo('Licenses', [
- 'foreignKey' => 'licence_id'
- ]);
- $this->hasOne('RawMetricsSoftwares', [
- 'foreignKey' => 'software_id'
- ]);
- $this->hasMany('RelationshipsSoftwaresUsers', [
- 'foreignKey' => 'software_id'
- ]);
- $this->hasMany('Reviews', [
- 'foreignKey' => 'software_id'
- ]);
- $this->hasMany('Screenshots', [
- 'foreignKey' => 'software_id'
- ]);
- $this->hasMany('SoftwaresStatistics', [
- 'foreignKey' => 'software_id'
- ]);
- $this->belongsToMany('Relationships', [
- 'foreignKey' => 'software_id',
- 'targetForeignKey' => 'relationship_id',
- 'joinTable' => 'relationships_softwares'
- ]);
-
-
-
- // #######################
- // ##### For search ######
- // #######################
-
- // Add the behaviour to your table
- $this->addBehavior('Search.Search');
-
- // Setup search filter using search manager
- $this->searchManager()
- ->value('id')
- // Here we will alias the 'q' query param to search the `Articles.title`
- // field and the `Articles.content` field, using a LIKE match, with `%`
- // both before and after.
- ->add('q', 'Search.Like', [
- 'before' => true,
- 'after' => true,
- 'mode' => 'or',
- 'comparison' => 'ILIKE',
- 'wildcardAny' => '*',
- 'wildcardOne' => '?',
- 'field' => [$this->aliasField('softwarename'), $this->aliasField('description')]
- ]);
-// ->add('foo', 'Search.Callback', [
-// 'callback' => function ($query, $args, $filter) {
-// // Modify $query as required
-// }
-// ]);
- }
-
- /**
- * Default validation rules.
- *
- * @param \Cake\Validation\Validator $validator Validator instance.
- * @return \Cake\Validation\Validator
- */
- public function validationDefault(Validator $validator)
- {
- $validator
- ->allowEmpty('id', 'create');
-
- $validator
- ->allowEmpty('softwarename')
- ->add('softwarename', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
-
- $validator
- ->requirePresence('url_repository', 'create')
- ->notEmpty('url_repository')
- ->add('url_repository', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
-
- $validator
- ->allowEmpty('description');
-
- $validator
- ->allowEmpty('logo_directory');
-
- $validator
- ->allowEmpty('photo');
-
-
- $validator->add('photo','file',[
- 'rule' => ['mimeType', ['image/jpeg', 'image/png','image/gif','image/svg+xml']],
- 'message' => "This format is not allowed"
- ]);
-
-
- $validator->provider('uploadImage',new \Josegonzalez\Upload\Validation\ImageValidation());
-
- //Check that the file is below the maximum height requirement (checked in pixels)
- $validator->add('photo', 'fileBelowMaxHeight', [
- 'rule' => ['isBelowMaxHeight', 250],
- 'message' => 'This image should not be higher than 500px',
- 'provider' => 'uploadImage'
- ]);
-
-
- // Check that the file is Above the minimum height requirement (checked in pixels)
- $validator->add('photo', 'fileAboveMinHeight', [
- 'rule' => ['isAboveMinHeight', 100],
- 'message' => 'This image should at least be 150px high',
- 'provider' => 'uploadImage'
- ]);
-
-
-
- $validator->provider('upload',new \Josegonzalez\Upload\Validation\UploadValidation());
-
- $validator->add('photo', 'fileBelowMaxSize', [
- 'rule' => ['isBelowMaxSize', 1000000],
- 'message' => 'This file is too large',
- 'provider' => 'upload'
- ]);
-
-
-
-
-
- return $validator;
- }
-
- /**
- * Returns a rules checker object that will be used for validating
- * application integrity.
- *
- * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
- * @return \Cake\ORM\RulesChecker
- */
- public function buildRules(RulesChecker $rules)
- {
- $rules->add($rules->isUnique(['softwarename']));
- $rules->add($rules->isUnique(['url_repository']));
- $rules->add($rules->existsIn(['licence_id'], 'Licenses'));
- return $rules;
- }
-}