PluginProbe
ShiftController Employee Shift Scheduling / 4.9.84
ShiftController Employee Shift Scheduling v4.9.84
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / hc3 / relations.php

relations.php in ShiftController Employee Shift Scheduling 4.9.84, at hc3/relations.php

146 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 interface HC3_Relations_
3 {
4 public function findFromIds( $relationName, $toModel );
5 public function findToIds( $fromModel, $relationName );
6
7 public function delete( $fromModel, $relationName, $toModel = NULL );
8 public function create( $fromModel, $relationName, $toModel );
9
10 public function updateTo( $fromModel, $relationName, $toModels );
11 public function updateFrom( $fromModels, $relationName, $toModel );
12 }
13
14 class HC3_Relations implements HC3_Relations_
15 {
16 public function __construct(
17 HC3_CrudFactory $crudFactory
18 )
19 {
20 $this->crud = $crudFactory->make('relation');
21 }
22
23 public function findFromIds( $relationName, $toModel )
24 {
25 $return = array();
26
27 $toModelId = is_object($toModel) ? $toModel->id() : $toModel;
28
29 $args = array();
30 $args[] = array('relation_name', '=', $relationName);
31 $args[] = array('to_id', '=', $toModelId);
32
33 $results = $this->crud->read( $args );
34
35 foreach( $results as $r ){
36 if( $r['from_id'] > 0 ){
37 $return[] = $r['from_id'];
38 }
39 }
40
41 return $return;
42 }
43
44 public function findToIds( $fromModel, $relationName )
45 {
46 $return = array();
47
48 $fromModelId = is_object($fromModel) ? $fromModel->id() : $fromModel;
49
50 $args = array();
51 $args[] = array('relation_name', '=', $relationName);
52 $args[] = array('from_id', '=', $fromModelId);
53
54 $results = $this->crud->read( $args );
55
56 foreach( $results as $r ){
57 if( $r['to_id'] > 0 ){
58 $return[] = $r['to_id'];
59 }
60 }
61
62 return $return;
63 }
64
65 public function updateTo( $relationName, $fromModel, $toModels )
66 {
67 $fromId = is_object($fromModel) ? $fromModel->id() : $fromModel;
68 $toIds = array();
69 foreach( $toModels as $e ){
70 $toIds[] = is_object($e) ? $e->id() : $e;
71 }
72
73 // get current
74 $currentToIds = $this->findToIds( $relationName, $fromId );
75
76 $toAdd = array_diff( $toIds, $currentToIds );
77 $toDelete = array_diff( $currentToIds, $toIds );
78
79 foreach( $toAdd as $toId ){
80 $this->create( $relationName, $fromId, $toId );
81 }
82
83 foreach( $toDelete as $toId ){
84 $this->delete( $relationName, $fromId, $toId );
85 }
86 }
87
88 public function updateFrom( $relationName, $toModel, $fromModels )
89 {
90 $toId = is_object($toModel) ? $toModel->id() : $toModel;
91 $fromIds = array();
92 foreach( $fromModels as $e ){
93 $fromIds[] = is_object($e) ? $e->id() : $e;
94 }
95
96 // get current
97 $currentFromIds = $this->findFromIds( $relationName, $toId );
98
99 $toAdd = array_diff( $fromIds, $currentFromIds );
100 $toDelete = array_diff( $currentFromIds, $fromIds );
101
102 foreach( $toAdd as $fromId ){
103 $this->create( $relationName, $fromId, $toId );
104 }
105
106 foreach( $toDelete as $fromId ){
107 $this->delete( $relationName, $fromId, $toId );
108 }
109 }
110
111 public function create( $relationName, $fromModel, $toModel )
112 {
113 $toId = is_object($toModel) ? $toModel->id() : $toModel;
114 $fromId = is_object($fromModel) ? $fromModel->id() : $fromModel;
115
116 $values = array(
117 'relation_name' => $relationName,
118 'to_id' => $toId,
119 'from_id' => $fromId,
120 );
121 $this->crud->create( $values );
122 }
123
124 public function delete( $relationName, $fromModel = NULL, $toModel = NULL )
125 {
126 $toId = is_object($toModel) ? $toModel->id() : $toModel;
127 $fromId = is_object($fromModel) ? $fromModel->id() : $fromModel;
128
129 // find existing
130 $args = array();
131 $args[] = array('relation_name', '=', $relationName);
132 if( $fromId ){
133 $args[] = array('from_id', '=', $fromId);
134 }
135 if( $toId ){
136 $args[] = array('to_id', '=', $toId);
137 }
138
139 $results = $this->crud->read( $args );
140 $ids = array_keys( $results );
141
142 foreach( $ids as $id ){
143 $this->crud->delete( $id );
144 }
145 }
146 }