Issues (3627)

app/bundles/LeadBundle/Entity/Company.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\LeadBundle\Entity;
13
14
use Doctrine\ORM\Mapping as ORM;
15
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
16
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
17
use Mautic\CoreBundle\Entity\FormEntity;
18
use Mautic\LeadBundle\Model\FieldModel;
19
use Mautic\UserBundle\Entity\User;
20
21
/**
22
 * Class Company.
23
 */
24
class Company extends FormEntity implements CustomFieldEntityInterface
25
{
26
    use CustomFieldEntityTrait;
27
28
    const FIELD_ALIAS = 'company';
29
30
    /**
31
     * @var int
32
     */
33
    private $id;
34
35
    /**
36
     * @var int
37
     */
38
    private $score = 0;
39
40
    /**
41
     * @var \Mautic\UserBundle\Entity\User
42
     */
43
    private $owner;
44
45
    /**
46
     * @var array
47
     */
48
    private $socialCache = [];
49
50
    private $email;
51
52
    private $address1;
53
54
    private $address2;
55
56
    private $phone;
57
58
    private $city;
59
60
    private $state;
61
62
    private $zipcode;
63
64
    private $country;
65
66
    private $name;
67
68
    private $website;
69
70
    private $industry;
71
72
    private $description;
73
74
    public function __clone()
75
    {
76
        $this->id = null;
77
78
        parent::__clone();
79
    }
80
81
    /**
82
     * Get social cache.
83
     *
84
     * @return mixed
85
     */
86
    public function getSocialCache()
87
    {
88
        return $this->socialCache;
89
    }
90
91
    /**
92
     * Set social cache.
93
     *
94
     * @param $cache
95
     */
96
    public function setSocialCache($cache)
97
    {
98
        $this->socialCache = $cache;
99
    }
100
101
    public static function loadMetadata(ORM\ClassMetadata $metadata)
102
    {
103
        $builder = new ClassMetadataBuilder($metadata);
104
        $builder->setTable('companies')
105
            ->setCustomRepositoryClass('Mautic\LeadBundle\Entity\CompanyRepository');
106
107
        $builder->createField('id', 'integer')
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\Mapping\Bui...Builder::isPrimaryKey() has been deprecated: Use makePrimaryKey() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

107
        /** @scrutinizer ignore-deprecated */ $builder->createField('id', 'integer')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
108
            ->isPrimaryKey()
109
            ->generatedValue()
110
            ->build();
111
112
        $builder->createField('socialCache', 'array')
113
            ->columnName('social_cache')
114
            ->nullable()
115
            ->build();
116
117
        $builder->createManyToOne('owner', 'Mautic\UserBundle\Entity\User')
118
            ->cascadeDetach()
119
            ->cascadeMerge()
120
            ->addJoinColumn('owner_id', 'id', true, false, 'SET NULL')
121
            ->build();
122
123
        $builder->createField('score', 'integer')
124
            ->nullable()
125
            ->build();
126
127
        self::loadFixedFieldMetadata(
128
            $builder,
129
            [
130
                'email',
131
                'address1',
132
                'address2',
133
                'phone',
134
                'city',
135
                'state',
136
                'zipcode',
137
                'country',
138
                'name',
139
                'website',
140
                'industry',
141
                'description',
142
            ],
143
            FieldModel::$coreCompanyFields
144
        );
145
    }
146
147
    /**
148
     * Prepares the metadata for API usage.
149
     *
150
     * @param $metadata
151
     */
152
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
153
    {
154
        $metadata->setGroupPrefix('companyBasic')
155
            ->addListProperties(
156
                [
157
                    'id',
158
                    'name',
159
                    'email',
160
                    'address1',
161
                    'address2',
162
                    'phone',
163
                    'city',
164
                    'state',
165
                    'zipcode',
166
                    'country',
167
                    'website',
168
                    'industry',
169
                    'description',
170
                    'score',
171
                ]
172
            )
173
            ->setGroupPrefix('company')
174
            ->addListProperties(
175
                [
176
                    'id',
177
                    'fields',
178
                    'score',
179
                ]
180
            )
181
            ->build();
182
    }
183
184
    /**
185
     * @param string $prop
186
     * @param mixed  $val
187
     */
188
    protected function isChanged($prop, $val)
189
    {
190
        $getter  = 'get'.ucfirst($prop);
191
        $current = $this->$getter();
192
        if ('owner' == $prop) {
193
            if ($current && !$val) {
194
                $this->changes['owner'] = [$current->getName().' ('.$current->getId().')', $val];
195
            } elseif (!$current && $val) {
196
                $this->changes['owner'] = [$current, $val->getName().' ('.$val->getId().')'];
197
            } elseif ($current && $val && $current->getId() != $val->getId()) {
198
                $this->changes['owner'] = [
199
                    $current->getName().'('.$current->getId().')',
200
                    $val->getName().'('.$val->getId().')',
201
                ];
202
            }
203
        } else {
204
            parent::isChanged($prop, $val);
205
        }
206
    }
207
208
    /**
209
     * Get id.
210
     *
211
     * @return int
212
     */
213
    public function getId()
214
    {
215
        return $this->id;
216
    }
217
218
    /**
219
     * Get the primary identifier for the company.
220
     *
221
     * @return string
222
     */
223
    public function getPrimaryIdentifier()
224
    {
225
        if ($name = $this->getName()) {
226
            return $name;
227
        } elseif (!empty($this->fields['core']['companyemail']['value'])) {
228
            return $this->fields['core']['companyemail']['value'];
229
        }
230
    }
231
232
    /**
233
     * Set owner.
234
     *
235
     * @param User $owner
236
     *
237
     * @return Company
238
     */
239
    public function setOwner(User $owner = null)
240
    {
241
        $this->isChanged('owner', $owner);
242
        $this->owner = $owner;
243
244
        return $this;
245
    }
246
247
    /**
248
     * Get owner.
249
     *
250
     * @return User
251
     */
252
    public function getOwner()
253
    {
254
        return $this->owner;
255
    }
256
257
    /**
258
     * Returns the user to be used for permissions.
259
     *
260
     * @return User|int
261
     */
262
    public function getPermissionUser()
263
    {
264
        return (null === $this->getOwner()) ? $this->getCreatedBy() : $this->getOwner();
265
    }
266
267
    /**
268
     * Set score.
269
     *
270
     * @param User $score
271
     *
272
     * @return Company
273
     */
274
    public function setScore($score)
275
    {
276
        $score = (int) $score;
277
278
        $this->isChanged('score', $score);
279
        $this->score = $score;
280
281
        return $this;
282
    }
283
284
    /**
285
     * Get score.
286
     *
287
     * @return int
288
     */
289
    public function getScore()
290
    {
291
        return $this->score;
292
    }
293
294
    /**
295
     * @return mixed
296
     */
297
    public function getName()
298
    {
299
        return $this->name;
300
    }
301
302
    /**
303
     * @param mixed $name
304
     *
305
     * @return Company
306
     */
307
    public function setName($name)
308
    {
309
        $this->name = $name;
310
311
        return $this;
312
    }
313
314
    /**
315
     * @return mixed
316
     */
317
    public function getEmail()
318
    {
319
        return $this->email;
320
    }
321
322
    /**
323
     * @param mixed $email
324
     *
325
     * @return Company
326
     */
327
    public function setEmail($email)
328
    {
329
        $this->email = $email;
330
331
        return $this;
332
    }
333
334
    /**
335
     * @return mixed
336
     */
337
    public function getAddress1()
338
    {
339
        return $this->address1;
340
    }
341
342
    /**
343
     * @param mixed $address1
344
     *
345
     * @return Company
346
     */
347
    public function setAddress1($address1)
348
    {
349
        $this->address1 = $address1;
350
351
        return $this;
352
    }
353
354
    /**
355
     * @return mixed
356
     */
357
    public function getAddress2()
358
    {
359
        return $this->address2;
360
    }
361
362
    /**
363
     * @param mixed $address2
364
     *
365
     * @return Company
366
     */
367
    public function setAddress2($address2)
368
    {
369
        $this->address2 = $address2;
370
371
        return $this;
372
    }
373
374
    /**
375
     * @return mixed
376
     */
377
    public function getPhone()
378
    {
379
        return $this->phone;
380
    }
381
382
    /**
383
     * @param mixed $phone
384
     *
385
     * @return Company
386
     */
387
    public function setPhone($phone)
388
    {
389
        $this->phone = $phone;
390
391
        return $this;
392
    }
393
394
    /**
395
     * @return mixed
396
     */
397
    public function getCity()
398
    {
399
        return $this->city;
400
    }
401
402
    /**
403
     * @param mixed $city
404
     *
405
     * @return Company
406
     */
407
    public function setCity($city)
408
    {
409
        $this->city = $city;
410
411
        return $this;
412
    }
413
414
    /**
415
     * @return mixed
416
     */
417
    public function getState()
418
    {
419
        return $this->state;
420
    }
421
422
    /**
423
     * @param mixed $state
424
     *
425
     * @return Company
426
     */
427
    public function setState($state)
428
    {
429
        $this->state = $state;
430
431
        return $this;
432
    }
433
434
    /**
435
     * @return mixed
436
     */
437
    public function getZipcode()
438
    {
439
        return $this->zipcode;
440
    }
441
442
    /**
443
     * @param mixed $zipcode
444
     *
445
     * @return Company
446
     */
447
    public function setZipcode($zipcode)
448
    {
449
        $this->zipcode = $zipcode;
450
451
        return $this;
452
    }
453
454
    /**
455
     * @return mixed
456
     */
457
    public function getCountry()
458
    {
459
        return $this->country;
460
    }
461
462
    /**
463
     * @param mixed $country
464
     *
465
     * @return Company
466
     */
467
    public function setCountry($country)
468
    {
469
        $this->country = $country;
470
471
        return $this;
472
    }
473
474
    /**
475
     * @return mixed
476
     */
477
    public function getWebsite()
478
    {
479
        return $this->website;
480
    }
481
482
    /**
483
     * @param mixed $website
484
     *
485
     * @return Company
486
     */
487
    public function setWebsite($website)
488
    {
489
        $this->website = $website;
490
491
        return $this;
492
    }
493
494
    /**
495
     * @return mixed
496
     */
497
    public function getIndustry()
498
    {
499
        return $this->industry;
500
    }
501
502
    /**
503
     * @param mixed $industry
504
     *
505
     * @return Company
506
     */
507
    public function setIndustry($industry)
508
    {
509
        $this->industry = $industry;
510
511
        return $this;
512
    }
513
514
    /**
515
     * @return mixed
516
     */
517
    public function getDescription()
518
    {
519
        return $this->description;
520
    }
521
522
    /**
523
     * @param mixed $description
524
     *
525
     * @return Company
526
     */
527
    public function setDescription($description)
528
    {
529
        $this->description = $description;
530
531
        return $this;
532
    }
533
}
534