ClubAR::tableName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This is the model class for table "club".
5
 *
6
 * The followings are the available columns in table 'club':
7
 * @property string $id
8
 * @property integer $owner
9
 * @property string $name
10
 * @property string $created
11
 */
12
class ClubAR extends CActiveRecord
13
{
14
    /**
15
     * Returns the static model of the specified AR class.
16
     * @param string $className active record class name.
17
     * @return Club the static model class
18
     */
19
    public static function model($className = __CLASS__)
20
    {
21
        return parent::model($className);
22
    }
23
24
    /**
25
     * @return string the associated database table name
26
     */
27
    public function tableName()
28
    {
29
        return 'club';
30
    }
31
32
    /**
33
     * @return array validation rules for model attributes.
34
     */
35
    public function rules()
36
    {
37
        // NOTE: you should only define rules for those attributes that
38
        // will receive user inputs.
39
        return array(
40
            ['name', 'clean'],
41
            ['name', 'required'],
42
            ['name', 'length', 'min'=>6, 'max'=>14],
43
            ['name', 'levelRequirement'],
44
            ['name', 'hasOtherClub'],
45
            ['name', 'nameIsFree'],
46
            ['id, owner', 'safe', 'on'=>'search'],
47
        );
48
    }
49
50
    public function clean($attribute)
51
    {
52
        $this->$attribute = trim($this->$attribute);
53
        $this->$attribute = strip_tags($this->$attribute);
54
        $this->$attribute = htmlspecialchars($this->$attribute);
55
    }
56
57
    public function levelRequirement($attribute, $params)
58
    {
59 View Code Duplication
        if (Yii::app()->player->model->level < Yii::app()->params['clubCreateLevelRequirement']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $this->addError($attribute, 'Saját klub indításához minimum ' . Yii::app()->params['clubCreateLevelRequirement'] . '. szintre kell fejlődnöd.');
61
        }
62
    }
63
64
    public function hasOtherClub($attribute, $params)
65
    {
66
        if (Yii::app()->player->model->in_club) {
67
            $this->addError($attribute, 'Egy másik klub tagja vagy. Először lépj ki abból.');
68
        }
69
    }
70
71
    public function nameIsFree($attribute, $params)
72
    {
73
        $clubWithSameName = Yii::app()->db->createCommand()
74
            ->select('id')
75
            ->from('club')
76
            ->where('name=:name', [':name'=>$this->$attribute])
77
            ->queryScalar();
78
        if ($clubWithSameName) {
79
            $this->addError($attribute, 'Ez a név már foglalt.');
80
        }
81
    }
82
83
    /**
84
     * @return array customized attribute labels (name=>label)
85
     */
86
    public function attributeLabels()
87
    {
88
        return array(
89
            'id' => 'ID',
90
            'owner' => 'Tulajdonos',
91
            'name' => 'Klub neve',
92
            'created' => 'Létrehozva',
93
        );
94
    }
95
96
    /**
97
     * Retrieves a list of models based on the current search/filter conditions.
98
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
99
     */
100
    public function search()
101
    {
102
        // Warning: Please modify the following code to remove attributes that
103
        // should not be searched.
104
105
        $criteria=new CDbCriteria;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
106
107
        $criteria->compare('id', $this->id, true);
108
        $criteria->compare('owner', $this->owner);
109
        $criteria->compare('name', $this->name, true);
110
        $criteria->compare('created', $this->created, true);
111
112
        return new CActiveDataProvider($this, array(
113
            'criteria'=>$criteria,
114
        ));
115
    }
116
117
    protected function beforeSave()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
118
    {
119
        $this->owner = Yii::app()->player->uid;
120
        return parent::beforeSave();
121
    }
122
123
    protected function afterSave()
124
    {
125
        $forum = new Forum;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
126
        $forum->id = $this->id;
0 ignored issues
show
Bug introduced by
The property id cannot be accessed from this context as it is declared private in class Forum.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
127
        $forum->save(Yii::app()->player->model->user . ' megalapította a klubot.', true);
128
129
        Yii::app()->badge->model->triggerSimple('club_create');
130
131
        //delete inactive join request
132
        Yii::app()->db->createCommand()->delete('club_members', 'uid=:uid AND approved=0', [':uid'=>Yii::app()->player->model->uid]);
133
        parent::afterSave();
134
    }
135
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
136