ImportMembersTask.php 3.28 KB
Newer Older
Thomas Löffler's avatar
Thomas Löffler committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
/**
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

class Tx_T3oMembership_Task_ImportMembersTask extends tx_scheduler_Task {

	/**
	 * @var string
	 */
	protected $importFile = '';

	/**
	 * @var integer
	 */
	protected $membershipStoragePid = 0;

	/**
	 * @return boolean
	 */
	public function execute() {
		// does the import file exist?
		if (!file_exists(PATH_site . $this->getImportFile())) {
			return FALSE;
		} else {
Thomas Löffler's avatar
Thomas Löffler committed
35
			$this->getDatabaseConnection()->exec_TRUNCATEquery('tx_t3omembership_domain_model_member');
Thomas Löffler's avatar
Thomas Löffler committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
			$fileData = file(PATH_site . $this->getImportFile());
			foreach ($fileData as $key => $line) {
				if ($key) {
					$fields = t3lib_div::trimExplode("\t", $line);
					$member = array(
						'name' =>  $fields[6],
						'external_id' => (int) $fields[0],
						'address' => $fields[7] ? $fields[7] : $fields[8],
						'zip' => $fields[10],
						'city' => $fields[11],
						'end_date' => strtotime($fields[16]),
						'membership' => $this->getMembershipUid($fields[12]),
						'pid' => $this->getMembershipStoragePid(),
						'crdate' => time(),
						'tstamp' => time()
					);

					$this->getDatabaseConnection()->exec_INSERTquery(
						'tx_t3omembership_domain_model_member',
						$member
					);
				}
			}
		}

		return TRUE;
	}

	/**
	 * @param string $membershipName
	 * @return integer
	 */
	protected function getMembershipUid($membershipName) {
Thomas Löffler's avatar
Thomas Löffler committed
69
70
71
		$membershipName = $this->getDatabaseConnection()
			->fullQuoteStr(trim(str_replace('Membership', '', $membershipName)), 'tx_t3omembership_domain_model_membership');

Thomas Löffler's avatar
Thomas Löffler committed
72
73
74
		$membershipRecord = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
			'uid',
			'tx_t3omembership_domain_model_membership',
Thomas Löffler's avatar
Thomas Löffler committed
75
			'name = ' . $membershipName . ' AND NOT deleted AND NOT hidden'
Thomas Löffler's avatar
Thomas Löffler committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
		);
		if (!empty($membershipRecord)) {
			$membershipUid = $membershipRecord['uid'];
		} else {
			$newMembership = array(
				'name' => mysql_real_escape_string($membershipName),
				'pid' => $this->getMembershipStoragePid(),
				'crdate' => time(),
				'tstamp' => time()
			);
			$this->getDatabaseConnection()->exec_INSERTquery(
				'tx_t3omembership_domain_model_membership',
				$newMembership
			);
			$membershipUid = $this->getDatabaseConnection()->sql_insert_id();
		}

		return $membershipUid;
	}

	/**
	 * Gets importFile
	 * @return string
	 */
	public function getImportFile() {
		return $this->importFile;
	}

	/**
	 * Sets importFile
	 * @param string $importFile
	 */
	public function setImportFile($importFile) {
		$this->importFile = $importFile;
	}

	/**
	 * Gets membershipStoragePid
	 * @return int
	 */
	public function getMembershipStoragePid() {
		return $this->membershipStoragePid;
	}

	/**
	 * Sets membershipStoragePid
	 * @param int $membershipStoragePid
	 */
	public function setMembershipStoragePid($membershipStoragePid) {
		$this->membershipStoragePid = $membershipStoragePid;
	}

	/**
	 * @return t3lib_DB
	 */
	protected function getDatabaseConnection() {
		return $GLOBALS['TYPO3_DB'];
	}
}