17 require_once SQ_CORE_PACKAGE_PATH.
'/system/triggers/trigger_action/trigger_action.inc';
18 require_once SQ_PACKAGES_PATH.
'/bulkmail/bulk_mailer.inc';
51 public static function execute($settings, &$state)
53 $bulkmail_job_id = array_get_index($settings,
'bulkmail_job_id', 0);
55 if ($bulkmail_job_id == 0)
return FALSE;
57 $asset_to_send = array_get_index($settings,
'asset_to_send', 0);
58 $current_asset = array_get_index($settings,
'current_asset', FALSE);
61 if (empty($state[
'asset'])) {
63 if (empty($state[
'assetid'])) {
66 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
70 if (is_null($state[
'asset']))
return FALSE;
73 $asset_to_send = $state[
'asset']->id;
76 $am = $GLOBALS[
'SQ_SYSTEM']->am;
77 $bm = $am->getSystemAsset(
'bulkmail_manager');
78 $job_asset = $am->getasset($bulkmail_job_id);
82 if ($asset_to_send == 0) {
83 $asset_to_send = $job_asset->attr(
'content_id');
86 $subscriptions = array_get_index($settings,
'subscriptions', Array());
87 $sub_status = array_get_index($settings,
'subscriptions_status', FALSE);
88 $recipient = array_get_index($settings,
'recipient', Array());
91 $post_office = $job_asset->getPostOffice();
95 $is_a_bm = get_class($post_office) ==
'Bulkmail_Manager';
96 $from = $job_asset->attr(
'from');
97 $recipient = empty($recipient) ? $job_asset->attr(
'recipients') : $recipient;
98 $gen_as = $job_asset->attr(
'content_gen_as');
99 $po_headers = $job_asset->attr(
'use_post_office_header') ? TRUE : FALSE;
100 $headers = $po_headers ? $post_office->attr(
'header_details') : $job_asset->attr(
'header_details');
101 $subject = $po_headers ? $post_office->attr(
'subject') : $job_asset->attr(
'subject');
102 $server_headers = $is_a_bm ? Array() : $post_office->attr(
'server_details');
103 $design = $job_asset->attr(
'content_design') ? $job_asset->attr(
'content_design') : 0;
104 $layout = $job_asset->attr(
'content_layout') ? $job_asset->attr(
'content_layout') : 0;
105 $post_office_id = $is_a_bm ? 0 : $post_office->id;
108 if ($sub_status && empty($subscriptions)) {
109 $subscriptions = $job_asset->attr(
'subscriptions');
116 $is_search_page = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($asset_to_send,
'search_page', TRUE);
118 if ($sub_status && !empty($subscriptions) && is_null($is_search_page)) {
119 $to_send_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($asset_to_send);
120 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
122 $users_to_mail = $bulk_mailer->getAllRecipients($asset_to_send, $recipient);
125 $recipient = Array();
127 foreach ($users_to_mail[
'recipient'] as $user_info) {
128 $user_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($user_info[
'assetid']);
129 if (!is_null($user_asset)) {
130 foreach ($subscriptions as $sub_name => $sub_info) {
131 foreach ($sub_info as $index => $info) {
133 $category_type = array_keys($info);
134 $user_metadata = $mm->getMetadataValueByAssetid($user_asset->id, $info[$category_type[0]][
'assetid']);
135 $rule_text = $info[$category_type[0]][
'rule_text'];
137 $old_frontend_asset = $GLOBALS[
'SQ_SYSTEM']->frontend_asset;
138 $GLOBALS[
'SQ_SYSTEM']->frontend_asset = $to_send_asset;
139 $GLOBALS[
'SQ_SYSTEM']->replaceKeyword($rule_text);
140 $GLOBALS[
'SQ_SYSTEM']->frontend_asset = $old_frontend_asset;
142 if ($info[$category_type[0]][
'rule_cond'] ==
'contains') {
143 $match = (stripos($user_metadata, $rule_text) !== FALSE);
146 $match = (strtolower($user_metadata) == strtolower($rule_text));
149 $recipient[] = $user_info[
'assetid'];
155 $subscriptions = Array();
158 if (!empty($recipient)) {
159 if (!$bm->createAdHocJob($from, $recipient, $subject, $asset_to_send, $gen_as, $headers, $post_office_id, $server_headers, $design, $layout, $subscriptions))
164 'job_id' => $job_asset->id,
180 public static function getInterface($settings, $prefix, $write_access=FALSE)
182 $bulkmail_job_id = array_get_index($settings,
'bulkmail_job_id', 0);
183 $subscriptions = array_get_index($settings,
'subscriptions', Array());
184 $sub_status = array_get_index($settings,
'subscriptions_status', FALSE);
185 $asset_to_send = array_get_index($settings,
'asset_to_send', 0);
186 $current_asset = array_get_index($settings,
'current_asset', FALSE);
187 $recipient = array_get_index($settings,
'recipient', Array());
189 $option_type = Array(
190 'matches' =>
'Matches',
191 'contains' =>
'Contains',
194 $new_rule_type = Array(
195 '0' =>
'-- Add New Rule --',
196 'metadata' =>
'Asset Metadata field',
206 echo translate(
'asset_to_send');
212 asset_finder($prefix.
'[asset_to_send]', $asset_to_send);
213 check_box($prefix.
'[current_asset]',
'1', $current_asset,
'',
'id="'.$prefix.
'_current_asset"');
214 label(translate(
'use_current_asset'), $prefix.
'_current_asset');
216 if ($asset_to_send) {
217 echo get_asset_tag_line($asset_to_send);
220 echo translate(
'use_current_asset');
221 echo
' <img src="'.sq_web_path(
'lib').
'/web/images/'.($current_asset ?
'tick' :
'cross').
'.gif" alt="'.($current_asset ? translate(
'yes') : translate(
'no')).
'" /> ';
230 echo translate(
'bulkmail_job');
236 asset_finder($prefix.
'[bulkmail_job_id]', $bulkmail_job_id, Array(
'bulkmail_job' =>
'I'));
238 if ($bulkmail_job_id) {
239 echo get_asset_tag_line($bulkmail_job_id);
241 echo translate(
'not_configured');
250 echo translate(
'recipients');
256 multiple_asset_finder($prefix.
'[recipient]', $recipient, Array(
'user' =>
'D',
'user_group' =>
'D'));
259 foreach ($recipient as $res) {
260 echo get_asset_tag_line($res);
264 echo translate(
'not_configured');
273 echo translate(
'enable_subscription_filter');
279 check_box($prefix.
'[subscriptions_status]',
'1', $sub_status);
281 echo
' <img src="'.sq_web_path(
'lib').
'/web/images/'.($sub_status ?
'tick' :
'cross').
'.gif" alt="'.($sub_status ? translate(
'yes') : translate(
'no')).
'" /> ';
292 echo translate(
'filters');
297 echo
'<table class ="sq-backend-table" border="1" style="margin-bottom: 7px">';
298 echo
'<tr><td rowspan =2 width="12%">Name</td><td colspan="3">Rules</td></tr>';
299 echo
'<tr><td width="15%">Type</td><td>Details</td><td>Delete?</td></tr>';
301 foreach ($subscriptions as $name => $subscription) {
302 hidden_field($prefix.
'[subscriptions]['.$name.
']',
'');
305 $rowspan = ($write_access) ? (count($subscription) + 1) : count($subscription);
306 if (empty($subscription)) $rowspan++;
307 echo
'<th rowspan='.$rowspan.
'>'.$name.
'</th>';
308 if (!empty($subscription)) {
309 foreach ($subscription as $index => $rules) {
310 if ($tr) echo
"<tr>";
311 $type_data = array_keys($rules);
312 echo
'<td>'.$new_rule_type[$type_data[0]].
'</td>';
315 asset_finder($prefix.
'[subscriptions]['.$name.
']['.$index.
']['.$type_data[0].
'][assetid]', $rules[$type_data[0]][
'assetid'], Array(
'metadata_field' =>
'D'));
317 echo get_asset_tag_line($rules[$type_data[0]][
'assetid']);
319 echo
' ';
321 combo_box($prefix.
'[subscriptions]['.$name.
']['.$index.
']['.$type_data[0].
'][rule_cond]', $option_type, FALSE, $rules[$type_data[0]][
'rule_cond']);
323 echo $rules[$type_data[0]][
'rule_cond'];
325 echo
' ';
327 text_box($prefix.
'[subscriptions]['.$name.
']['.$index.
']['.$type_data[0].
'][rule_text]', $rules[$type_data[0]][
'rule_text'], 30);
329 echo $rules[$type_data[0]][
'rule_text'];
333 if ($write_access) check_box($prefix.
'[subscriptions]['.$name.
']['.$index.
'][delete]');
339 echo
'<td colspan=3></td></tr>';
346 combo_box($prefix.
'[subscriptions]['.$name.
'][new_rule]', $new_rule_type, FALSE);
355 label(translate(
'new_category'), $prefix.
'[new_category_name]');
357 text_box($prefix.
'[new_category_name]',
'', 15);
370 $html = ob_get_clean();
390 $settings[
'bulkmail_job_id'] = array_get_index($request_data[
'bulkmail_job_id'],
'assetid', 0);
391 $settings[
'subscriptions_status'] = array_get_index($request_data,
'subscriptions_status', FALSE);
392 $settings[
'asset_to_send'] = array_get_index($request_data[
'asset_to_send'],
'assetid', 0);
393 $settings[
'current_asset'] = array_get_index($request_data,
'current_asset', FALSE);
396 $raw_userids = $request_data[
'recipient'];
398 foreach ($raw_userids as $user) {
399 if (!empty($user[
'assetid'])) {
400 $userids[] = $user[
'assetid'];
403 $settings[
'recipient'] = $userids;
405 if ($settings[
'current_asset']) $settings[
'asset_to_send'] = 0;
407 if ($settings[
'subscriptions_status']) {
408 $new_subscriptions = Array();
409 $subscriptions = array_get_index($request_data,
'subscriptions', Array());
410 $new_category_name = array_get_index($request_data,
'new_category_name',
'');
411 if (!empty($subscriptions)) {
412 foreach ($subscriptions as $sub_name => $subscription) {
413 foreach ($subscription as $index => $subscription_data) {
414 if ($index ===
'new_rule') {
415 if ($subscription[
'new_rule'] !==
'0') {
416 $new_subscriptions[$sub_name][][$subscription[
'new_rule']] = Array(
418 'rule_cond' =>
'contains',
425 if (!isset($subscription_data[
'delete'])) {
426 $category = array_keys($subscription_data);
427 $new_subscriptions[$sub_name][][$category[0]] = Array(
428 'assetid' => $subscription_data[$category[0]][
'assetid'][
'assetid'],
429 'rule_cond' => array_get_index($subscription_data[$category[0]],
'rule_cond',
'contains'),
430 'rule_text' => array_get_index($subscription_data[$category[0]],
'rule_text',
''),
437 if (!empty($new_category_name)) {
438 $new_subscriptions[$new_category_name] = Array();
440 $settings[
'subscriptions'] = $new_subscriptions;