Skip to content

Commit

Permalink
create year range select box
Browse files Browse the repository at this point in the history
  • Loading branch information
KimHak-mb committed Sep 14, 2023
1 parent 90f2606 commit bb3f771
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 25 deletions.
14 changes: 5 additions & 9 deletions src/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ public function logout()
*/
public function index()
{
$key = $this->request->getQuery('key');
if($key){
$query = $this->Users->find('all')->where(['email like'=>'%'.$key.'%']);
}else{
$query = $this->Users;
}
$users = $this->paginate($query,['contain'=>['Profiles']]);
$users = $this->paginate($this->Users);

$this->set(compact('users'));
}
Expand All @@ -81,7 +75,7 @@ public function index()
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Articles'],
'contain' => ['Profiles', 'Articles'],
]);

$this->set(compact('user'));
Expand Down Expand Up @@ -151,7 +145,9 @@ public function register()
*/
public function edit($id = null)
{
$user = $this->Users->get($id);
$user = $this->Users->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->getData());

Expand Down
4 changes: 4 additions & 0 deletions src/Model/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* @property \Cake\I18n\FrozenTime|null $created
* @property \Cake\I18n\FrozenTime|null $modified
* @property string $image
* @property int $status
* @property string $birthyear
*
* @property \App\Model\Entity\Profile $profile
* @property \App\Model\Entity\Article[] $articles
*/
class User extends Entity
Expand All @@ -35,6 +38,7 @@ class User extends Entity
'modified' => true,
'image' => true,
'status' => true,
'birthyear' => true,
'profile' => true,
'articles' => true,
];
Expand Down
29 changes: 17 additions & 12 deletions src/Model/Table/UsersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Users Model
*
* @property \App\Model\Table\ArticlesTable&\Cake\ORM\Association\HasMany $Articles
* @property \App\Model\Table\ProfilesTable&\Cake\ORM\Association\HasMany $Profiles
*
* @method \App\Model\Entity\User newEmptyEntity()
* @method \App\Model\Entity\User newEntity(array $data, array $options = [])
Expand Down Expand Up @@ -52,6 +53,12 @@ public function initialize(array $config): void
$this->hasMany('Articles', [
'foreignKey' => 'user_id',
]);
$this->hasMany('Profiles', [
'foreignKey' => 'user_id',
]);
$this->hasMany('UserLogs', [
'foreignKey' => 'user_id',
]);
}

/**
Expand All @@ -74,20 +81,18 @@ public function validationDefault(Validator $validator): Validator
->notEmptyString('password');

$validator
->sameAs('retype_password','password','Password match failed!');
->scalar('image')
->maxLength('image', 255)
->requirePresence('image', 'create')
->notEmptyFile('image');

$validator
->notEmptyString('status');

$validator
->allowEmptyFile('image')
->add( 'image', [
'mimeType' => [
'rule' => [ 'mimeType', [ 'image/jpg', 'image/png', 'image/jpeg' ] ],
'message' => 'Please upload only jpg and png.',
],
'fileSize' => [
'rule' => [ 'fileSize', '<=', '1MB' ],
'message' => 'Image file size must be less than 1MB.',
],
] );
->scalar('birthyear')
->requirePresence('birthyear', 'create')
->notEmptyString('birthyear');

return $validator;
}
Expand Down
7 changes: 6 additions & 1 deletion templates/Users/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
echo $this->Form->control('retype_password', ['type'=>'password']);
echo $this->Form->control('profile.mobile');
echo $this->Form->control('image_file',['type'=>'file']);
echo $this->Form->year('yr',[
'min' => date('Y')-5,
'max' => date('Y'),
'empty' => false,
]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->button(__('Submit'),['class'=>'mt-4']) ?>
<?= $this->Form->end() ?>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions templates/Users/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<?php
echo $this->Form->control('email');
echo $this->Form->control('change_image',['type'=>'file']);
echo $this->Form->control('status');
echo $this->Form->control('birthyear');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
Expand Down
4 changes: 3 additions & 1 deletion templates/Users/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<th><?= $this->Paginator->sort('modified') ?></th>
<th><?= $this->Paginator->sort('image') ?></th>
<th><?= $this->Paginator->sort('Change Status') ?></th>
<th><?= $this->Paginator->sort('birthyear') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
Expand All @@ -48,10 +49,11 @@
<?= $this->Form->postLink(__('Active'), ['action' => 'userStatus', $user->id,$user->status], ['block'=>true, 'confirm' => __('Are you sure you want to inactive # {0}?', $user->id)]) ?>
<?php endif; ?>
</td>
<td><?= h($user->birthyear) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['block'=>true, 'confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
Expand Down
12 changes: 12 additions & 0 deletions templates/Users/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@
<th><?= __('Image') ?></th>
<td><?= h($user->image) ?></td>
</tr>
<tr>
<th><?= __('Birthyear') ?></th>
<td><?= h($user->birthyear) ?></td>
</tr>
<tr>
<th><?= __('Profile') ?></th>
<td><?= $user->has('profile') ? $this->Html->link($user->profile->id, ['controller' => 'Profiles', 'action' => 'view', $user->profile->id]) : '' ?></td>
</tr>
<tr>
<th><?= __('Id') ?></th>
<td><?= $this->Number->format($user->id) ?></td>
</tr>
<tr>
<th><?= __('Status') ?></th>
<td><?= $this->Number->format($user->status) ?></td>
</tr>
<tr>
<th><?= __('Created') ?></th>
<td><?= h($user->created) ?></td>
Expand Down
6 changes: 4 additions & 2 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public function init(): void
'id' => 1,
'email' => 'Lorem ipsum dolor sit amet',
'password' => 'Lorem ipsum dolor sit amet',
'created' => '2023-09-05 04:36:57',
'modified' => '2023-09-05 04:36:57',
'created' => '2023-09-14 08:41:14',
'modified' => '2023-09-14 08:41:14',
'image' => 'Lorem ipsum dolor sit amet',
'status' => 1,
'birthyear' => 'Lorem ipsum dolor sit amet',
],
];
parent::init();
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class UsersControllerTest extends TestCase
*/
protected $fixtures = [
'app.Users',
'app.Profiles',
'app.Articles',
];

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Model/Table/UsersTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class UsersTableTest extends TestCase
*/
protected $fixtures = [
'app.Users',
'app.Profiles',
'app.Articles',
];

Expand Down

0 comments on commit bb3f771

Please sign in to comment.