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