Search code examples
phpyii2swiftmailer

Yii2 - Swiftmailer - adding multiple dynamic records per user


The setup is like this:

I have users/clients who book classes. Classes can be multiple classes per date and per location and per instructor.

Now there is a need to cancel the bookings for unavoidable reasons for specific location.So I added a table site_closure with location_name, start_date and end_date, and in the controller update the order_item status as cancelled against all the bookings for the dates posted in site_closure

Next step is I need to send the mail to user/client detailing the list of classes cancelled.

so in my site_closure controller create action I have added this code:

public function actionCreate()
    {
        $model = new SiteClosure();
        $st = Yii::$app->getTable;
        if ($model->load(Yii::$app->request->post()) ) {
            $order_items= OrderItem::find()->where(['location_id'=>$model->location_name])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();
            $users_id_array = OrderItem::find()->select('distinct(user_id)')->where(['location_id'=>$model->location_name])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->asArray()->all();
            $users_id = array_values($users_id_array[0]);
            $users=implode(',',$users_id);
            $client_emails = User::find()->where(['in','user.id' ,$users])->asArray()->all();

           // var_dump($client_emails);exit;

            foreach($order_items as $order_item){
                $order_item->cancellation_status="cancelled";

                $order_item->save();
               // $user_id.=$order_item->user_id;

            }
            $model->save();

            $from_email = $st->settings('email', 'from_email');
            $from_name = $st->settings('email', 'from_name');


            foreach( $client_emails as  $client_email){
                $cancelled_items=OrderItem::find()->where(['location_id'=>$model->location_name] and ['user_id'=>$client_email['id']])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();
                $start_time='';
                foreach($cancelled_items as $cancelled_item){
                    $cancelled_item->date;
                    $start_time.=$cancelled_item->start_time;

                }
            \Yii::$app->mailer->compose()
                ->setFrom([$from_email => $from_name])
                ->setTo($client_email['email'])
                ->setSubject('Regarding Cancellation of Classes')
                ->setHtmlBody(
                'regarding cancellation '.$cancelled_item->date .'<br>'.
                $start_time                
                )
                ->send();

            }          


            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

If I iterate the \Yii::$app->mailer->compose() in foreach, it will send mail for each class, whereas I want all classes cancelled for that user in single mail.

update: while @weegee solution is working as expected, but I think I have missed something in my code as you can see in the attached images, I have 3 classes cancelled. enter image description here

but the mail sent is for all the classes in the picture for 22nd July. like: regarding cancellation Date Start Time 2019-07-22 09:50:00 2019-07-22 09:00:00 2019-07-22 09:20:00 2019-07-22 10:00:00 2019-07-22 10:10:00

enter image description here

my update code looks like this:

foreach( $client_emails as $client_email){ $cancelled_items=OrderItem::find()->where(['location_id'=>$model->location_name] and ['user_id'=>$client_email['id'] and ['cancellation_status'=>'cancelled']])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all(); $body = "regarding cancellation"; $body .= "DateStart Time"; foreach($cancelled_items as $cancelled_item){

                $body.="<tr><td>".$cancelled_item->date  ."</td><td>". $cancelled_item->start_time."</td></tr>";

            }
            $body.="</table>";

but still it is including all the classes for 22July.


Solution

  • You can save all the time for a client in a string, in a list and then use that in the email

    foreach( $client_emails as  $client_email){
        $cancelled_items=OrderItem::find()->where(['location_id'=>$model->location_name] and ['user_id'=>$client_email['id']])->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();
        $body = "regarding cancellation 
        <ul>"; // create the ul element
        foreach($cancelled_items as $cancelled_item){
            $start_time = $cancelled_item->start_time;
            $body .= "<li>". $start_time."</li>"; // add the li with the start_time content inside
        }
        $body .= "</ul>"; // close the list
        \Yii::$app->mailer->compose()
        ->setFrom([$from_email => $from_name])
        ->setTo($client_email['email'])
        ->setSubject('Regarding Cancellation of Classes')
        ->setHtmlBody($body)
        ->send();
    }
    
    

    Don't fetch the users which have the cancellation status not set to canceled. Edit your $orders variable like this

    $order_items = OrderItem::find()->where(['location_id'=>$model->location_name])
    ->andwhere(['between', 'date', '', $model->from_date, $model->to_date ])
    ->andWhere("cancellation_status=:cancellation_status",array(':cancellation_status'=>'cancelled'))->all();