1+ <?php
2+
3+
4+ namespace Serverfireteam \Panel ;
5+
6+ use Illuminate \Support \Facades \Hash ;
7+ use Maatwebsite \Excel \Concerns \ToModel ;
8+ use Serverfireteam \Panel \libs \AppHelper ;
9+ use Maatwebsite \Excel \Row ;
10+ use Maatwebsite \Excel \Concerns \OnEachRow ;
11+ use Maatwebsite \Excel \Concerns \WithHeadingRow ;
12+
13+ class EntityImport implements OnEachRow, WithHeadingRow
14+ {
15+ protected $ entity ;
16+ protected $ notNullColumnNames ;
17+ protected $ model ;
18+ protected $ columns ;
19+ protected $ key ;
20+ protected $ status ;
21+
22+ public function __construct ($ entity , $ status )
23+ {
24+ $ this ->entity = $ entity ;
25+ $ appHelper = new libs \AppHelper ();
26+
27+ $ className = $ appHelper ->getModel ($ entity );
28+ $ model = new $ className ;
29+ $ tablePrefix = \DB ::getTablePrefix ();
30+ $ table = $ model ->getTable ();
31+ $ columns = \Schema::getColumnListing ($ table );
32+ $ key = $ model ->getKeyName ();
33+
34+ $ notNullColumnNames = array ();
35+ $ notNullColumnsList = \DB ::select (\DB ::raw ("SHOW COLUMNS FROM ` " . $ tablePrefix .$ table . "` where `Null` = 'no' " ));
36+ if (!empty ($ notNullColumnsList )) {
37+ foreach ($ notNullColumnsList as $ notNullColumn ) {
38+ $ notNullColumnNames [] = $ notNullColumn ->Field ;
39+ }
40+ }
41+ $ this ->notNullColumnNames = $ notNullColumnNames ;
42+ $ this ->model = $ model ;
43+ $ this ->columns = $ columns ;
44+ $ this ->key = $ key ;
45+ $ this ->status = $ status ;
46+
47+ if ($ this ->status == 1 ) {
48+ $ this ->model ->truncate ();
49+ }
50+ }
51+
52+ /**
53+ * @param array $row
54+ *
55+ * @return User|null
56+ */
57+ public function onRow (Row $ row )
58+ {
59+ $ rowIndex = $ row ->getIndex ();
60+ $ row = $ row ->toArray ();
61+
62+ $ newData = array ();
63+ $ updatedData = array ();
64+
65+ foreach ($ this ->notNullColumnNames as $ notNullColumn ) {
66+ if (!isset ($ row [$ notNullColumn ])) {
67+ unset($ row );
68+ }
69+ }
70+
71+ if (!empty ($ row [$ this ->key ])) {
72+ $ exists = $ this ->model ->where ($ this ->key , '= ' , $ row [$ this ->key ])->count ();
73+ if (!$ exists ) {
74+ $ values = array ();
75+ foreach ($ this ->columns as $ col ) {
76+ if ($ col != $ this ->key && array_key_exists ($ col , $ row )) {
77+ $ values [$ col ] = $ row [$ col ];
78+ }
79+ }
80+ $ newData [] = $ values ;
81+ } else if ($ this ->status == 2 && $ exists ) {
82+ $ values = array ();
83+ foreach ($ this ->columns as $ col ) {
84+ if (array_key_exists ($ col , $ row ))
85+ $ values [$ col ] = $ row [$ col ];
86+ }
87+ $ updatedData [] = $ values ;
88+ }
89+ }
90+
91+ // insert data into table
92+ if (!empty ($ newData )) {
93+ $ this ->model ->insert ($ newData );
94+ }
95+
96+ // update available data
97+ if (!empty ($ updatedData )) {
98+ foreach ($ updatedData as $ data ) {
99+ $ keyValue = $ data [$ this ->key ];
100+ unset($ data [$ this ->key ]);
101+ $ this ->model ->where ($ this ->key , $ keyValue )->update ($ data );
102+ }
103+ }
104+ }
105+ }
0 commit comments