Completed
Branch master (ae19d9)
by Pierre-Henry
36:56
created

AffiliateModel::join()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author         Pierre-Henry Soria <[email protected]>
4
 * @copyright      (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
5
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
6
 * @package        PH7 / App / System / Module / Affiliate / Model
7
 */
8
9
namespace PH7;
10
11
use PH7\Framework\Security\Security;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PH7\Security.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use PH7\Framework\Mvc\Model\Engine\Db;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PH7\Db.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
14
class AffiliateModel extends AffiliateCoreModel
15
{
16
    /**
17
     * Add a new affiliate.
18
     *
19
     * @param array $aData
20
     *
21
     * @return boolean Returns TRUE on success or FALSE on failure.
22
     */
23
    public function join(array $aData)
24
    {
25
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix('Affiliates') .
26
            '(email, username, password, firstName, lastName, sex, birthDate, active, ip, hashValidation, joinDate, lastActivity, affiliatedId)
27
            VALUES (:email, :username, :password, :firstName, :lastName, :sex, :birthDate, :active, :ip, :hashValidation, :joinDate, :lastActivity, :affiliatedId)');
28
29
        $rStmt->bindValue(':email', $aData['email'], \PDO::PARAM_STR);
30
        $rStmt->bindValue(':username', $aData['username'], \PDO::PARAM_STR);
31
        $rStmt->bindValue(':password', Security::hashPwd($aData['password']), \PDO::PARAM_STR);
32
        $rStmt->bindValue(':firstName', $aData['first_name'], \PDO::PARAM_STR);
33
        $rStmt->bindValue(':lastName', $aData['last_name'], \PDO::PARAM_STR);
34
        $rStmt->bindValue(':sex', $aData['sex'], \PDO::PARAM_STR);
35
        $rStmt->bindValue(':birthDate', $aData['birth_date'], \PDO::PARAM_STR);
36
        $rStmt->bindValue(':active', $aData['is_active'], \PDO::PARAM_INT);
37
        $rStmt->bindValue(':ip', $aData['ip'], \PDO::PARAM_STR);
38
        $rStmt->bindParam(':hashValidation', $aData['hash_validation'], \PDO::PARAM_STR, 40);
39
        $rStmt->bindValue(':joinDate', $aData['current_date'], \PDO::PARAM_STR);
40
        $rStmt->bindValue(':lastActivity', $aData['current_date'], \PDO::PARAM_STR);
41
        $rStmt->bindValue(':affiliatedId', $aData['affiliated_id'], \PDO::PARAM_INT);
42
        $rStmt->execute();
43
        $this->setKeyId( Db::getInstance()->lastInsertId() ); // Set the affiliate's ID
44
        Db::free($rStmt);
45
46
        return $this->join2($aData);
47
    }
48
49
    /**
50
     * Join part 2.
51
     *
52
     * @param array $aData
53
     *
54
     * @return boolean Returns TRUE on success or FALSE on failure.
55
     */
56
    public function join2(array $aData)
57
    {
58
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix('AffiliatesInfo') .
59
            '(profileId, country, city, state, zipCode) VALUES (:profileId, :country, :city, :state, :zipCode)');
60
61
        $rStmt->bindValue(':profileId', $this->getKeyId(), \PDO::PARAM_INT);
62
        $rStmt->bindParam(':country', $aData['country'], \PDO::PARAM_STR, 2);
63
        $rStmt->bindValue(':city', $aData['city'], \PDO::PARAM_STR);
64
        $rStmt->bindValue(':state', $aData['state'], \PDO::PARAM_STR);
65
        $rStmt->bindValue(':zipCode', $aData['zip_code'], \PDO::PARAM_STR);
66
67
        return $rStmt->execute();
68
    }
69
70
    /**
71
     * Add a reference affiliate.
72
     *
73
     * @param integer $iProfileId
74
     *
75
     * @return boolean Returns TRUE on success or FALSE on failure.
76
     */
77
    public function addRefer($iProfileId)
78
    {
79
        $rStmt = Db::getInstance()->prepare('UPDATE' . Db::prefix('Affiliates') . 'SET refer = refer+1 WHERE profileId = :profileId');
80
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
81
        Db::free($rStmt);
82
83
        return $rStmt->execute();
84
    }
85
86
    /**
87
     * Search an affiliate.
88
     *
89
     * @param integer|string $mLooking (integer for profile ID or string for a keyword)
90
     * @param boolean $bCount Put 'true' for count the affiliates or 'false' for the result of affiliates.
91
     * @param string $sOrderBy
92
     * @param string $sSort
93
     * @param integer $iOffset
94
     * @param integer $iLimit
95
     *
96
     * @return object|integer Object for the affiliate list or Integer for the total number users returned.
97
     */
98
    public function searchAff($mLooking, $bCount, $sOrderBy, $sSort, $iOffset, $iLimit)
99
    {
100
        $bCount = (bool) $bCount;
101
        $iOffset = (int) $iOffset;
102
        $iLimit = (int) $iLimit;
103
        $mLooking = trim($mLooking);
104
105
        $sSqlLimit = (!$bCount) ? ' LIMIT :offset, :limit' : '';
106
        $sSqlSelect = (!$bCount) ? '*' : 'COUNT(a.profileId) AS totalUsers';
107
108
        if (ctype_digit($mLooking)) {
109
            $sSqlWhere = ' WHERE a.profileId = :looking';
110
        } else {
111
            $sSqlWhere = ' WHERE username LIKE :looking OR firstName LIKE :looking OR lastName LIKE :looking OR email LIKE :looking OR bankAccount LIKE :looking OR sex LIKE :looking OR ip LIKE :looking';
112
        }
113
114
        $sSqlOrder = SearchCoreModel::order($sOrderBy, $sSort);
115
116
        $rStmt = Db::getInstance()->prepare('SELECT ' . $sSqlSelect . ' FROM' . Db::prefix('Affiliates') . 'AS a LEFT JOIN' . Db::prefix('AffiliatesInfo') . 'AS i ON a.profileId = i.profileId' . $sSqlWhere . $sSqlOrder . $sSqlLimit);
117
118
        (ctype_digit($mLooking)) ? $rStmt->bindValue(':looking', $mLooking, \PDO::PARAM_INT) : $rStmt->bindValue(':looking', '%' . $mLooking . '%', \PDO::PARAM_STR);
119
120
        if (!$bCount) {
121
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
122
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
123
        }
124
125
        $rStmt->execute();
126
127
        if (!$bCount) {
128
            $mData = $rStmt->fetchAll(\PDO::FETCH_OBJ);
129
        } else {
130
            $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
131
            $mData = (int) $oRow->totalUsers;
132
            unset($oRow);
133
        }
134
        Db::free($rStmt);
135
136
        return $mData;
137
    }
138
139
    /**
140
     * Adding an Affiliate.
141
     *
142
     * @param array $aData
143
     *
144
     * @return integer The ID of the Affiliate.
145
     */
146
    public function add(array $aData)
147
    {
148
        $sCurrentDate = (new Framework\Date\CDateTime)->get()->dateTime('Y-m-d H:i:s');
149
150
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix('Affiliates') . '(email, username, password, firstName, lastName, sex, birthDate, bankAccount, ip, joinDate, lastActivity)
151
        VALUES (:email, :username, :password, :firstName, :lastName, :sex, :birthDate, :bankAccount, :ip, :joinDate, :lastActivity)');
152
153
        $rStmt->bindValue(':email',   trim($aData['email']), \PDO::PARAM_STR);
154
        $rStmt->bindValue(':username', trim($aData['username']), \PDO::PARAM_STR);
155
        $rStmt->bindValue(':password', Security::hashPwd($aData['password']), \PDO::PARAM_STR);
156
        $rStmt->bindValue(':firstName', $aData['first_name'], \PDO::PARAM_STR);
157
        $rStmt->bindValue(':lastName', $aData['last_name'], \PDO::PARAM_STR);
158
        $rStmt->bindValue(':sex', $aData['sex'], \PDO::PARAM_STR);
159
        $rStmt->bindValue(':birthDate', $aData['birth_date'], \PDO::PARAM_STR);
160
        $rStmt->bindValue(':bankAccount', $aData['bank_account'], \PDO::PARAM_STR);
161
        $rStmt->bindValue(':ip', $aData['ip'], \PDO::PARAM_STR);
162
        $rStmt->bindValue(':joinDate', $sCurrentDate, \PDO::PARAM_STR);
163
        $rStmt->bindValue(':lastActivity', $sCurrentDate, \PDO::PARAM_STR);
164
        $rStmt->execute();
165
        $this->setKeyId( Db::getInstance()->lastInsertId() ); // Set the affiliate's ID
166
        Db::free($rStmt);
167
        $this->setInfoFields($aData);
168
169
        return $this->getKeyId();
170
    }
171
172
    public function setInfoFields(array $aData)
173
    {
174
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix('AffiliatesInfo') . '(profileId, middleName, country, city, state, zipCode, phone, description, website)
175
            VALUES (:profileId, :middleName, :country, :city, :state, :zipCode, :phone, :description, :website)');
176
177
        $rStmt->bindValue(':profileId', $this->getKeyId(), \PDO::PARAM_INT);
178
        $rStmt->bindValue(':middleName', $aData['middle_name'], \PDO::PARAM_STR);
179
        $rStmt->bindParam(':country', $aData['country'], \PDO::PARAM_STR, 2);
180
        $rStmt->bindValue(':city', $aData['city'], \PDO::PARAM_STR);
181
        $rStmt->bindValue(':state', $aData['state'], \PDO::PARAM_STR);
182
        $rStmt->bindValue(':zipCode', $aData['zip_code'], \PDO::PARAM_STR);
183
        $rStmt->bindValue(':description', $aData['description'], \PDO::PARAM_STR);
184
        $rStmt->bindValue(':phone', $aData['phone'], \PDO::PARAM_STR);
185
        $rStmt->bindValue(':website', trim($aData['website']), \PDO::PARAM_STR);
186
187
        return $rStmt->execute();
188
    }
189
190
    /**
191
     * Get the Affiliate's Amount.
192
     *
193
     * @param integer $iProfileId
194
     *
195
     * @return integer|float The amount
196
     */
197
    public function getAmount($iProfileId)
198
    {
199
        $rStmt = Db::getInstance()->prepare('SELECT amount FROM' . Db::prefix('Affiliates') . ' WHERE profileId = :profileId LIMIT 1');
200
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
201
        $rStmt->execute();
202
        $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
203
        Db::free($rStmt);
204
205
        return $oRow->amount;
206
    }
207
}
208