/** * Create a new recipient */ public function createRecipientAction() { $email = $this->Request()->getParam('email', null); $groupId = $this->Request()->getParam('groupId', null); if ($email === null || $groupId === null) { $this->View()->assign(array('success' => false, 'message' => 'email and groupId needed')); return; } $model = new \Shopware\Models\Newsletter\Address(); if ($model === null) { $this->View()->assign(array('success' => false, 'message' => 'Could not create address')); return; } $model->setGroupId($groupId); $model->setEmail($email); $model->setIsCustomer(false); Shopware()->Models()->persist($model); Shopware()->Models()->flush(); $this->View()->assign(array('success' => true, 'data' => Shopware()->Models()->toArray($model))); }
/** * @param $filePath */ public function importNewsletter($filePath) { $newsletterRepository = $this->getManager()->getRepository('Shopware\Models\Newsletter\Address'); $newsletterGroupRepository = $this->getManager()->getRepository('Shopware\Models\Newsletter\Group'); $results = new Shopware_Components_CsvIterator($filePath, ';'); $insertCount = 0; $updateCount = 0; $errors = array(); $emailValidator = new Zend_Validate_EmailAddress(); foreach ($results as $newsletterData) { if (empty($newsletterData['email'])) { continue; } if (!$emailValidator->isValid($newsletterData['email'])) { continue; } // Set newsletter recipient/group $group = null; if ($newsletterData['group']) { $group = $newsletterGroupRepository->findOneBy(array('name' => $newsletterData['group'])); } $existingRecipient = $newsletterRepository->findOneBy(array('email' => $newsletterData['email'])); if (!$existingRecipient) { $recipient = new Shopware\Models\Newsletter\Address(); $recipient->setEmail($newsletterData['email']); if($newsletterData['userID']) { $recipient->setIsCustomer(true); }else{ $recipient->setIsCustomer(false); } if ($group) { $recipient->setNewsletterGroup($group); } $this->getManager()->persist($recipient); $this->getManager()->flush(); $insertCount++; } if ($group && !empty($newsletterData['firstname'])) { $sql = "INSERT INTO s_campaigns_maildata (groupId, email, firstname, lastname) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE groupId = ?, email = ?, firstname = ?, lastname = ?"; $values = array( $group->getId(), $newsletterData['email'], $newsletterData['firstname'], $newsletterData['lastname'], ); Shopware()->Db()->query($sql, array_merge(array_values($values), array_values($values))); if ($existingRecipient) { $updateCount++; } } } if (!empty($errors)) { $message = implode("<br>\n", $errors); echo json_encode(array( 'success' => false, 'message' => sprintf("Errors: $message"), )); return; } echo json_encode(array( 'success' => true, 'message' => sprintf("Imported: %s. Updated: %s.", $insertCount, $updateCount) )); return; }