Exemplo n.º 1
0
	/**
	 * Save
	 *
	 * Save loaded/added trigger email record to the database.
	 * If triggeremailsid is specified, it will update the existing record with the newer data,
	 * otherwise, it will insert the record into the database.
	 *
	 * If "record" parameter is specified, it will use the record instead of
	 * the record that has been loaded to the API
	 *
	 * @param Array $record Record to be saved (Associative array) (Optional)
	 * @return Mixed Returns triggeremailsid if successful, FALSE otherwise
	 *
	 * @uses TriggerEmails_API::$_properties
	 * @uese Db::Quote()
	 * @uses Db::StartTransaction()
	 * @uses API::CreateQueue()
	 * @uses Stats_API
	 * @uses Stats_API::SaveNewsletterStats()
	 * @uses Db::RollbackTransaction()
	 * @uses SENDSTUDIO_TABLEPREFIX
	 * @uses Db::GetError()
	 * @uses Db::LastId()
	 * @uses Db::CommitTransaction()
	 *
	 * @todo Test save using parameter
	 */
	public function Save($record = array())
	{
		// --------------------------------------------------------------------------------------------
		// If record is specified from the parameter, that means the function need to save
		// the record that got passed in instead of the record that may/may not be "loaded" in the class
		// properties.
		//
		// This is predominantly will be used by the XML API
		// --------------------------------------------------------------------------------------------
			if (!empty($record)) {
				$api = new TriggerEmails_API();

				if (array_key_exists('triggeremailsid', $record) && !empty($record['triggeremailsid'])) {
					if (!$api->Load($record['triggeremailsid'])) {
						return false;
					}
				}

				foreach ($record as $key => $value) {
					if (in_array($key, $this->_propertiesReadOnly)) {
						continue;
					}

					$api->{$key} = $value;
				}

				return $api->Save();
			}
		// --------------------------------------------------------------------------------------------

		// Make necessary modifications of the record
		switch ($this->triggertype) {
			case 'f': break;
			case 's': break;

			case 'l':
				// Only send once for "Link Click"
				$this->triggerinterval = 0;
			break;

			case 'n':
				// Only send once for "Email open"
				$this->triggerinterval = 0;
			break;
		}

		$record = $this->_properties;
		foreach ($record as $key => $value) {
			if (is_null($value)) {
				$record[$key] = 'NULL';
			} elseif (!is_numeric($value)) {
				$record[$key] = "'" . $this->Db->Quote(strval($value)) . "'";
			}
		}

		$status = $this->_validateRecord($this->_properties, $this->_data, $this->_actions);
		if (!$status) {
			trigger_error('Invalid record', E_USER_NOTICE);
			return false;
		}

		// ----- INSERT
			if ($this->triggeremailsid == 0) {
				$this->Db->StartTransaction();

				// Unset this, as we don't want the ID to be inserted too
				unset($record['triggeremailsid']);

				// Populate read-only field
				$record['createdate'] = time();
				$record['queueid'] = $this->CreateQueue('triggeremail');

				/**
				 * Create statistic for trigger, and populate statid
				 */
					require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'stats.php');
					$statsapi = new Stats_API();

					$tempDetails = array(
						'Job' => 0,
						'Queue' => $record['queueid'],
						'TrackOpens' => 0,
						'TrackLinks' => 0,
						'Newsletter' => 0,
						'SendFromName' => '',
						'SendFromEmail' => '',
						'BounceEmail' => '',
						'ReplyToEmail' => '',
						'Charset' => '',
						'SendCriteria' => '',
						'SendSize' => 0,
						'SentBy' => 0,
						'NotifyOwner' => 0,
						'SendType' => 'triggeremail',
						'Lists' => array(0),
					);

					$statid = $statsapi->SaveNewsletterStats($tempDetails);
					if (!$statid) {
						$this->Db->RollbackTransaction();
						trigger_error('Cannot get statistic ID', E_USER_NOTICE);
						return false;
					}

					$record['statid'] = $statid;
				/**
				 * -----
				 */

				// Add to triggeremails table
				$query = 'INSERT INTO [|PREFIX|]triggeremails(' . implode(',', array_keys($record)) . ') VALUES (' . implode(',', $record) . ')';
				if (SENDSTUDIO_DATABASE_TYPE == 'pgsql') {
					$query .= ' RETURNING triggeremailsid';
				}

				$status = $this->Db->Query($query);
				if ($status === false) {
					list($msg, $errno) = $this->Db->GetError();
					$this->Db->RollbackTransaction();
					trigger_error($msg, $errno);
					return false;
				}

				// Get new ID
				if (SENDSTUDIO_DATABASE_TYPE == 'pgsql') {
					$temp = $this->Db->Fetch($status);
					$this->Db->FreeResult($status);

					$record['triggeremailsid'] = $temp['triggeremailsid'];
				} else {
					$record['triggeremailsid'] = $this->Db->LastId(SENDSTUDIO_TABLEPREFIX . 'triggeremails_triggeremailsid_seq');
				}

				// Add data and it's data if available
				if (is_array($this->_actions)) {
					if (!$this->_updateData($record['triggeremailsid'], $this->_data)) {
						$this->Db->RollbackTransaction();
						trigger_error('Unable to save data', E_USER_NOTICE);
						return false;
					}
				}

				// Add actions and it's data if available
				if (is_array($this->_actions)) {
					if (!$this->_updateActions($record['triggeremailsid'], $this->_actions)) {
						$this->Db->RollbackTransaction();
						trigger_error('Unable to save action data', E_USER_NOTICE);
						return false;
					}
				}

				$this->Db->CommitTransaction();

				// Populate the properties with new data
				$this->_properties['statid'] = $record['statid'];
				$this->_properties['queueid'] = $record['queueid'];
				$this->_properties['triggeremailsid'] = $record['triggeremailsid'];
				$this->_properties['createdate'] = $record['createdate'];

				return $record['triggeremailsid'];
			}
		// -----


		// ----- UPDATE
			$this->Db->StartTransaction();

			$id = $record['triggeremailsid'];
			unset($record['triggeremailsid']);

			$temp = array();
			foreach ($record as $key => $value) {
				array_push($temp, "{$key} = {$value}");
			}

			$query = 'UPDATE [|PREFIX|]triggeremails SET ' . implode(',', $temp) . ' WHERE triggeremailsid = ' . $id;
			$status = $this->Db->Query($query);
			if ($status === false) {
				list($msg, $errno) = $this->Db->GetError();
				$this->Db->RollbackTransaction();
				trigger_error($msg, $errno);
				return false;
			}

			// Add data and it's ata if available
			if (is_array($this->_actions)) {
				if (!$this->_updateData($id, $this->_data)) {
					$this->Db->RollbackTransaction();
					trigger_error('Unable to save data', E_USER_NOTICE);
					return false;
				}
			}

			// Add actions and it's data if available
			if (is_array($this->_actions)) {
				if (!$this->_updateActions($id, $this->_actions)) {
					$this->Db->RollbackTransaction();
					trigger_error('Unable to save action data', E_USER_NOTICE);
					return false;
				}
			}

			$this->Db->CommitTransaction();
			return $id;
		// -----
	}