示例#1
0
    function run()
    {
        $errors = array();
        $importFields = array('email' => 'email', 'name_f' => 'name_f', 'name_l' => 'name_l');
        $subusers_fields = $this->getDi()->config->get('subusers_fields', array());
        /*        if (in_array('login', $subusers_fields)) {
                    $importFields['login'] = '******';
                }
                if (in_array('pass', $subusers_fields)) {
                    $importFields['pass'] = '******';
                }*/
        foreach ($this->getDi()->config->get('subusers_fields', array()) as $field) {
            $importFields[$field] = $field;
        }
        $form = new Am_Form();
        $form->setAttribute('target', '_top');
        $form->setAttribute('enctype', 'multipart/form-data');
        $form->addStatic()->setContent('<div>' . ___('File should contain CSV list of user records for import in the following format:<br />
<strong>%s</strong>', implode(',', $importFields)) . '</div>');
        $form->addFile('file[]', null, array('prefix' => self::UPLOAD_PREFIX))->setLabel(___('File'))->addRule('required', ___('This field is a requried field'));
        $options = $this->getDi()->subusersSubscriptionTable->getProductOptions($this->reseller, true);
        reset($options);
        if (count($options) == 1) {
            $form->addHidden('groups[0]')->setValue(key($options))->toggleFrozen(true);
        } else {
            $form->addMagicSelect('groups')->setLabel(___('Groups'))->loadOptions($options);
        }
        $form->addSaveButton(___('Do Import'));
        $this->initForm($form);
        if ($form->isSubmitted()) {
            $value = $form->getValue();
            $upload = new Am_Upload($this->getDi());
            $upload->setPrefix(self::UPLOAD_PREFIX)->setTemp(3600);
            $upload->processSubmit('file');
            list($file) = $upload->getUploads();
            if (!$file) {
                throw new Am_Exception_InputError(___('CSV File was not specified'));
            }
            $pn = fopen($file->getFullPath(), 'r');
            while ($res = fgetcsv($pn)) {
                if (count($res) == count($importFields)) {
                    $imp = array();
                    foreach ($importFields as $fieldName => $v) {
                        $imp[$fieldName] = trim(array_shift($res));
                    }
                    $user = Am_Di::getInstance()->userRecord;
                    if ($error = $this->checkUniqEmail($imp['email'])) {
                        $errors[] = $error;
                        continue;
                    }
                    if (isset($imp['login']) && ($error = $this->checkUniqLogin($imp['login']))) {
                        $errors[] = $error;
                        continue;
                    }
                    $user->email = $imp['email'];
                    $user->name_f = $imp['name_f'];
                    $user->name_l = $imp['name_l'];
                    isset($imp['pass']) ? $user->setPass($imp['pass']) : $user->generatePassword();
                    isset($imp['login']) ? $user->login = $imp['login'] : $user->generateLogin();
                    foreach (array('login', 'email', 'pass', 'name_f', 'name_l') as $k) {
                        unset($imp[$k]);
                    }
                    foreach ($imp as $k => $v) {
                        $user->set($k, $v);
                    }
                    $user->data()->set('signup_email_sent', 1);
                    $user->set('subusers_parent_id', $this->reseller->pk());
                    $user->is_approved = 1;
                    $user->save();
                    if ($et = Am_Mail_Template::load('subusers.registration_mail', $user->lang)) {
                        $et->setUser($user);
                        $et->setPassword($user->getPlaintextPass());
                        $et->setReseller($this->reseller);
                        //backward compatibality (reseller_name_f, reseller_name_l, reseller_email)
                        $et->setReseller_name_f($this->reseller->name_f);
                        $et->setReseller_name_l($this->reseller->name_l);
                        $et->setReseller_email($this->reseller->email);
                        if (!empty($value['groups'])) {
                            $userTitle = array();
                            foreach ($this->getDi()->productTable->loadIds($value['groups']) as $product) {
                                $userTitle[] = $product->title;
                            }
                            $et->setUser_product(join(', ', $userTitle));
                            $resellerTitle = array();
                            $conditions = array('subusers_product_id' => $value['groups']);
                            foreach ($this->getDi()->productTable->findBy($conditions) as $product) {
                                $resellerTitle[] = $product->title;
                            }
                            $et->setReseller_product(join(', ', $resellerTitle));
                        }
                        $et->send($user);
                    }
                    $this->getDi()->subusersSubscriptionTable->setForUser($user->pk(), $value['groups']);
                }
            }
            fclose($pn);
            $this->getDi()->modules->get('subusers')->checkAndUpdate($this->reseller);
            if ($errors) {
                $out = '<ul class="errors">';
                foreach ($errors as $error) {
                    $out .= sprintf('<li>%s</li>', $error);
                }
                $out .= "</ul>";
                echo $out . $this->renderBackUrl() . '<br /><br />';
            } else {
                $this->grid->redirectBack();
            }
        } else {
            echo $this->renderTitle();
            echo $form;
        }
    }