Completed
Push — master ( 02cc92...6df54b )
by Stephanie
02:42
created

FrmFormsListHelper::pagination()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmFormsListHelper extends FrmListHelper {
7
	public $status = '';
8
9
	public function __construct( $args ) {
10
		$this->status = self::get_param( array( 'param' => 'form_type' ) );
0 ignored issues
show
Documentation Bug introduced by
It seems like self::get_param(array('param' => 'form_type')) can also be of type array. However, the property $status is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
11
12
		parent::__construct( $args );
13
	}
14
15
	public function prepare_items() {
16
		global $wpdb, $per_page, $mode;
17
18
		$page = $this->get_pagenum();
19
		$per_page = $this->get_items_per_page( 'formidable_page_formidable_per_page' );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
20
21
		$mode    = self::get_param( array( 'param' => 'mode', 'default' => 'list' ) );
1 ignored issue
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
22
		$orderby = self::get_param( array( 'param' => 'orderby', 'default' => 'name' ) );
23
		$order   = self::get_param( array( 'param' => 'order', 'default'  => 'ASC' ) );
0 ignored issues
show
introduced by
Expected 1 space between "'default'" and double arrow; 2 found
Loading history...
24
		$start   = self::get_param( array( 'param' => 'start', 'default' => ( ( $page - 1 ) * $per_page ) ) );
25
26
        $s_query = array();
27
        $s_query[] = array( 'or' => 1, 'parent_form_id' => null, 'parent_form_id <' => 1 );
28
		switch ( $this->status ) {
29
		    case 'template':
30
                $s_query['is_template'] = 1;
31
                $s_query['status !'] = 'trash';
32
		        break;
33
		    case 'draft':
34
                $s_query['is_template'] = 0;
35
                $s_query['status'] = 'draft';
36
		        break;
37
		    case 'trash':
38
                $s_query['status'] = 'trash';
39
		        break;
40
		    default:
41
                $s_query['is_template'] = 0;
42
                $s_query['status !'] = 'trash';
43
		        break;
44
		}
45
46
		$s = FrmAppHelper::get_simple_request( array( 'param' => 's', 'sanitize' => 'sanitize_text_field' ) );
47
	    if ( $s != '' ) {
48
	        preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
49
		    $search_terms = array_map('trim', $matches[0]);
50
	        foreach ( (array) $search_terms as $term ) {
51
                $s_query[] = array(
52
                    'or'    => true, 'name LIKE' => $term, 'description LIKE' => $term, 'created_at LIKE' => $term,
53
                );
54
	            unset($term);
55
            }
56
	    }
57
58
		$this->items = FrmForm::getAll( $s_query, $orderby . ' ' . $order, $start . ',' . $per_page );
0 ignored issues
show
Documentation Bug introduced by
It seems like \FrmForm::getAll($s_quer...tart . ',' . $per_page) can also be of type object. However, the property $items is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
        $total_items = FrmDb::get_count( 'frm_forms', $s_query );
60
61
		$this->set_pagination_args( array(
62
			'total_items' => $total_items,
63
			'per_page' => $per_page,
64
		) );
65
	}
66
67
	public function no_items() {
68
	    if ( 'template' == $this->status ) {
69
            _e( 'No Templates Found.', 'formidable' ) ?>
70
            <br/><br/><?php _e( 'To add a new template:', 'formidable' ) ?>
71
			<ol><li><?php printf( __( 'Create a new %1$sform%2$s.', 'formidable' ), '<a href="' . esc_url( admin_url( 'admin?page=formidable&frm_action=new' ) ) . '"', '</a>' ) ?></li>
72
                <li><?php printf(__( 'After your form is created, go to Formidable -> %1$sForms%2$s.', 'formidable' ), '<a href="?page=formidable">', '</a>') ?></li>
73
                <li><?php _e( 'Place your mouse over the name of the form you just created, and click the "Create Template" link.', 'formidable' ) ?></li>
74
            </ol>
75
<?php   } else {
76
            _e( 'No Forms Found.', 'formidable' ) ?>
77
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable&frm_action=new' ) ) ?>"><?php _e( 'Add New', 'formidable' ); ?></a>
78
<?php   }
79
	}
80
81
	public function get_bulk_actions() {
82
	    $actions = array();
83
84
		if ( in_array( $this->status, array( '', 'published' ) ) ) {
85
	        $actions['bulk_create_template'] = __( 'Create Template', 'formidable' );
86
	    }
87
88
	    if ( 'trash' == $this->status ) {
89
	        if ( current_user_can('frm_edit_forms') ) {
90
	            $actions['bulk_untrash'] = __( 'Restore', 'formidable' );
91
	        }
92
93
	        if ( current_user_can('frm_delete_forms') ) {
94
	            $actions['bulk_delete'] = __( 'Delete Permanently', 'formidable' );
95
	        }
96
	    } else if ( EMPTY_TRASH_DAYS && current_user_can('frm_delete_forms') ) {
97
	        $actions['bulk_trash'] = __( 'Move to Trash', 'formidable' );
98
	    } else if ( current_user_can('frm_delete_forms') ) {
99
	        $actions['bulk_delete'] = __( 'Delete');
100
	    }
101
102
        return $actions;
103
    }
104
105
	public function extra_tablenav( $which ) {
106
        if ( 'top' != $which ) {
107
            return;
108
        }
109
110
        if ( 'trash' == $this->status && current_user_can('frm_delete_forms') ) {
111
?>
112
            <div class="alignleft actions frm_visible_overflow">
113
			<?php submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false ); ?>
114
            </div>
115
<?php
116
            return;
117
        }
118
119
        if ( 'template' != $this->status ) {
120
            return;
121
        }
122
123
		$where = apply_filters( 'frm_forms_dropdown', array(), '' );
124
		$forms = FrmForm::get_published_forms( $where );
125
126
        $base = admin_url('admin.php?page=formidable&form_type=template');
127
        $args = array(
128
            'frm_action'    => 'duplicate',
129
            'template'      => true,
130
        );
131
132
?>
133
    <div class="alignleft actions frm_visible_overflow">
134
    <div class="dropdown frm_tiny_top_margin">
135
        <a href="#" id="frm-templateDrop" class="frm-dropdown-toggle button" data-toggle="dropdown"><?php _e( 'Create New Template', 'formidable' ) ?> <b class="caret"></b></a>
136
		<ul class="frm-dropdown-menu" role="menu" aria-labelledby="frm-templateDrop">
137
		<?php
138
        if ( empty( $forms ) ) { ?>
139
            <li class="frm_dropdown_li"><?php _e( 'You have not created any forms yet. <br/>You must create a form before you can make a template.', 'formidable' ) ?></li>
140
        <?php
141
        } else {
142
            foreach ( $forms as $form ) {
143
		        $args['id'] = $form->id; ?>
144
			<li><a href="<?php echo esc_url( add_query_arg( $args, $base ) ); ?>" tabindex="-1"><?php echo esc_html( empty( $form->name ) ? __( '(no title)' ) : FrmAppHelper::truncate( $form->name, 33 ) ); ?></a></li>
145
			<?php
146
			    unset($form);
147
			}
148
        }
149
        ?>
150
		</ul>
151
	</div>
152
	</div>
153
<?php
154
	}
155
156
	public function get_views() {
157
158
		$statuses = array(
159
		    'published' => __( 'My Forms', 'formidable' ),
160
		    'template'  => __( 'Templates', 'formidable' ),
161
		    'draft'     => __( 'Drafts', 'formidable' ),
162
		    'trash'     => __( 'Trash', 'formidable' ),
163
		);
164
165
	    $links = array();
166
	    $counts = FrmForm::get_count();
167
		$form_type = self::get_param( array( 'param' => 'form_type', 'default' => 'published' ) );
168
169
	    foreach ( $statuses as $status => $name ) {
170
171
	        if ( $status == $form_type ) {
172
    			$class = ' class="current"';
173
    		} else {
174
    		    $class = '';
175
    		}
176
177
    		if ( $counts->{$status} || 'published' == $status ) {
178
				$links[ $status ] = '<a href="' . esc_url( '?page=formidable&form_type=' . $status ) . '" ' . $class . '>' . sprintf( __( '%1$s <span class="count">(%2$s)</span>', 'formidable' ), $name, number_format_i18n( $counts->{$status} ) ) . '</a>';
179
		    }
180
181
		    unset($status, $name);
182
	    }
183
184
		return $links;
185
	}
186
187
	public function pagination( $which ) {
188
		global $mode;
189
190
		parent::pagination( $which );
191
192
		if ( 'top' == $which ) {
193
			$this->view_switcher( $mode );
194
		}
195
	}
196
197
	public function single_row( $item, $style = '' ) {
198
	    global $frm_vars, $mode;
199
200
		// Set up the hover actions for this user
201
		$actions = array();
202
		$edit_link = '?page=formidable&frm_action=edit&id=' . $item->id;
203
		$duplicate_link = '?page=formidable&frm_action=duplicate&id=' . $item->id;
204
205
        $this->get_actions($actions, $item, $edit_link, $duplicate_link);
206
207
        $action_links = $this->row_actions( $actions );
208
209
		// Set up the checkbox ( because the user is editable, otherwise its empty )
210
		$checkbox = '<input type="checkbox" name="item-action[]" id="cb-item-action-' . absint( $item->id ) . '" value="' . esc_attr( $item->id ) . '" />';
211
212
		$r = '<tr id="item-action-' . absint( $item->id ) . '"' . $style . '>';
213
214
		list( $columns, $hidden ) = $this->get_column_info();
215
216
        $format = 'Y/m/d';
217
        if ( 'list' != $mode ) {
218
            $format .= ' \<\b\r \/\> g:i:s a';
219
		}
220
221
		foreach ( $columns as $column_name => $column_display_name ) {
222
			$class = $column_name . ' column-' . $column_name . ( 'name' == $column_name ? ' post-title page-title column-title' : '' );
223
224
			$style = '';
225
			if ( in_array( $column_name, $hidden ) ) {
226
                $class .= ' frm_hidden';
227
			}
228
229
			$class = 'class="' . esc_attr( $class ) . '"';
230
			$data_colname = ' data-colname="' . esc_attr( $column_display_name ) . '"';
231
			$attributes = $class . $style . $data_colname;
232
233
			switch ( $column_name ) {
234
				case 'cb':
235
					$r .= '<th scope="row" class="check-column">' . $checkbox . '</th>';
236
					break;
237
				case 'id':
238
				case 'form_key':
239
				    $val = $item->{$column_name};
240
				    break;
241
				case 'name':
242
				    $val = $this->get_form_name( $item, $actions, $edit_link, $mode );
243
			        $val .= $action_links;
244
245
				    break;
246
				case 'created_at':
247
				    $date = date($format, strtotime($item->created_at));
248
					$val = '<abbr title="' . esc_attr( date( 'Y/m/d g:i:s A', strtotime( $item->created_at ) ) ) . '">' . $date . '</abbr>';
249
					break;
250
				case 'shortcode':
251
					$val = '<input type="text" readonly="readonly" class="frm_select_box" value="' . esc_attr( '[formidable id=' . $item->id . ']' ) . '" /><br/>';
252
				    if ( 'excerpt' == $mode ) {
253
						$val .= '<input type="text" readonly="readonly" class="frm_select_box" value="' . esc_attr( '[formidable key=' . $item->form_key . ']' ) . '" />';
254
				    }
255
			        break;
256
			    case 'entries':
257
					if ( isset( $item->options['no_save'] ) && $item->options['no_save'] ) {
258
						$val = '<i class="frm_icon_font frm_forbid_icon frm_bstooltip" title="' . esc_attr('Saving entries is disabled for this form', 'formidable' ) . '"></i>';
259
			        } else {
260
			            $text = FrmEntry::getRecordCount($item->id);
261
						$val = current_user_can('frm_view_entries') ? '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-entries&form=' . $item->id ) ) . '">' . $text . '</a>' : $text;
262
                        unset($text);
263
                    }
264
			        break;
265
                case 'type':
266
                    $val = ( $item->is_template && $item->default_template ) ? __( 'Default', 'formidable' ) : __( 'Custom', 'formidable' );
267
                    break;
268
			}
269
270
			if ( isset($val) ) {
271
			    $r .= "<td $attributes>";
272
			    $r .= $val;
273
			    $r .= '</td>';
274
			}
275
			unset($val);
276
		}
277
		$r .= '</tr>';
278
279
		return $r;
280
	}
281
282
    /**
283
     * @param string $edit_link
284
     * @param string $duplicate_link
285
     */
286
    private function get_actions( &$actions, $item, $edit_link, $duplicate_link ) {
287
		if ( 'trash' == $this->status ) {
288
			if ( current_user_can('frm_edit_forms') ) {
289
				$actions['restore'] = FrmFormsHelper::delete_trash_link( $item->id, $item->status, 'short' );
290
			}
291
292 View Code Duplication
		    if ( current_user_can('frm_delete_forms') ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293
				$trash_url = wp_nonce_url( '?page=formidable&form_status=trash&frm_action=destroy&id=' . $item->id, 'destroy_form_' . $item->id );
294
				$actions['trash'] = '<a href="' . esc_url( $trash_url ) . '" class="submitdelete" onclick="return confirm(\'' . __( 'Are you sure you want to permanently delete that?', 'formidable' ) . '\')">' . __( 'Delete Permanently' ) . '</a>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
295
    		}
296
            return;
297
		}
298
299
		if ( current_user_can('frm_edit_forms') ) {
300
            if ( ! $item->is_template || ! $item->default_template ) {
301
				$actions['frm_edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit' ) . '</a>';
302
            }
303
304
		    if ( $item->is_template ) {
305
				$actions['frm_duplicate'] = '<a href="' . esc_url( wp_nonce_url( $duplicate_link ) ) . '">' . __( 'Create Form from Template', 'formidable' ) . '</a>';
306 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
				$actions['frm_settings'] = '<a href="' . esc_url( '?page=formidable&frm_action=settings&id=' . $item->id ) . '">' . __( 'Settings', 'formidable' ) . '</a>';
308
309
    		    if ( FrmAppHelper::pro_is_installed() ) {
310
					$actions['duplicate'] = '<a href="' . esc_url( wp_nonce_url( $duplicate_link ) ) . '">' . __( 'Duplicate', 'formidable' ) . '</a>';
311
        	    }
312
        	}
313
        }
314
315
		$actions['trash'] = FrmFormsHelper::delete_trash_link( $item->id, $item->status, 'short' );
316
		if ( empty( $actions['trash'] ) ) {
317
			// the user doesn't have permission
318
			unset( $actions['trash'] );
319
		}
320
321
		$actions['view'] = '<a href="' . esc_url( FrmFormsHelper::get_direct_link( $item->form_key, $item ) ) . '" target="_blank">' . __( 'Preview') . '</a>';
322
    }
323
324
    /**
325
     * @param string $edit_link
326
     */
327
	private function get_form_name( $item, $actions, $edit_link, $mode = 'list' ) {
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
328
        $form_name = $item->name;
329
        if ( trim($form_name) == '' ) {
330
            $form_name = __( '(no title)');
331
        }
332
		$form_name = FrmAppHelper::kses( $form_name );
333
		if ( 'excerpt' != $mode ) {
334
			$form_name = FrmAppHelper::truncate( $form_name, 50 );
335
		}
336
337
        $val = '<strong>';
338
        if ( 'trash' == $this->status ) {
339
            $val .= $form_name;
340
        } else {
341
			$val .= '<a href="' . esc_url( isset( $actions['frm_edit'] ) ? $edit_link : FrmFormsHelper::get_direct_link( $item->form_key, $item ) ) . '" class="row-title">' . FrmAppHelper::kses( $form_name ) . '</a> ';
342
        }
343
344
        $this->add_draft_label( $item, $val );
345
        $val .= '</strong>';
346
347
        $this->add_form_description( $item, $val );
348
349
        return $val;
350
    }
351
352
    /**
353
     * @param string $val
354
     */
355
    private function add_draft_label( $item, &$val ) {
356
        if ( 'draft' == $item->status && 'draft' != $this->status ) {
357
			$val .= ' - <span class="post-state">' . __( 'Draft', 'formidable' ) . '</span>';
358
        }
359
    }
360
361
    /**
362
     * @param string $val
363
     */
364
    private function add_form_description( $item, &$val ) {
365
        global $mode;
366
        if ( 'excerpt' == $mode ) {
367
            $val .= FrmAppHelper::truncate(strip_tags($item->description), 50);
368
        }
369
    }
370
}
371