/[drupal]/contributions/modules/ad_ubercart/ad_ubercart.admin.inc
ViewVC logotype

Contents of /contributions/modules/ad_ubercart/ad_ubercart.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Mon Jun 8 18:29:01 2009 UTC (5 months, 2 weeks ago) by neochief
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +82 -66 lines
File MIME type: text/x-php
Added Simpletests.
Fixed used quota query, which produced fake items in month select.
Refactoring.
1 <?php
2
3 /**
4 * @file
5 * Ad_ubercart administration routine.
6 */
7
8 /**
9 * Quota overview.
10 */
11 function ad_ubercart_quota() {
12 $result = pager_query(
13 // Fantastic query, which returns ad plan's quota and usage...
14 'SELECT aq.*, daily_used, monthly_used FROM {ad_quota} aq
15 LEFT JOIN (
16 SELECT count(SUBSTR(date, 1, 7)) as "daily_used", date, type
17 FROM {ad_used_quota}
18 WHERE type = "daily"
19 GROUP BY SUBSTR(date, 1, 7)) auq
20 ON aq.month = SUBSTR(auq.date, 1, 7)
21 LEFT JOIN (
22 SELECT count(date) as "monthly_used", date, type
23 FROM {ad_used_quota}
24 WHERE type = "monthly"
25 GROUP BY date) auq2
26 ON aq.month = auq2.date
27 ORDER BY aq.month DESC',
28 // ...for last 12 months
29 12, 0,
30 // and this is COUNT query
31 'SELECT count(*) FROM {ad_quota}');
32
33 // Prepare a table.
34 $header = array(t('Month'), t('Daily plans'), t('Monthly plans'), '');
35 while ($quota = db_fetch_array($result)) {
36 $daily_used = t('(Used: %used)', array('%used' => $quota['daily_used'] ? $quota['daily_used'] : 0));
37 $monthly_used = t('(Used: %used)', array('%used' => $quota['monthly_used'] ? $quota['monthly_used'] : 0));
38 $rows[] = array(
39 ad_ubercart_format_month($quota['month']),
40 $quota['daily'] .' '. $daily_used,
41 $quota['monthly'] .' '. $monthly_used,
42 l(t('edit'), 'admin/settings/ad_ubercart/quota/'. $quota['month'])
43 );
44 }
45
46 if (!empty($rows)) {
47 $output = theme('table', $header, $rows);
48 $output .= theme('pager');
49 }
50 else {
51 drupal_set_message(t('Please, <a href="!url">add</a> some quota first.', array('!url' => url('admin/settings/ad_ubercart/add_quota'))));
52 $output = t('There is no available quota.');
53 }
54
55 return $output;
56 }
57
58 /**
59 * Quota adding form.
60 */
61 function ad_ubercart_quota_form($form_state, $active_month = NULL) {
62 // Prepare a list of options if we have "add" form
63 $months = get_next_12_months($active_month);
64 $form['month'] = array(
65 '#type' => 'select',
66 '#options' => $months,
67 '#requitred' => TRUE,
68 '#title' => t('Month'),
69 );
70
71 // Load quota for specified month.
72 if ($active_month) {
73 $form['month']['#disabled'] = TRUE;
74 $form['month']['#value'] = $active_month;
75
76 // Also, get quota and usage amount.
77 $quota = db_fetch_array(db_query('
78 SELECT aq.*, daily_used, monthly_used FROM {ad_quota} aq
79 LEFT JOIN (
80 SELECT count(SUBSTR(date, 1, 7)) as "daily_used", date, type
81 FROM {ad_used_quota}
82 WHERE type = "daily"
83 GROUP BY SUBSTR(date, 1, 7)) auq
84 ON aq.month = SUBSTR(auq.date, 1, 7)
85 LEFT JOIN (
86 SELECT count(date) as "monthly_used", date, type
87 FROM {ad_used_quota}
88 WHERE type = "monthly"
89 GROUP BY date) auq2
90 ON aq.month = auq2.date
91 WHERE aq.month = "%s"
92 ORDER BY aq.month DESC', $active_month));
93 $form['#quota'] = $quota;
94 }
95 else {
96 // Default quota values.
97 $quota['daily'] = 5;
98 $quota['monthly'] = 25;
99 }
100
101 // Create rest of the form and fill it's defaults.
102 $form['daily'] = array(
103 '#type' => 'textfield',
104 '#title' => t('Allowed daily plans per month'),
105 '#requitred' => TRUE,
106 '#default_value' => $quota['daily'],
107 '#description' => $quota['daily_used'] ? format_plural($quota['daily_used'], '1 plan is already used.', '@count plans are already used, quota can not be less than this value.') : '',
108 );
109 $form['monthly'] = array(
110 '#type' => 'textfield',
111 '#title' => t('Allowed monthly plans per month'),
112 '#requitred' => TRUE,
113 '#default_value' => $quota['monthly'],
114 '#description' => $quota['monthly_used'] ? format_plural($quota['monthly_used'], '1 plan is already used.', '@count plans are already used, quota can not be less than this value.') : '',
115 );
116 $form['submit'] = array(
117 '#type' => 'submit',
118 '#value' => t('Save'),
119 );
120
121 return $form;
122 }
123
124 /**
125 * Returns list of next 12 months.
126 */
127 function get_next_12_months($active_month) {
128 // Add specified month to list only.
129 if ($active_month) {
130 $months[$active_month] = ad_ubercart_format_month($active_month);
131 }
132 // or 12 months if it's not specified
133 else {
134 $current_month = date('m');
135 $current_year = date('Y');
136 for ($h = 1; $h <= 12; $h++) {
137 $month = $current_year .'-'. sprintf("%02d",$current_month);
138 $months[$month] = ad_ubercart_format_month($month);
139
140 $current_month++;
141 if ($current_month > 13) {
142 $current_year++;
143 $current_month = 1;
144 }
145 }
146
147 // Remove existing months from a list.
148 $existing = get_quota();
149 if (!empty($existing)) {
150 foreach ($existing as $existing_month => $data) {
151 unset($months[$existing_month]);
152 }
153 }
154 }
155 return $months;
156 }
157
158 /**
159 * Quota form validator.
160 */
161 function ad_ubercart_quota_form_validate($form, &$form_state) {
162 if (!is_numeric($form_state['values']['daily']) || $form_state['values']['daily'] < 0) {
163 form_set_error('daily', t('Should be a positive number or zero.'));
164 }
165 if (!is_numeric($form_state['values']['monthly']) || $form_state['values']['monthly'] < 0) {
166 form_set_error('monthly', t('Should be a positive number or zero.'));
167 }
168 if (isset($form['#quota'])) {
169 if ($form_state['values']['daily'] < $form['#quota']['daily_used']) {
170 form_set_error('daily', t('Quota can not be less than already used.'));
171 }
172 if ($form_state['values']['monthly'] < $form['#quota']['monthly_used']) {
173 form_set_error('monthly', t('Quota can not be less than already used.'));
174 }
175 }
176 }
177
178 /**
179 * Quota form submit handler.
180 */
181 function ad_ubercart_quota_form_submit($form, &$form_state) {
182 if (isset($form['#quota'])) {
183 drupal_write_record('ad_quota', $form_state['values'], array('month'));
184 drupal_set_message(t('Quota for %month updated successfully!', array('%month' => ad_ubercart_format_month($form_state['values']['month']))));
185 }
186 else {
187 drupal_write_record('ad_quota', $form_state['values']);
188 drupal_set_message(t('Quota for %month added successfully!', array('%month' => ad_ubercart_format_month($form_state['values']['month']))));
189 }
190
191 $form_state['redirect'] = 'admin/settings/ad_ubercart';
192 }

  ViewVC Help
Powered by ViewVC 1.1.2