PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.6.9
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.6.9
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / framework / models / peoples.php

peoples.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.9, at includes/framework/models/peoples.php

147 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WeDevs\ERP\Framework\Models;
4
5 use WeDevs\ERP\Framework\Model;
6
7 class People extends Model {
8 protected $primaryKey = 'id';
9
10 protected $table = 'erp_peoples';
11
12 public $timestamps = false;
13
14 protected $fillable = [ 'user_id', 'first_name', 'last_name', 'company', 'email', 'phone', 'mobile',
15 'other', 'website', 'fax', 'notes', 'street_1', 'street_2', 'city', 'state', 'postal_code', 'country',
16 'currency', 'life_stage', 'hash', 'contact_owner', 'created_by', 'created', ];
17
18 /**
19 * Peoplemeta model relation
20 *
21 * @since 1.1.7
22 *
23 * @return object
24 */
25 public function meta() {
26 return $this->hasMany( '\WeDevs\ERP\Framework\Models\Peoplemeta', 'erp_people_id' );
27 }
28
29 /**
30 * Fetch people with types
31 *
32 * @param object $query
33 * @param string $type
34 *
35 * @return object
36 */
37 public function scopeType( $query, $type ) {
38 if ( is_array( $type ) ) {
39 return $query->whereHas( 'types', function ( $qry ) use ( $type ) {
40 $qry->whereIn( 'name', $type )->whereNull( 'deleted_at' );
41 } );
42 } elseif ( $type !== 'all' ) {
43 return $query->whereHas( 'types', function ( $qry ) use ( $type ) {
44 $qry->where( 'name', '=', $type )->whereNull( 'deleted_at' );
45 } );
46 }
47
48 return $query;
49 }
50
51 /**
52 * Fetch only trashed people
53 *
54 * @since 1.0
55 *
56 * @param collection $query
57 * @param string|array $type
58 *
59 * @return collection
60 */
61 public function scopeTrashed( $query, $type ) {
62 if ( is_array( $type ) ) {
63 return $query->whereHas( 'types', function ( $qry ) use ( $type ) {
64 $qry->whereIn( 'name', $type )->whereNotNull( 'deleted_at' );
65 } );
66 } elseif ( is_string( $type ) ) {
67 return $query->whereHas( 'types', function ( $qry ) use ( $type ) {
68 $qry->where( 'name', '=', $type )->whereNotNull( 'deleted_at' );
69 } );
70 }
71
72 return $query;
73 }
74
75 /**
76 * Get the types of peoples
77 *
78 * @return object
79 */
80 public function types() {
81 global $wpdb;
82
83 return $this->belongsToMany( '\WeDevs\ERP\Framework\Models\PeopleTypes', $wpdb->prefix . 'erp_people_type_relations' );
84 }
85
86 /**
87 * Assign type to a people
88 *
89 * @param mixed $type
90 *
91 * @return mixed
92 */
93 public function assignType( $type ) {
94 return $this->types()->attach( $type );
95 }
96
97 /**
98 * Remove type from a people
99 *
100 * @param mixed $type
101 *
102 * @return mixed
103 */
104 public function removeType( $type ) {
105 return $this->types()->detach( $type );
106 }
107
108 /**
109 * Temporary trashed a people
110 *
111 * @param mixed $type
112 *
113 * @return mixed
114 */
115 public function softDeleteType( $type ) {
116 return $this->types()->updateExistingPivot( $type->id, ['deleted_at' => current_time( 'mysql' ) ] );
117 }
118
119 /**
120 * Restore for trash
121 *
122 * @param mixed $type
123 *
124 * @return mixed
125 */
126 public function restore( $type ) {
127 return $this->types()->updateExistingPivot( $type->id, [ 'deleted_at' => null ] );
128 }
129
130 /**
131 * Does this people has a particular type?
132 *
133 * @param string $name
134 *
135 * @return bool
136 */
137 public function hasType( $name ) {
138 foreach ( $this->types as $type ) {
139 if ( $type->name == $name ) {
140 return true;
141 }
142 }
143
144 return false;
145 }
146 }
147