Skip to content

Commit

Permalink
improved initYiiStrap and update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cornernote committed Jan 4, 2014
1 parent f3697a6 commit e708bf5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,14 @@ Download the [latest version](https://github.com/cornernote/yii-email-module/arc

## Configuration

If you installed with composer, you should set an alias to your `vendor` folder in your yii configuration:

```php
return array(
'aliases' => array(
'vendor' => '/path/to/vendor',
),
);
```

Add yii-email-module to the `modules` in your yii configuration:

```php
return array(
'modules' => array(
'email' => array(
// path to the EmailModule class
'class' => 'vendor.cornernote.yii-email-module.email.EmailModule',
// if you downloaded into modules
//'class' => 'application.modules.email.EmailModule',
'class' => '/path/to/vendor/cornernote/yii-email-module/email/EmailModule',

// add a list of users who can access the email module
'adminUsers' => array('admin'),
Expand Down Expand Up @@ -199,11 +187,13 @@ return array(
Yii::app()->emailManager->email('[email protected]', 'test email', '<b>Hello</b> <i>World<i>!');
```


### Using Templates

To send more complex emails you will need to use Email Templates.

### Component

#### Component

Create a new component in `components/Email.php`:

Expand Down Expand Up @@ -238,7 +228,8 @@ class Email {
}
```

### PHP Templates

#### PHP Templates

Subject `views/emails/example/subject.php`:
```php
Expand All @@ -255,7 +246,8 @@ Message `views/emails/example/message.php`:
<?php echo 'Here is an <b>awesome</b> email!';
```

### DB Templates

#### DB Templates

Subject
```
Expand All @@ -273,7 +265,7 @@ Here is an <b>awesome</b> email!
```


### Sending the Email
#### Sending the Email

Now you can send an email like this:

Expand All @@ -282,6 +274,7 @@ $user = User::model()->findByPk(123);
Email::sendUserWelcome($user);
```


### Sending Spooled Emails

You can send the spooled emails using the yiic command:
Expand All @@ -290,6 +283,7 @@ You can send the spooled emails using the yiic command:
yiic emailSpool
```


### Automatically Sending

Setup [lockrun](https://github.com/pushcx/lockrun) for overlap protection. This allows us to setup a cron job that will run every minute, with no risk of a new process starting if an existing process is running.
Expand Down
51 changes: 33 additions & 18 deletions email/EmailModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,8 @@ public function init()
if (empty($this->modelMap[$method][$name]))
$this->modelMap[$method][$name] = $options;

// when in web application
if (Yii::app() instanceof CWebApplication) {
// and in this module
$route = explode('/', Yii::app()->urlManager->parseUrl(Yii::app()->request));
if ($route[0] == $this->id) {
// setup yiiStrap components
if ($this->yiiStrapPath) {
Yii::setPathOfAlias('bootstrap', realpath($this->yiiStrapPath));
Yii::import('bootstrap.helpers.TbHtml');
Yii::app()->setComponents(array(
'bootstrap' => array(
'class' => 'bootstrap.components.TbApi',
),
), false);
}
}
}

// init yiiStrap
$this->initYiiStrap();
}

/**
Expand Down Expand Up @@ -192,4 +176,35 @@ public function setAssetsUrl($value)
$this->_assetsUrl = $value;
}

/**
* Setup yiiStrap, works even if YiiBooster is used in main app.
*/
public function initYiiStrap()
{
// check that we are in a web application
if (!(Yii::app() instanceof CWebApplication))
return;
// and in this module
$route = explode('/', Yii::app()->urlManager->parseUrl(Yii::app()->request));
if ($route[0] != $this->id)
return;
// and yiiStrap is not configured
if (Yii::getPathOfAlias('bootstrap') && file_exists(Yii::getPathOfAlias('bootstrap.helpers') . '/TbHtml.php'))
return;
// try to guess yiiStrapPath
if ($this->yiiStrapPath === null)
$this->yiiStrapPath = Yii::getPathOfAlias('vendor.crisu83.yiistrap');
// check for valid path
if (!realpath($this->yiiStrapPath))
return;
// setup yiiStrap components
Yii::setPathOfAlias('bootstrap', realpath($this->yiiStrapPath));
Yii::import('bootstrap.helpers.TbHtml');
Yii::app()->setComponents(array(
'bootstrap' => array(
'class' => 'bootstrap.components.TbApi',
),
), false);
}

}

0 comments on commit e708bf5

Please sign in to comment.